multimap clear() function in C++ STL

In this article, we will be discussing the working, syntax, and examples of multimap::clear() function in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::clear()?

multimap::clear() function is an inbuilt function in C++ STL, which is defined in <map> header file. clear() is used to remove all the content from the associated multimap container. This function removes all the values and makes the size of the container 0.




This function accepts no parameter.

Return value

This function returns nothing


multimap<char, int > newmap;
newmap.insert(make_pair(‘a’, 1));
newmap.insert(make_pair(‘b’, 2));
newmap.insert(make_pair(‘c’, 3));


size of the multimap is: 0


 Live Demo

using namespace std;
int main(){
   multimap<int,int > mul_1;
   //inserting elements to multimap1
   cout << "Multimap size before using clear function : ";
   cout <<mul_1.size() << '\n';
   cout << "Multimap size after using clear function : ";
   cout << mul_1.size() << '\n';


If we run the above code it will generate the following output −

Multimap size before using clear function : 5
Multimap size after using clear function : 0