multiset clear() function in C++ STL


In this article we will be discussing the working, syntax and examples of multiset::clear() function in C++ STL.

What is a multiset in C++ STL?

Multisets are the containers similar to the set container, meaning they store the values in the form of keys the same like a set, in a specific order.

In multiset the values are identified as keys as same like sets. The main difference between multiset and set is that the set has distinct keys, meaning no two keys are the same, in multiset there can be the same keys value.

Multiset keys are used to implement binary search trees.

What is multiset::clear()?

multiset::clear() function is an inbuilt function in C++ STL, which is defined in <set> header file.

This is used to clear the whole multiset container.

clear() removes all the elements from the elements of the multiset container and makes the size of the multiset container as 0.

Syntax

ms_name.clear();

Parameters

The function accepts no parameter.

Return value

This function returns nothing

Example

Input: std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
mymultiset.clear();
mymultiset.size();
Output: size of multiset = 0

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {2, 4, 1, 3, 8, 5, 6};
   multiset<int> check(arr, arr + 7);
   cout<<"List is : ";
   for (auto i = check.begin(); i != check.end(); i++)
   cout << *i << " ";
   cout<<"\nList when clear() is applied: ";
   check.clear();
   for (auto i = check.begin(); i != check.end(); i++)
   cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

List is : 1 2 3 4 5 6 8
List when clear() is applied:

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {2, 4, 1, 3, 8, 5, 6};
   multiset<int> check(arr, arr + 7);
   cout<<"List is : ";
   for (auto i = check.begin(); i != check.end(); i++)
   cout << *i << " ";
   cout<<"\nList when clear() is applied: ";
   if(check.empty()) {
      cout<<"\nList is null";
   } else {
      cout<<"\nList isn't null : ";
      for (auto i = check.begin(); i != check.end(); i++)
      cout << *i << " ";
      cout<<"\nsize is : "<<check.size();
   }
   int arr2[] = {2, 4, 1, 3, 8, 5, 6};
   multiset<int> check_2(arr2, arr2 + 7);
   cout<<"\nList when clear() is applied: ";
   check_2.clear();
   if(check_2.empty()) {
      cout<<"\nList is null";
      cout<<"\nsize is : "<<check_2.size();
   } else {
      cout<<"\nList isn't null : "<<check_2.size();
      for (auto i = check_2.begin(); i != check_2.end(); i++)
      cout << *i << " ";
      cout<<"\nsize is : "<<check_2.size();
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

List is : 1 2 3 4 5 6 8
List when clear() is applied:
List isn't null : 1 2 3 4 5 6 8
Size is : 7
List when clear() is applied:
List is null
size is : 0

Updated on: 23-Mar-2020

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements