29

Please consider the following situation:

std::unordered_map<int, std::vector<A>> elements;

Now I'm iterating over this unordered map:

for (auto it = elements.begin(); it != elements.end(); ++it)

Inside the loop, I'm forming clusters out of several elements of elements (the current one that it points to and some more, not necessarily those next in line!). Because each element can be only part of one cluster, I'd like to remove those from the map and then continue with the next element (i.e., build the next cluster).

How can I do this and still continue the iteration at the correct position?

2

3 Answers 3

52
for (auto it = elements.begin(); it != elements.end();) {
   if(you have to rease) {
      it = elements.erase(it);
   }
   else
      it++;
}

This way you make sure you don't increment after erasing and alos you don't increment the end().

After suggestions that there's no good reason to have for loop that doesn't increment anything you might want to us a while loop instead. Mainly for more readability.

Sign up to request clarification or add additional context in comments.

2 Comments

Style nitpick: there's no good reason to have a for-loop that doesn't increment anything. This would probably be better as a while-loop.
@MichaelKristofik The for-loop is useful here; It serves the purpose of initializing the iterator and restrict its scope.
21

unordered_map::erase will return the iterator past the deleted element.

You would want code along the lines of:

it = myMap.erase( it );

Important note: If you are doing this within a loop, you'll want to make sure that you avoid the typical increment of it at the end of the loop if this is done.

Comments

7

The erase function returns the iterator after the removed element (all collections erase functions do). You can use that to continue.

Just remember to not increase the iterator in the case when you erase an element, or you will step over one element.

2 Comments

set::erase() returns the number of elements removed.
Or should I say "the value based version of erase() returns the number of elements removed". It's a bit misleading to say "all collections erase functions does".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.