tags:

views:

265

answers:

1

Hi I have STL Multimap, I want to remove entries from the map which has specific value , I do not want to remove entire key, as that key may be mapping to other values which are required.

any help please.

A: 

If I understand correctly these values can appear under any key. If that is the case you'll have to iterate over your multimap and erase specific values.

typedef std::multimap<std::string, int> Multimap;
Multimap data;

for (Multimap::iterator iter = data.begin(); iter != data.end();)
{
    // you have to do this because iterators are invalidated
    Multimap::iterator erase_iter = iter++;

    // removes all even values
    if (erase_iter->second % 2 == 0)
        data.erase(erase_iter);
}
Nikola Smiljanić
Yes you are correct, values can appear under any key.
Avinash
Thanks This works for me, I was looking for using remove_if algorithms.
Avinash
I'm afraid `remove_if` from `<algorithm>` only works for containers where it is possible to reassign values (vector, deque, list - except suboptimal for the last) doing `*it1 = *it2`. This is not possible for map, as it might break the ordering.
UncleBens