P
Pieter Thysebaert
Hello,
I've got a question conerning erasing key-value pairs from a std::map while
iterating over it.
According to the STL docs, erasing an element in a map invalidates all
iterators pointing to that element
so
for (map<...>::iterator i = mymap.begin(); i != mymap.end(); i++) {
if (test(i)) {
mymap.erase(i);
mymap.insert(newelement);
}
}
looks like bad practice, and it does crash when it gets executed, at least
on one machine i've run such code on.
So my question is, how do I conveniently erase the "current" element in a
map on the fly while I'm iterating over it? (I admit that this sounds as
weird in English as it sounds in C++)?
Thanx,
Pieter
I've got a question conerning erasing key-value pairs from a std::map while
iterating over it.
According to the STL docs, erasing an element in a map invalidates all
iterators pointing to that element
so
for (map<...>::iterator i = mymap.begin(); i != mymap.end(); i++) {
if (test(i)) {
mymap.erase(i);
mymap.insert(newelement);
}
}
looks like bad practice, and it does crash when it gets executed, at least
on one machine i've run such code on.
So my question is, how do I conveniently erase the "current" element in a
map on the fly while I'm iterating over it? (I admit that this sounds as
weird in English as it sounds in C++)?
Thanx,
Pieter