Map Clear Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Map Clear Function

Map Clear Function

BLUF: Mastering Map Clear Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Map Clear Function

C++ is renowned for its efficiency. Learn how Map Clear Function enables low-level control and high-performance computing in the tutorial below.

The clear function in C++ for maps is employed to eliminate all elements from the map container. This operation effectively empties the map and resets its size to 0.

Syntax

Example

void clear(); //until C++ 11
void clear() noexcept; //since C++ 11

Parameter

Return value

Example 1

Let's examine a straightforward example to determine the size of a map before and after executing the clear operation.

Example

#include <iostream>
#include <map>

using namespace std;

int main() {

   map<char, int> mymap = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Initial size of map before clear operation = " << mymap.size() << endl;

   mymap.clear();

   cout << "Size of map after clear opearation = " << mymap.size() << endl;

   return 0;
}

Output:

Output

Initial size of map before clear operation = 5
Size of map after clear opearation = 0

After initializing the map with 5 elements, the map's size is 5. However, following the clear operation, the size resets to 0.

Example 2

Let's examine a basic example to understand the components of the map clearly.

Example

#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<int,string> mymap;

  mymap[1] = "Nikita";
  mymap[2] = "Deep";
  mymap[3] = "Ashish";

  cout << "mymap contains:\n";
  for (map<int,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  mymap.clear();
  
  mymap[4] = "Rajni";
  mymap[5] = "Sunil";

  cout << "\nmymap contains:\n";
  for (map<int,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " : " << it->second << '\n';

  return 0;
}

Output:

Output

mymap contains:
1 : Nikita
2 : Deep
3 : Ashish

mymap contains:
4 : Rajni
5 : Sunil

After resetting the map, it is possible to insert additional elements without needing to initialize them again.

Example 3

Let's explore a basic illustration to understand the components of the map.

Example

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  int n;
  map<int,string> m1,m2,m3;

  m1[1] = "Nikita";
  m1[2] = "Deep";
  m1[3] = "Ashish";
  
  m2[1] = "Nidhi";
  m2[2] = "Priya";
  m2[3] = "Gitanjali";
   m3[1] = "Rakesh";
  m3[2] = "Fruti";
  m3[3] = "Kamlesh";
  cout << "m1 group has following members:\n";
  for (map<int,string>::iterator it=m1.begin(); it!=m1.end(); ++it)
  cout << it->first << " : " << it->second << '\n';
  cout << "m2 group has following members:\n";
  for (map<int,string>::iterator it=m2.begin(); it!=m2.end(); ++it)
  cout << it->first << " : " << it->second << '\n';
  cout << "m3 group has following members:\n";
  for (map<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:

Output

m1 group has following members:
1 : Nikita
2 : Deep
3 : Ashish
m2 group has following members:
1 : Nidhi
2 : Priya
3 : Gitanjali
m3 group has following members:
1 : Rakesh
2 : 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 scenario described, there are three categories within a map, and one of these categories has been removed based on the user's selection.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <map>
 using namespace std;
 int main() {
 int n;
 map<string, int> fruit = {
            {"Banana", 40},
            {"Apple", 190},
            {"Orange", 120},
            };
 cout << "Fruit bucket has following fruits = \n";
 for (map<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:

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 previous example, a fruit map is initialized with three different types of fruits. To clear the map, entering 0 will result in the fruit bucket containing 3 elements, while entering 1 will clear the fruit map entirely, reducing its size to 0.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience