multiset erase() in C++ STL

Prerequisite : multiset

The multiset::erase() is the STL function in C++ removes the specified element from multiset.

There are three versions of this method. These are:

  1. Syntax:

    void erase (iterator position_of_iterator);
    

    Parameters: This method accepts following parameters:

    • position_of_iterator: It refers to the position of the specific element to be removed with the help of iterator.

    Return value: This method returns the iterator following the removed element.

    Below examples illustrate the working of multiset::erase() method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // multiset::erase() method
      
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // Initialise the multiset
        multiset<int> multi_set;
        multiset<int>::iterator ms_iterator;
      
        // Add values to the multiset
        for (int i = 1; i < 10; i++) {
            multi_set.insert(i);
        }
      
        cout << "Original multiset: ";
      
        for (ms_iterator = multi_set.begin();
             ms_iterator != multi_set.end();
             ++ms_iterator)
      
            cout
                << ' ' << *ms_iterator;
        cout << '\n';
      
        ms_iterator = multi_set.begin();
        ms_iterator++;
      
        // Passing the iterator for the position
        // at which the value is to be erased
        multi_set.erase(ms_iterator);
      
        cout << "Modified multiset: ";
      
        for (ms_iterator = multi_set.begin();
             ms_iterator != multi_set.end();
             ++ms_iterator)
      
            cout << ' ' << *ms_iterator;
        cout << '\n';
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Original multiset:  1 2 3 4 5 6 7 8 9
    Modified multiset:  1 3 4 5 6 7 8 9
    
  2. Syntax:

    size_type erase (const value_type& contant_value);
    

    Parameters: This method accepts following parameters:

    • constant_value: It refers to the specific element to be removed from the multiset with the help of its value. It must be constant. All instances of this value is erased by this method.

    Return value: This method returns the iterator following the removed element.

    Below examples illustrate the working of multiset::erase() method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // multiset::erase() method
      
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // Initialise the multiset
        multiset<int> multi_set;
      
        multiset<int>::iterator ms_iterator;
      
        // Add values to the multiset
        for (int i = 1; i < 10; i++) {
            multi_set.insert(i);
        }
      
        cout << "Original multiset: ";
      
        for (ms_iterator = multi_set.begin();
             ms_iterator != multi_set.end();
             ++ms_iterator)
            cout << ' ' << *ms_iterator;
        cout << '\n';
      
        ms_iterator = multi_set.begin();
      
        // Passing constant value to be erased
        multi_set.erase(2);
      
        cout << "Modified multiset: ";
      
        for (ms_iterator = multi_set.begin();
             ms_iterator != multi_set.end();
             ++ms_iterator)
            cout << ' ' << *ms_iterator;
        cout << '\n';
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Original multiset:  1 2 3 4 5 6 7 8 9
    Modified multiset:  1 3 4 5 6 7 8 9
    
  3. Syntax:

    void erase (iterator starting_iterator, iterator ending_iterator);
    

    Parameters: This method accepts following parameters:

    • starting_iterator: It refers to the starting iterator of the range of values to be removed from the multiset.
    • ending_iterator: It refers to the ending iterator of the range of values to be removed from the multiset.

    Return value: This method returns the number of removed elements.

    Below examples illustrate the working of multiset::erase() method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate
    // multiset::erase() method
      
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // Initialise the multiset
        multiset<int> multi_set;
        multiset<int>::iterator ms_iterator;
      
        // Add values to the multiset
        for (int i = 1; i < 10; i++) {
            multi_set.insert(i);
        }
      
        cout << "Original multiset: ";
      
        for (ms_iterator = multi_set.begin();
             ms_iterator != multi_set.end();
             ++ms_iterator)
      
            cout << ' ' << *ms_iterator;
        cout << '\n';
      
        ms_iterator = multi_set.begin();
        ms_iterator++;
        ms_iterator++;
      
        // Passing the iterator range for the positions
        // at which the values are to be erased
        multi_set.erase(ms_iterator, multi_set.end());
      
        cout << "Modified multiset: ";
      
        for (ms_iterator = multi_set.begin();
             ms_iterator != multi_set.end();
             ++ms_iterator)
      
            cout << ' ' << *ms_iterator;
        cout << '\n';
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Original multiset:  1 2 3 4 5 6 7 8 9
    Modified multiset:  1 2
    


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.