unordered_multiset erase() function in C++ STL

The unordered_multiset::erase() function is a built-in function in C++ STL which is used to remove either a single element or, all elements with a definite value or, a range of elements ranging from start(inclusive) to end(exclusive). This decreases the size of the container by the number of elements removed.

Syntax:

  1. unordered_multiset_name.erase(iterator position)
  2. unordered_multiset_name.erase(iterator start, iterator end)
  3. unordered_multiset_name.erase(key_value)

Parameters: This function has three versions.



  1. The first one takes an iterator as an argument, erases the element present at that position.
  2. The second version takes two iterator(say start and end) takes 2 iterators as argument and erases all elements in the range [start, end).
  3. The third version takes a key value as argument and erases all elements of that value in the multiset.

Return Value: The 1st and 2nd version of the function as shown in the above syntax returns an iterator immediately following the last element erased. The 3rd version returns the number of element erased.

Below programs illustrate the unordered_multiset::erase() function:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multiset::erase() function
  
#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
    unordered_multiset<int> samplemultiSet;
  
    // Inserting elements
    samplemultiSet.insert(10);
    samplemultiSet.insert(5);
    samplemultiSet.insert(15);
    samplemultiSet.insert(20);
    samplemultiSet.insert(25);
    samplemultiSet.insert(10);
    samplemultiSet.insert(15);
    samplemultiSet.insert(20);
  
    // Erases a particular element by its position
    samplemultiSet.erase(samplemultiSet.begin());
  
    // Displaying the set after removal
    for (auto it = samplemultiSet.begin(); 
                    it != samplemultiSet.end(); it++) 
    {
        cout << *it << " ";
    }
  
    // erases a range of elements, 
    // here all the elements
    samplemultiSet.erase(samplemultiSet.begin(), 
                              samplemultiSet.end());
  
    cout << "\nMultiSet size: " << samplemultiSet.size();
  
    return 0;
}

chevron_right


Output:

10 10 15 15 20 20 25 
MultiSet size: 0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multiset::erase() function
  
#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
    unordered_multiset<int> samplemultiSet;
  
    // Inserting elements
    samplemultiSet.insert(10);
    samplemultiSet.insert(5);
    samplemultiSet.insert(15);
    samplemultiSet.insert(20);
    samplemultiSet.insert(25);
    samplemultiSet.insert(10);
    samplemultiSet.insert(15);
    samplemultiSet.insert(20);
  
    // Erases all elements of value 10
    samplemultiSet.erase(10);
  
    // Displaying the set after removal
    for (auto it = samplemultiSet.begin(); 
                it != samplemultiSet.end(); it++) 
    {
        cout << *it << " ";
    }
  
    return 0;
}

chevron_right


Output:

5 15 15 20 20 25


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.