Last Updated : 14 Dec, 2018
unordered_multimap::erase() is a built in function in C++ STL which removes the element from given range, by position and by key. There are three variant of this function in C++ STL. There are following type of erase() functions in C++ for unordered_multimap.
- iterator erase ( iterator position )
- size erase ( key_type& k )
- iterator erase ( iterator first, iterator last );
Below programs explains about above functions.
Example 1 CPP
// C++ program to illustrate the
// unordered_multimap::erase() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of unordered_multimap
unordered_multimap<char, int> sample;
// inserts element
sample.insert({ 'a', 2 });
sample.insert({ 'b', 4 });
sample.insert({ 'c', 8 });
sample.insert({ 'd', 10 });
sample.insert({ 'c', 4 });
sample.insert({ 'e', 4 });
sample.insert({ 'f', 4 });
cout << " Elements of multimap are : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
// delete element by position
sample.erase(sample.begin());
// print after delete by position
cout << " Elements of multimap after deleting by position are : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
// erase by Element
sample.erase('c');
// print after delete by element
cout << " Elements of multimap after deleting by element name : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
// erase by range
sample.erase(sample.find('e'), sample.end());
// print after delete by range
cout << " Elements of multimap after deleting by range are : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
return 0;
}
Output:
Elements of multimap are : f : 4 b : 4 a : 2 c : 4 c : 8 d : 10 e : 4 Elements of multimap after deleting by position are : b : 4 a : 2 c : 4 c : 8 d : 10 e : 4 Elements of multimap after deleting by element name : b : 4 a : 2 d : 10 e : 4 Elements of multimap after deleting by range are : b : 4 a : 2 d : 10Example 2 CPP
// C++ program to illustrate the
// unordered_multimap::erase() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of unordered_multimap
unordered_multimap<int, int> sample;
// inserts element
sample.insert({ 1, 2 });
sample.insert({ 2, 4 });
sample.insert({ 3, 8 });
sample.insert({ 4, 10 });
sample.insert({ 3, 4 });
sample.insert({ 5, 4 });
sample.insert({ 6, 4 });
cout << " Elements of multimap are : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
// delete element by position
sample.erase(sample.begin());
// print after delete by position
cout << " Elements of multimap after deleting by position are : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
// erase by Element
sample.erase(3);
// print after delete by element
cout << " Elements of multimap after deleting by element name : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
// erase by range
sample.erase(sample.find(5), sample.end());
// print after delete by range
cout << " Elements of multimap after deleting by range are : \n";
for (auto& x : sample)
cout << x.first << " : " << x.second << endl;
return 0;
}
Output:
Elements of multimap are : 6 : 4 2 : 4 1 : 2 3 : 4 3 : 8 4 : 10 5 : 4 Elements of multimap after deleting by position are : 2 : 4 1 : 2 3 : 4 3 : 8 4 : 10 5 : 4 Elements of multimap after deleting by element name : 2 : 4 1 : 2 4 : 10 5 : 4 Elements of multimap after deleting by range are : 2 : 4 1 : 2 4 : 10
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4