unordered_multimap erase in C++ STL Last Updated : 14 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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. By position : It removes element from unordered_multimap by given position and return an iterator pointing to the position immediately following the last of the elements erased. By key : It removes elements by the key. It returns the number of elements which has been erased. By range : It takes iterator first and last and removes all elements between them including first but excluding last.It return an iterator pointing to the position immediately following the last of the elements erased. Syntax: 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 : 10 Example 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 Comment More infoAdvertise with us Next Article unordered_multimap empty() function in C++ STL A ankit15697 Follow Improve Article Tags : C++ cpp-unordered_multimap Practice Tags : CPP Similar Reads Unordered Multimap in C++ STL In C++, the unordered_multimap is an unordered associative container that stores data in the form of key-value pairs. It is similar to unordered map, but it allows multiple elements with the same key. It provides fast insertion, deletion and search operations in O(1) time by using hashing.Example:C+ 7 min read unordered_multimap begin() and end() function in C++ STL The unordered_multimap::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the container or to the first element in one of its buckets. Syntax: unordered_multimap_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, 5 min read unordered_multimap size() function in C++ STL The unordered_multimap::size() is a built-in function in C++ STL which returns the size of the unordered_multimap. It denotes the number of elements in that container. Syntax: unordered_multimap_name.size() Parameters: The function does not accept any parameters. Return Value: It returns an integral 3 min read unordered_multimap empty() function in C++ STL The unordered_multimap::empty() is a built-in function in C++ STL which returns a boolean value. It returns true if the unordered_multimap container is empty. Otherwise, it returns false. Syntax: unordered_multimap_name.empty() Parameters: The function does not accept any parameter. Return Value: It 2 min read unordered_multimap insert() in C++ STL The function std::unordered_multimap::insert() is a built-in function in C++ STL that extends container by inserting new element in unordered_multimap. This function increases container size by one. The insert() function can be used to insert a single key-value pair, a complete unordered_map, initia 2 min read unordered_multimap emplace() function in C++ STL The unordered_multimap::emplace() is a built-in function in C++ STL which inserts a new {key, element} in the unordered_multimap container. The insertion is done automatically at the position according to the container's criterion. It increases the size of the container by one. Syntax: unordered_mul 2 min read unordered_multimap find() function in C++ STL The unordered_multimap::find() is a built-in function in C++ STL which returns an iterator which points to one of the elements which has the key k. If the container does not contain any element with key k, then it returns an iterator which points to the position which is past the last element in the 3 min read unordered_multimap count() function in C++ STL The unordered_multimap::count() is a built-in function in C++ STL that returns the number of elements in the container whose key is equal to the key passed in the parameter. Syntax: unordered_multimap_name.count(key) Parameters: The function accepts a single mandatory parameter key that specifies th 2 min read unordered_multimap clear() function in C++ STL The unordered_multimap::clear() is a built-in function in C++ STL which clears the contents of the unordered_multimap container. The final size of the container after the call of the function is 0. Syntax: unordered_multimap_name.clear() Parameters: The function does not accept any parameter. Return 2 min read unordered_multimap swap() function in C++ STL The unordered_multimap::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multimap containers. The sizes can differ of both the containers. Syntax: unordered_multimap_name1.swap(unordered_multimap_name2) Parameters: The function accepts a single mandatory parameter u 3 min read Like