The erase method in C++ for multimap is employed to delete either a specific element linked to a provided key value or a series of elements from the multimap container. Consequently, the size of the multimap will decrease by the count of removed elements.
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 indicating a specific element to be deleted from the multimap.
k : Key representing the element that will be deleted from the multimap.
first : Beginning of the range to erase.
last : End of the range to erase.
Return value
It returns an iterator pointing to the next element after the deleted element or provides the count of deleted elements.
Complexity
erase(position) : Amortized constant.
erase (val) : Logarithmic in container size.
Delete(first, last): The time complexity is proportional to the distance between the first and last elements being removed.
Iterator validity
Iterators, references, and pointers pointing to elements affected by the function become invalid.
All remaining iterators, pointers, and references maintain their validity.
Data races
The container is modified.
The removed components have been altered. It is considered unsafe to iterate over ranges within the container, but accessing other elements concurrently is considered safe.
Exception safety
This function does not throw exception.
When specifying an incorrect range or position, it can lead to undefined behavior.
Example 1
Let's examine a basic example of removing an element using an 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 previous instance, the term "element" is replaced by the iterator "it".
Example 2
Let's examine a basic example of removing the entry from the multimap based on the specified 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 provided example, the erase(key) method removes all occurrences of the key 'b' along with its corresponding value from the multimap.
Example 3
Let's examine a basic example demonstrating how to delete an element within a specified 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 previous example, the erase (first, last) function is employed to remove elements within a specified range from beginning to end.
Example 4
Let's explore a basic illustration on removing all the uneven 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 aforementioned instance, all the odd integers have been removed and only even numbers are being shown.