C++ Standard Library C++ STL Library

C++ unordered_multiset - clear() Function



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

Syntax

void clear() noexcept;

Parameters

No parameter is required.

Return Value

None.

Time Complexity

Linear i.e, Θ(n)

Example:

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

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

  cout<<"Before clear() function: \nThe unordered_multiset contains:";
  for(it = uMSet.begin(); it != uMSet.end(); ++it)
    cout<<" "<<*it;
  cout<<"\nUnordered Multiset size is: "<<uMSet.size()<<"\n\n";

  uMSet.clear();

  cout<<"After clear() function: \nThe unordered_multiset contains:";
  for(it = uMSet.begin(); it != uMSet.end(); ++it)
    cout<<" "<<*it;
  cout<<"\nUnordered Multiset size is: "<<uMSet.size();
  return 0;
}

The output of the above code will be:

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

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

❮ C++ <unordered_set> Library