Linked Questions
32 questions linked to/from How to remove from a map while iterating it?
0 votes
2 answers
3k views
Does std::map::erase(it++) maintain a valid iterator pointing to the next element in the map? [duplicate]
I'm using Xcode with C++ 11 for a std::map. Some elements in my map have a flag that says they need to be removed. I want to iterate through the map, erasing the flagged elements in O(n) time. The ...
5 votes
1 answer
3k views
Safe to erase from map inside range based for? [duplicate]
I often use this syntax to loop through a std::map: for( const auto& my_pair: my_map ) Can I call my_map.erase( my_pair.first ); safely?
1 vote
2 answers
1k views
C++ map container erase() segmentation fault [duplicate]
I use this code to remove elements from map container that equal to some int. for(auto x:m){ if((x.second)==element)m.erase(x.first); } As result Segmentation fault. I also tried this: for(map<...
0 votes
1 answer
3k views
Delete all entries in STL Map which matches value [duplicate]
I'm trying to delete all entries in a map where value == 50 for any key. This code is working fine for me. while (itr != mymap.end()) { if ((*itr).second == 50) mymap.erase(itr++); ...
0 votes
1 answer
349 views
Performing recursion on a list C++ [duplicate]
I am trying to determine the maximum number of items I can remove from a list using std list to get the minimum size. However, it keeps ending up in bad memory access. This is my recursive function: ...
1 vote
1 answer
54 views
Map Not Incrementable Error [duplicate]
I am using a small library to have a database connection pool. However, when I run the provided example, at the point where it calls DestroyPool I get a map is not increment-able assertion fail. ...
0 votes
0 answers
80 views
Can you safely use a std::set as the key to a std::map? [duplicate]
I have defined the following: typedef std::set<std::string> entry; typedef std::map<entry, size_t> freq_map; In this way, I can read in lines from a text file of this form and keep track ...
0 votes
0 answers
67 views
Erasing the first key-value pair from a map (in a for loop) gives segmentation fault [duplicate]
I have a std::map<int, std::string> and my aim is to remove a key-value pair based on a certain condition. For simplicity, let say, it looks like this: int main(int argc, char const *argv[]) { ...
0 votes
0 answers
54 views
Multiset erasing only one instance [duplicate]
for(auto it = prefixSet.begin();it!=prefixSet.end();it++) { string str = *it; if(prefixSet.count(str) > 1) { cout << prefixSet.count(str) << " " &...
254 votes
15 answers
107k views
How to call erase with a reverse iterator
I am trying to do something like this: for ( std::list< Cursor::Enum >::reverse_iterator i = m_CursorStack.rbegin(); i != m_CursorStack.rend(); ++i ) { if ( *i == pCursor ) { ...
151 votes
14 answers
89k views
remove_if equivalent for std::map
I was trying to erase a range of elements from map based on particular condition. How do I do it using STL algorithms? Initially I thought of using remove_if but it is not possible as remove_if does ...
140 votes
3 answers
87k views
What happens if you call erase() on a map element while iterating from begin to end?
In the following code I loop through a map and test if an element needs to be erased. Is it safe to erase the element and keep iterating or do I need to collect the keys in another container and do a ...
29 votes
9 answers
25k views
Erase/Remove contents from the map (or any other STL container) while iterating it
Allegedly you cannot just erase/remove an element in a container while iterating as iterator becomes invalid. What are the (safe) ways to remove the elements that meet a certain condition? please only ...
29 votes
4 answers
25k views
How to filter items from a std::map? [duplicate]
I have roughly the following code. Could this be made nicer or more efficient? Perhaps using std::remove_if? Can you remove items from the map while traversing it? Can we avoid using the temporary map?...
15 votes
7 answers
3k views
Should I expect to see the counter in `for` loop changed inside its body? [closed]
I'm reading someone else's code and they separately increment their for loop counter inside the loop, as well as including the usual afterthought. For example: for( int y = 4; y < 12; y++ ) { /...