The C++ multimap erase function is used to remove either a single element associated with given key value or a range of elements from the multimap container. Hence, the size will be reduced by the number of elements removed.
Syntax
void erase (iterator position); //until C++ 11
size_type erase (const key_type& k); //until C++ 11
void erase (iterator first, iterator last); //until C++ 11
iterator erase (const_iterator position); //since C++ 11
size_type erase (const key_type& k); //since C++ 11
iterator erase (const_iterator first, const_iterator last); //since C++ 11
Parameter
position : Iterator pointing to a single element to be removed from the multimap.
k : Key of the element to be removed from the multimap.
first : Beginning of the range to erase.
last : End of the range to erase.
Return value
It returns an iterator thacpp tutorials to the next element of the deleted element or returns the number of deleted elements.
Complexity
erase(position) : Amortized constant.
erase (val) : Logarithmic in container size.
erase(first,last) : Linear in the distance between first and last.
Iterator validity
Iterators, references and the pointers pointing to elements removed by the function are invalidated.
All other iterators, pointers and references keep their validity.
Data races
The container is modified.
The elements removed are modified. Iterating ranges in the container is not safe although concurrently accessing other elements is safe.
Exception safety
This function does not throw exception.
If an invalid range or position is specified, it causes undefined behavior.
Example 1
Let's see the simple example to erase the element by the iterator.
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
multimap<char,int>::iterator it;
mymultimap= {
{'a', 100},
{'b', 200},
{'b', 300},
{'c', 400}
};
cout<<"Before erasing the element: \n";
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
it=mymultimap.find('b');
mymultimap.erase (it); // erasing by iterator
cout<<"\nAfter erasing the element: \n";
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
Output:
Before erasing the element:
a => 100
b => 200
b => 300
c => 400
After erasing the element:
a => 100
b => 300
c => 400
In the above example, element is erased by the iterator it.
Example 2
Let's see a simple example to erase the element of the multimap with the given key value:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
multimap<char,int>::iterator it;
mymultimap = {
{'a', 100},
{'b', 200},
{'b', 300},
{'c', 400}
};
cout<<"Before erasing the element: \n";
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
mymultimap.erase ('b'); // erasing by key
cout<<"\nAfter erasing the element: \n";
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
Output:
Before erasing the element:
a => 100
b => 200
b => 300
c => 400
After erasing the element:
a => 100
c => 400
In the above example, erase(key) function erases the all the key 'b' and its value from the multimap.
Example 3
Let's see a simple example to erase the element by the given range:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
multimap<char,int>::iterator it;
mymultimap = {
{'a', 100},
{'b', 200},
{'b', 300},
{'c', 400}
};
cout<<"Before erasing the element are: \n";
cout<<"Size is: "<<mymultimap.size()<<'\n';
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
mymultimap.erase ( mymultimap.begin () , mymultimap.end () ); // erasing by range
cout<<"\nAfter erasing the element are: \n";
cout<<"Size is: "<<mymultimap.size();
for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
return 0;
}
Output:
Before erasing the element are:
Size is: 4
a => 100
b => 200
b => 300
c => 400
After erasing the element are:
Size is: 0
In the above example, erase (first, last) function is used to erase the elements with the given range i.e. begin to end.
Example 4
Let's see a simple example to erase all the odd numbers from the multimap:
#include <map>
#include <iostream>
using namespace std;
int main()
{
multimap<int, string> m = { {1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"}
};
// erase all odd numbers from m
cout<<"After erasing odd numbers,elements are:\n ";
for(auto it = m.begin(); it != m.end(); )
if(it->first % 2 == 1)
it = m.erase(it);
else
++it;
for(auto& p : m)
cout <<p.first <<", "<< p.second << "\n ";
}
Output:
After erasing odd numbers, elements are:
2, two
4, four
6, six
In the above example, all the odd numbers has been erased and displaying even numbers.