stl

vector iterators incompatible

那年仲夏 提交于 2019-12-22 08:57:12
问题 I'm currently working on a graph library for C++ and now got stuck at a point where I get an assertion error in debug mode during runtime. I also had a look an some other question here on SO but none of the questions and answers lead me to a solution. After reading in some forums I have the impression that this error happens because iterators become invalid as soon as the vector content is changed. (for example when using erase() ) But as you can see in my code, I'm not modifying the vector,

Integer index-able RAII container for non-copyable type

不想你离开。 提交于 2019-12-22 08:55:42
问题 Is there a standard container that has the same general API as vector<T> but that populates new locations via direct default construction? Background: I have a type that disallows copying but has a default constructor and what I really want to do is this: vector<NoCopy> bag(some_size); // use bag[i]'s return; // bag & contents get correctly cleaned up. However, this doesn't work because vector<T>(int) is implemented in terms of default constructing an object and then copying it into each of

'ext/slist' file not found on OS X 10.9

孤街醉人 提交于 2019-12-22 08:54:50
问题 I am trying to get some older third-party software to compile on OS X 10.9. I've managed to get rid of most compilation problems by adjusting settings in the Makefiles, which were originally written for gcc probably around 2005. However, I currently don't know how to overcome this error for a C++ source file: /utility.h:42:10: fatal error: 'ext/slist' file not found I understand that ext/slist belongs to some version of STL. Has that version been superseded or does it have to be activated in

Which STL container should I use? C++

不问归期 提交于 2019-12-22 08:46:18
问题 I have a 'list' of objects, from which I want to take object at random position and push it on front of this list. Only this kind of operation will be performed. So I don't need a fast access to end of the list, only to it's front and average access to any other place. Which container would be the best for this? I was thinking about std::vector , but I've read that insert operation is not efficient. Then I came up with std::deque because of it's fast access to front, but what about efficiency

How to use experimental parallel STL in C++1z?

断了今生、忘了曾经 提交于 2019-12-22 08:35:45
问题 I want to try parallel STL of C++17. However, I can't find experimental/execution_policy in libc++. How can I try this? I'm trying http://en.cppreference.com/w/cpp/experimental/reduce, which says I should include these files, but I cannot find execution_policy. #include <experimental/execution_policy> After I install libc++ (I followed http://libcxx.llvm.org/docs/BuildingLibcxx.html), I tried the following commands, but in vain. $ clang++-3.5 -std=c++1z test.cpp -lc++experimental test.cpp:5

How do I create a set with std::pair thats sorted based on the ::second pair member using bind

北城余情 提交于 2019-12-22 08:28:55
问题 I know I could use the following: template <typename Pair> struct ComparePairThroughSecond : public std::unary_function<Pair, bool> { bool operator ()(const Pair& p1, const Pair& p2) const { return p1.second < p2.second; } }; std::set<std::pair<int, long>, ComparePairThroughSecond> somevar; but wondered if it could be done with boost::bind 回答1: How about the following one. I'm using boost::function to 'erase' the actual type of the comparator. The comparator is created using boost:bind itself

Why is it necessary to to use set.find(x) != set.end() while finding an element.

一曲冷凌霜 提交于 2019-12-22 08:27:10
问题 I am wondering what is wrong when I use *(set.find(x)) == x instead of set.find(x)!=set.end() . It usually works but while attempting a question on Hackerrank (question : link). This code gives CA for all test cases : int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ set<int>s; int n,x,y; cin >> n; while(n--){ cin >> y >> x; if(y==1) s.insert(x); else if(y==2) s.erase(x); else { set<int>::iterator it=s.find(x); if(it != s.end()) cout << "Yes" <<endl; else

how to move an std::unique_ptr<> from one STL container to another?

為{幸葍}努か 提交于 2019-12-22 08:26:35
问题 Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T> member. Inside method, send_to_purgatory_if( predicate ) , I would like to look at all items in m_taskdq and move items from m_taskdq to m_purgatory , if the predicate evaluates to true. Issues I have two issues that I'm struggling with: my iterator it gets trashed if I remove items from m_taskdq from inside the loop I am worried about the state of the std::unique_ptr<> if I do

Inheriting std::vector::iterator for custom class?

▼魔方 西西 提交于 2019-12-22 08:12:10
问题 I am implementing a custom class that contains an STL std::vector as central data member. Now, I would like this class to provide an iterator, which merely needs to iterate through this vector and also works with C++11 range based iteration. It is very tempting to just somehow inherit the iterator from std::vector::iterator as it is supposed to do exactly the same job. Is this possible or do I need to implement a completely custom iterator? class Custom { private: std::vector<double> _data;

Refactoring a “dumb” function into generic STL-style with iterators to containers

孤人 提交于 2019-12-22 08:06:55
问题 I've managed to wrap my head around some of C++'s functional capacities (for_each, mapping functions, using iterators...) but the construction of the templates and function argument lists for taking in generic containers and iterators still eludes me. I have a practical example I'm hoping someone can illustrate for me: Take the following function that processes an incoming std::vector and builds a running total of many data-points/iterations of a process: /* the for-loop method - not very