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.

Syntax

Map_name.clear();

Parameter

This function accepts no parameter.

Return value

This function returns nothing

Input 

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

Output 

size of the multimap is: 0

Example

 Live Demo

#include<iostream>
#include<map&g;
using namespace std;
int main(){
   multimap<int,int > mul_1;
   //inserting elements to multimap1
   mul_1.insert({1,10});
   mul_1.insert({2,20});
   mul_1.insert({3,30});
   mul_1.insert({4,40});
   mul_1.insert({5,50});
   cout << "Multimap size before using clear function : ";
   cout <<mul_1.size() << '\n';
   mul_1.clear();
   cout << "Multimap size after using clear function : ";
   cout << mul_1.size() << '\n';
}

Output

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

Updated on: 22-Apr-2020

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements