The clear method in C++ for multimap is employed to eliminate all the elements stored in the multimap container. This action results in an empty multimap with a size of 0.
Syntax
void clear(); //until C++ 11
void clear() noexcept; //since C++ 11
Parameter
Return value
Complexity
Linear in size.
Iterator validity
All iterators, references, and pointers associated with this container become invalid.
Data Races
The container is modified.
All contained elements are modified.
Exception safety
This function never throws exception.
Example 1
Let's explore a basic example to determine the size of a multimap both before and after performing the clear operation:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<char, int> mymultimap = {
{'a', 1},
{'b', 2},
{'b', 3},
{'d', 4},
{'d', 5},
};
cout << "Initial size of multimap before clear operation = " << mymultimap.size() << endl;
mymultimap.clear();
cout << "Size of multimap after clear operation = " << mymultimap.size() << endl;
return 0;
}
Output:
Initial size of multimap before clear operation = 5
Size of multimap after clear operation = 0
After populating the multimap with 5 elements as shown in the example, its size is initially set to 5. However, following the clear operation, the size is reset to 0.
Example 2
Let's examine a straightforward illustration to clarify the components of the multimap:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<int,string> mymultimap;
mymultimap = {
{1, "Nikita"},
{2, "Divya" },
{1, "Ashish"} };
cout << "mymultimap contains:\n";
for (multimap<int,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " : " << it->second << '\n';
mymultimap.clear();
mymultimap = {
{1, "Aman"},
{2, "Deep"}
};
cout << "\nmymultimap contains:\n";
for (multimap<int,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " : " << it->second << '\n';
return 0;
}
Output:
mymultimap contains:
1 : Nikita
1 : Ashish
2 : Divya
mymultimap contains:
1 : Aman
2 : Deep
After emptying the multimap as shown in the example above, new elements can be inserted without the need for reinitialization.
Example 3
Let's examine a basic illustration to clarify the components of the multimap:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
int n;
multimap<int,string> m1,m2,m3;
m1 = { {1, "Nikita"},
{2, "Deep"},
{1, "Ashish"} };
m2 = { {1, "Nidhi"},
{2, "Priya"},
{2, "Gitanjali"} };
m3 = { {1, "Manas"},
{3, "Fruti"},
{3, "Kamlesh"} };
cout << "m1 group has following members:\n";
for (multimap<int,string>::iterator it=m1.begin(); it!=m1.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout << "m2 group has following members:\n";
for (multimap<int,string>::iterator it=m2.begin(); it!=m2.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout << "m3 group has following members:\n";
for (multimap<int,string>::iterator it=m3.begin(); it!=m3.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout<<"\nWhich group do you want to delete?\n 1.m1\n 2.m2\n 3.m3\n Please enter your choice: ";
cin>>n;
if(n==1){
m1.clear();
cout<<"\nGroup m1 has been cleared.";
}
else if(n==2){
m2.clear();
cout<<"\nGroup m2 has been cleared.";
}
else if(n==3){
m3.clear();
cout<<"\nGroup m3 has been cleared.";
}
else
cout<<"Invalid option!";
return 0;
}
Output:
m1 group has following members:
1 : Nikita
1 : Ashish
2 : Deep
m2 group has following members:
1 : Nidhi
2 : Priya
2 : Gitanjali
m3 group has following members:
1 : Manas
3 : Fruti
3 : Kamlesh
Which group do you want to delete?
1.m1
2.m2
3.m3
Please enter your choice: 2
Group m2 has been cleared.
In the previous example, there are three sets of multimap, and based on the user's selection, one set has been removed.
Example 4
Let's see a simple example:
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
multimap<string, int> fruit = {
{"Banana", 40},
{"Apple", 190},
{"Orange", 120},
};
cout << "Fruit bucket has following fruits = \n";
for (multimap<string,int>::iterator it=fruit.begin(); it!=fruit.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout<<"\nDo you want to clear your fruit bucket?\nPress 1 for Yes and 0 for No: ";
cin>>n;
if( n==1){
fruit.clear();
cout<<fruit.size()<<" fruits in bucket \n";
}
else if(n==0)
cout <<fruit.size() << " fruits in bucket \n" ;
return 0;
}
Output:
1.
Fruit bucket has following fruits =
Apple : 190
Banana : 40
Orange : 120
Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 0
3 fruits in bucket
2.
Fruit bucket has following fruits =
Apple : 190
Banana : 40
Orange : 120
Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 1
0 fruits in bucket
In the earlier instance, a fruit multimap is set up with three different types of fruits. To clear the multimap, if you input 0, the fruit collection will contain 3 elements. On the other hand, entering 1 will result in the fruit multimap being emptied, reducing its size to 0.