Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing elements from a C++ map through a for-loop

Tags:

c++

c++11

map

stl

My STL is a bit rusty, so forgive me for asking a possibly trivial question. Consider the following piece of code:

map<int,int> m;
...
for (auto itr = m.begin(); itr != m.end(); ++itr) {
    if (itr->second == 0) {
        m.erase(itr);
    }
}

The question is: Is it safe to erase elements while looping over the map?

like image 901
Petter Avatar asked Mar 04 '11 09:03

Petter


People also ask

How do you delete a map while iterating?

Remove entries from a map while iterating it in C++ Since calling the erase() function invalidates the iterator, we can use the return value of erase() to set the iterator to the next element in the sequence.

Which code snippet will remove all elements from a map in the correct way?

Using clear() : This function clears all the elements present in the map. After this function is called, the size of map becomes 0.


2 Answers

Yes, but not the way you do it. You're invalidating itr when you erase, then incrementing the invalid iterator.

auto itr = m.begin();
while (itr != m.end()) {
  if (itr->first == 0) {
    m.erase(itr++);
  } else {
    ++itr;
  }
}
like image 137
Erik Avatar answered Oct 14 '22 21:10

Erik


I think that you shouldn't use removed iterator at all - in case of lists this causes serious problems, shouldn't be different for maps.

EDIT by Matthieu M: this code is well-formed in C++0x and allowed as an extension by MSVC.

map<int,int> m;
...
auto itr = m.begin();
while (itr != m.end())
{
    if (itr->second == 0) {
        itr = m.erase(itr);
    }
    else 
    {
        itr++;
    }
}
like image 35
XAder Avatar answered Oct 14 '22 22:10

XAder