How to remove values from a QMap?

Viewed 17954

Suppose I want to remove items according to some criterium. Let's say:

QMap<int, int> map;

and I want to remove all the items where value is an odd number. If I use an iterator:

for (auto it = map.begin(); it != map.end(); ++it)
    if (it.value() % 2 == 1)
        map.remove(it.key());

This code is probably wrong, since the call of

map.remove(it.key()) 

invalidates the iterator. How can I do this without reseting the iterator after each removal?

2 Answers
Related