I have a map which elements are vectors.I have to delete from these vectors all elements which are equal to special number num
std::map<size_t,std::vector<size_t> > myMap;
for (std::map<size_t,std::vector<size_t> >::iterator itMap = myMap.begin();itMap != myMap.end();++itMap )
{
for (std::vector<size_t>::iterator itVec = itMap->second.begin();itVec != itMap->second.end();)
{
auto itNextVec = itVec;
++itNextVec;
if (*itVec == num)
{
itMap->second.erase(itVec );
}
itVec = itNextVec;
}
}
The code causes run-time exepssion .In VS - vector iterators incompatible
.
Can someone point what is the cause for that?
Thanks
Consider either using a different collection class than
vector
or creating a new vector with the desired items removed rather than removing from existing vector.erasing will invalidate the iterator
You can't trivially erase an item from a collection while iterating over it. Think a little about it, your removing what
itVec
"points" to, after the removalitVec
no longer "points" to an element, so it no longer have a "next" pointer.If you check e.g. this reference, you will see that the
erase
function returns an iterator to the next element. Continue the loop with this one (without increasing it of course).std::vector::erase
returns aniterator
to the next position of the list, and so when you do an erase you should make your iterator equal to the returned value.The only thing that you have to consider is that the returned
iterator
could be the end so you should check for that.What I personally like to do is is after doing in an erase and I get the next iterator position, I go back to the previous position of the returned iterator and than call a continue on the
for loop
Example:
But doing an erase inside of a iterator loop is frowned upon because it invalidates the old iterator and that could cause a lot of issues if you didn't plan for them.