C++ Unordered_map::clear() Function



The C++ unordered_map::clear() function is used to destroy or drop the unordered_map containers by removing all elements and sets the size of unordered_map to zero.

Even after removing all the elements from the unordered_map, we can add the new element to the same unordered_map using the [] operator. for example; unordered_map[1] = "tutorialspoint";

Syntax

Following is the syntax of unordered_map::clear() function.

unordered_map.clear();

Parameters

This function does not accepts any parameter.

Return value

This function does not returns anything because the size of unordered_map after using the clear() function is zero.

Example 1

In the following example, let's demonstrate the uses of the clear() function, as follows:

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'d', 4},
      {'e', 5}
   };
   cout << "Initial size of unordered map = " << um.size() << endl;
   um.clear();
   cout << "Size of unordered map after clear operation = " << um.size() << endl;
   return 0;
}

Output

Output of the above code is as follows −

Initial size of unordered map = 5
Size of unordered map after clear operation = 0

Example 2

Following is the example, where we are using the clear() function to clear the unordered_map and later we are adding two elements to the same unordered_map, as follows:

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
   unordered_map<string,string> Umap = { {"Aman","Kumar"}, {"Vivek","Verma"}, {"Jhon","Satya"} };

   cout << "mymap contains:";
   for (auto& x: Umap)cout << " " << x.first << "=" << x.second;
   cout << endl;
    
   //removing all the element from the map.
   Umap.clear();
    
   //adding two element from the map
   Umap["TutorialsPoint"]="Tutorix";
   Umap["Location"]="Hyderabad";
   cout << "mymap contains:";
   for (auto& x: Umap) cout << " " << x.first << "=" << x.second;
      cout << endl;
   return 0;
}

Output

On executing we get the following output −

mymap contains: Jhon=Satya Vivek=Verma Aman=Kumar
mymap contains: Location=Hyderabad TutorialsPoint=Tutorix

Example 3

Let's look at the following example, where we are displaying the unordered_map element before the use of the clear() function, and after that, we are displaying the size of the unordered_map, as follows:

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
   unordered_map<int, string> Umap;
   Umap[1] = "tutorialspoint";
   Umap[2] = "Hyderabad India";
   Umap[3] = "Tutorix";
   Umap[4] = "Noida India";
   cout<<"Before use of clear() functon";
   for (auto& x: Umap) cout << "\n" << x.first << "=" << x.second;
      cout << endl;
    
   Umap.clear();
   cout<<"After use of clear() function \n";
   cout <<Umap.size();
   return 0;
}

Output

Following is the output of the above code −

Before use of clear() functon
4=Noida India
3=Tutorix
2=Hyderabad India
1=tutorialspoint
After use of clear() function 
0
Advertisements