multimap::erase() in C++ STL Last Updated : 18 Nov, 2020 Comments Improve Suggest changes 13 Likes Like Report multimap::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range. Syntax for erasing a key: multimap_name.erase(key) Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the multimap container. Return Value: The function does not return anything. It erases all the elements with the specified key. CPP // C++ program to illustrate // multimap::erase(key) #include <bits/stdc++.h> using namespace std; int main() { // initialize container multimap<int, int> mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 2, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "The multimap before using erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.begin(); itr != mp.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // function to erase given keys mp.erase(1); mp.erase(2); // prints the elements cout << "\nThe multimap after applying erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } Output: The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 5 50 3 60 Syntax for removing a position: multimap_name.erase(iterator position) Parameters: The function accept one mandatory parameter position which specifies the iterator that is the reference to the position of the element to be erased. Return Value: The function does not returns anything. Program below illustrate the above syntax: CPP // C++ program to illustrate // multimap::erase(position) #include <bits/stdc++.h> using namespace std; int main() { // initialize container multimap<int, int> mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 2, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "The multimap before using erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.begin(); itr != mp.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // function to erase given position auto it = mp.find(2); mp.erase(it); auto it1 = mp.find(5); mp.erase(it1); // prints the elements cout << "\nThe multimap after applying erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } Output: The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 3 60 2 20 1 40 Syntax for erasing a given range: multimap_name.erase(iterator position1, iterator position2) Parameters: The function accepts two mandatory parameters which are described below: position1 - specifies the iterator that is the reference to the element from which removal is to be done. position2 - specifies the iterator that is the reference to the element upto which removal is to be done. Return Value: The function does not returns anything. It removes all the elements in the given range of iterators. Program below illustrate the above syntax: CPP // C++ program to illustrate // multimap::erase() #include <bits/stdc++.h> using namespace std; int main() { // initialize container multimap<int, int> mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 2, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "The multimap before using erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.begin(); itr != mp.end(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } // function to erase in a given range // find() returns the iterator reference to // the position where the element is auto it1 = mp.find(2); auto it2 = mp.find(5); mp.erase(it1, it2); // prints the elements cout << "\nThe multimap after applying erase() is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } Output: The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 5 50 1 40 Create Quiz Comment G gopaldave Follow 13 Improve G gopaldave Follow 13 Improve Article Tags : Misc C++ STL CPP-Functions cpp-multimap +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like