The clear() Method can be used to remove all the elements from the Map.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, string> x =
{
{ "Five", "Is a Number" },
{ "John", "Is a Name" },
{ "C++", "Is a Language"}
};
x.clear();
for (auto i : x)
{
cout << "The Key is : " << i.first << " and the Value is :" << i.second << endl;
}
return 0;
}
So, in the above code we have created a Map using braces {} and Key and Value pairs.
map<string, string> x =
{
{ "Five", "Is a Number" },
{ "John", "Is a Name" },
{ "C++", "Is a Language"}
};And initialised to the variable x.
-Method1.png)
And we have used the clear() method to remove all the elements from the Map.
x.clear()
And the print statement, prints an empty Map.