C++ Standard Library C++ STL Library

C++ multiset - clear() Function



The C++ multiset::clear function is used to clear all elements of the multiset. This function makes the multiset empty with a size of zero.

Syntax

void clear();
void clear() noexcept;

Parameters

No parameter is required.

Return Value

None.

Time Complexity

Linear i.e, Θ(n)

Example:

In the example below, the multiset::clear function is used to clear all elements of the multiset called MyMSet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MyMSet{10, 20, 30, 40, 50};
  multiset<int>::iterator it;

  cout<<"Before clear() function: \nThe multiset contains:";
  for(it = MyMSet.begin(); it != MyMSet.end(); ++it)
    cout<<" "<<*it;
  cout<<"\nMultiset size is: "<<MyMSet.size()<<"\n\n";

  MyMSet.clear();

  cout<<"After clear() function: \nThe multiset contains:";
  for(it = MyMSet.begin(); it != MyMSet.end(); ++it)
    cout<<" "<<*it;
  cout<<"\nMultiset size is: "<<MyMSet.size();
  return 0;
}

The output of the above code will be:

Before clear() function: 
The multiset contains: 10 20 30 40 50
Multiset size is: 5

After clear() function: 
The multiset contains:
Multiset size is: 0

❮ C++ <set> Library