multimap::clear() method in C++ STL

Multimaps are associative containers available in the C++ Standard Library. They are similar to a map with an addition that multiple elements can have the same keys. The multimap::clear() is a method available in the STL that removes all the elements from a multimap.

Syntax:

map_name.clear();

Parameters: The multimap::clear() does not accept any parameter:
Return value: none

Example of multimap::clear() method

#include<iostream>
#include<map>


using namespace std;

void print(multimap<char,int> m)
{
    multimap<char,int>::iterator it;

    for(it = m.begin(); it != m.end(); ++it)
    {
        cout<< it->first <<" : "<<it->second<<endl;
    }

    cout<<endl;
}

int main()
{
    multimap<char,int> m;

    m.insert( pair<int,int> ('a',1) );
    m.insert( pair<int,int> ('b',2) );
    m.insert( pair<int,int> ('c',3) );

    print(m);

    m.clear();
    cout<<"Size after clear: "<<m.size();

}

Output:

a : 1
b : 2
c : 3

Size after clear: 0

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top