Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL Multimap Remove/Erase Values

Tags:

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.

like image 967
Avinash Avatar asked Jan 22 '10 07:01

Avinash


People also ask

How do you delete an element from a multimap?

erase() is used to remove or erase elements from a multimap container. This function can remove or erase the elements by its key, position or the given range. When we run this function the size of the multimap container is reduced by the number of elements being removed.

How do you value a multimap?

multimap::find( ) an inbuilt function in C++ STL, which is defined in <map> header file. find() searches elements in the container which are associated with key K. This function returns an iterator pointing to the single element in a container. It returns an iterator if the element found in the container.

Can you sort a multimap?

You cannot do that. Multimap in C++ STL is ordered and the order cannot/must not be changed (I think at the bottom line it is using a balanced binary tree for the keys I think, not sure though). What you can do is instantiate a new multimap object passing a comparator that fits your needs (research strict weak order).

What is STD multimap?

} (2) (since C++17) Multimap is an associative container that contains a sorted list of key-value pairs, while permitting multiple entries with the same key. Sorting is done according to the comparison function Compare , applied to the keys.


1 Answers

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); } 
like image 126
Nikola Smiljanić Avatar answered Sep 20 '22 11:09

Nikola Smiljanić