multiset clear() function in C++ STL

The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multimap container after removal is 0.

Syntax:

multiset_name.clear()

Parameters: The function does not accept any parameter.

Return Value: The function does not returns anything.

Below programs illustrates the multimap::clear() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// multiset::clear() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    int arr[] = { 15, 10, 15, 11, 10 };
  
    // initializes the set from an array
    multiset<int> s(arr, arr + 5);
  
    // prints all elements in set
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
  
    cout << "\nThe size after clear() is: ";
  
    // erases all elements
    s.clear();
    cout << s.size();
  
    return 0;
}

chevron_right


Output:

The elements in multiset are: 10 10 11 15 15 
The size after clear() is: 0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// multiset::clear() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
  
    // initializes the set from an array
    multiset<int> s(arr, arr + 9);
  
    // prints all elements in set
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
  
    cout << "\nThe size after clear() is: ";
  
    // erases all elements
    s.clear();
    cout << s.size();
  
    return 0;
}

chevron_right


Output:

The elements in multiset are: 10 10 11 15 15 18 18 20 20 
The size after clear() is: 0


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.