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

Multiset Clear Function

BLUF: Mastering Multiset 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: Multiset Clear Function

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

C++ multiset clear

The clear method in C++ for multimultisets is employed to eliminate all elements from the container. This action resets the multimultiset, reducing its size to zero.

Syntax

Example

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

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

Parameter

Return value

Complexity

Linear in size.

Iterator validity

All pointers, references, and iterators 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 cardinality of a multiset before and after executing the clear operation:

Example

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

int main() {

   multiset<int> mymultiset = {10,20,20,30}; 

   cout << "Initial size of multiset before clear operation = " << mymultiset.size() << endl;

   mymultiset.clear();

   cout << "Size of multiset after clear operation = " << mymultiset.size() << endl;

   return 0;
}

Output:

Output

Initial size of multiset before clear operation = 4
Size of multiset after clear operation = 0

After initializing the multiset with four elements in the example above, the size is set to 4. 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 multiset:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<string> mymultiset;

  mymultiset = {"Nikita","Deep","Aman"};

  cout << "mymultiset contains:\n";
  for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';

  mymultiset.clear();
  
  mymultiset= {"Deep", "Raaz"};

  cout << "\nmymultiset contains:\n";
  for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';

  return 0;
}

Output:

Output

mymultiset contains:
Aman
Deep
Nikita

mymultiset contains:
Deep
Raaz

After emptying the multiset as shown above, new elements can be added without the need for initialization.

Example 3

Let's examine a basic illustration to clarify the components of the multiset:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  int n;
  multiset<string> m1,m2,m3;
  
  m1 = {"Hello", "World"};
  m2 = {"Java", "Program"};
  m3 = {"C++", "Coding"};

  cout << "m1 group has following members:\n";
  for (multiset<string>::iterator it=m1.begin(); it!=m1.end(); ++it)
    cout << *it << ' ';
  

  cout << "\n\nm2 group has following members:\n";
  for (multiset<string>::iterator it=m2.begin(); it!=m2.end(); ++it)
    cout << *it<< ' ';

  cout << "\n\nm3 group has following members:\n";
  for (multiset<string>::iterator it=m3.begin(); it!=m3.end(); ++it)
    cout << *it<< ' ';
  
  cout<<"\n\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:
Hello World 

m2 group has following members:
Java Program 

m3 group has following members:
C++ Coding 

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 sets of multiset elements, and based on the user's selection, one of these sets is removed.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main() {
    
   int n;
   
   multiset<string> fruit = {"Banana","Apple","Orange","Banana"};

   cout << "Fruit bucket has following fruits = \n";
   for (multiset<string>::iterator it=fruit.begin(); it!=fruit.end(); ++it)
    cout << *it<< '\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
Banana
Banana
Orange

Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 1
0 fruits in bucket 

2. 
Fruit bucket has following fruits = 
Apple 
Banana 
Banana
Orange

Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 1
4 fruits in bucket

In the given scenario, a fruit multiset is created with three different types of fruits. To clear the multiset, inputting 0 will result in the fruit bucket containing 4 elements, while entering 1 will clear the fruit multiset 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