Calihog
Calihog

Reputation: 135

Erasing an equal_range iterator

I've got a pair of iterators:

pair <multimap<CFile,Filetype>::iterator, multimap<CFile,Filetype>::iterator> range;
range = m_DirectoryMap.equal_range(obj);

That pair holds duplicated elements in a MultiMap - e.g. there is 1 object that has 2 more duplicates (so basically 3 objects) and I need to remove 2 of them, so only 1 is left.

I was doing this by a simple while loop, like this:

auto it = range.first;
++it;
while (it != range.second)
        it = m_DirectoryMap.erase(it);

After that, only 1 object was left - which is my goal. Later I have found that I should probably try and erase the whole pair by 1 function call and there shouldn't be any needs for loops, like this:

m_DirectoryMap.erase(range.first, range.second);

This seems more cleaner, but the problem is that it removes all objects.

Then I tried:

m_DirectoryMap.erase(++range.first, range.second);

This seems to leave the first object and remove the rest, so it is working for me, but my question is - is this the right way to do, what I'm looking for?

Upvotes: 1

Views: 1113

Answers (0)

Related Questions