Linked Questions

0 votes
2 answers
3k views

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 ...
Gandalf458's user avatar
  • 2,277
5 votes
1 answer
3k views

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?
user avatar
1 vote
2 answers
1k views

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<...
Mbroo's user avatar
  • 39
0 votes
1 answer
3k views

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++); ...
Devesh Agrawal's user avatar
0 votes
1 answer
349 views

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: ...
lws803's user avatar
  • 947
1 vote
1 answer
54 views

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. ...
jmasterx's user avatar
  • 54.5k
0 votes
0 answers
80 views

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 ...
Collin's user avatar
  • 1,917
0 votes
0 answers
67 views

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[]) { ...
Milan's user avatar
  • 2,147
0 votes
0 answers
54 views

for(auto it = prefixSet.begin();it!=prefixSet.end();it++) { string str = *it; if(prefixSet.count(str) > 1) { cout << prefixSet.count(str) << " " &...
Anup Buchke's user avatar
  • 5,570
254 votes
15 answers
107k views

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 ) { ...
0xC0DEFACE's user avatar
  • 9,300
151 votes
14 answers
89k views

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 ...
aJ.'s user avatar
  • 35.7k
140 votes
3 answers
87k views

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 ...
Matthew Smith's user avatar
29 votes
9 answers
25k views

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 ...
user avatar
29 votes
4 answers
25k views

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?...
1800 INFORMATION's user avatar
15 votes
7 answers
3k views

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++ ) { /...
volvox's user avatar
  • 3,068

15 30 50 per page