#include <map>
...
multimap<char,int> mymap;
mymap.insert(pair<char,int>('a',10));
mymap.insert(pair<char,int>('b',15));
mymap.insert(pair<char,int>('b',20));
mymap.insert(pair<char,int>('c',25));
Say I now want to remove one of the pairs I have just added to the map.
I have examples to remove an entire key entry, which for key 'b' would remove both 'b',15 and 'b',20.
But what is the code to remove just, say, the pair 'b',20?
multimap::erase() function is an inbuilt function in C++ STL, which is defined in <map> header file. 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.
list<pair<string,int>> l0 { { "name1", 20 }, { "name2", 30 }, { "name3", 40 } }; for( auto &it : l0 ) l0 . erase( it );
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.
C++ multimap rbegin() function. The C++ multimap rbegin() function is used to return a reverse iterator referring to the last element of the multimap container. A reverse iterator of multimap moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multimap container.
You can use std::multimap<char, int>::equal_range
, which will give you an iterator range containing all pairs which have a certain key. So if you look for 'b', you will get an iterator range containing all pairs which have 'b' as the key.
You can then simply iterate over the range, and erase any pair you see fit, by erasing the iterator.
multimap<char,int> mymap;
mymap.insert(pair<char,int>('a',10));
mymap.insert(pair<char,int>('b',15));
mymap.insert(pair<char,int>('b',20));
mymap.insert(pair<char,int>('c',25));
typedef multimap<char, int>::iterator iterator;
std::pair<iterator, iterator> iterpair = mymap.equal_range('b');
// Erase (b,15) pair
//
iterator it = iterpair.first;
for (; it != iterpair.second; ++it) {
if (it->second == 15) {
mymap.erase(it);
break;
}
}
In case you need to continue iterating after the first match you need to first retrieve an iterator to the next element since the erased iterator gets invalidated.
One way to achieve this, starting from C++11, is to use the return value of the erase function which is an iterator to the element that follows the last element removed (or multimap::end, if the last element was removed). Beware the key based version returns the number of elements erased, not an iterator.
Building on top of the valuable Charles Salvia answer, showing how to erase (b,15 ) pair, you get
multimap<char,int> mymap;
mymap.insert(pair<char,int>('a',10));
mymap.insert(pair<char,int>('b',15));
mymap.insert(pair<char,int>('b',20));
mymap.insert(pair<char,int>('c',25));
typedef multimap<char, int>::iterator iterator;
std::pair<iterator, iterator> iterpair = mymap.equal_range('b');
// Erase (b,15) pair
//
iterator it = iterpair.first;
for (; it != iterpair.second; ) {
if (it->second == 15) {
it=mymap.erase(it);
}
else
++it;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With