Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a specific pair from a C++ multimap?

#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?

like image 657
Jim Blackler Avatar asked Oct 17 '10 07:10

Jim Blackler


People also ask

How do I delete a pair in multimap?

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.

How do I remove a pair from a list in C++?

list<pair<string,int>> l0 { { "name1", 20 }, { "name2", 30 }, { "name3", 40 } }; for( auto &it : l0 ) l0 . erase( it );

How do you access elements in 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.

How do you reverse a multimap in C++?

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.


2 Answers

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;
    }
}
like image 79
Charles Salvia Avatar answered Oct 16 '22 13:10

Charles Salvia


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;
}
like image 3
Paz Avatar answered Oct 16 '22 13:10

Paz