Open In App

multiset::erase() in C++ STL

Last Updated : 25 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::multiset::erase() is a built-in STL function used to remove elements from the multiset container. It is member function of std::multiset class defined inside <multiset> header file. In this article, we will learn about std::multiset::erase() in C++.

The multiset::erase() function provides 3 different implementations:

m.erase(i); // For single element
m.erase(val); // For all elements with value val
m.erase(first, last) // For multiple elements

Erase a Single Element

We can erase a single element from a multiset using multiset::erase() function. We need to pass the iterator to the element which we want to remove.

Syntax

m.erase(itr);

Parameters

  • itr: Iterator to the element to remove.

Return Value

  • Returns an iterator point to the element just after the removed element.

Example

C++
// C++ Program to erase a single element using
// multiset::erase() with iterator
#include <bits/stdc++.h>
using namespace std;
int main() {
    multiset<int> m = {9, 11, 11, 13, 45};

    // Iterator to 4th element
    auto it = next(m.begin(), 3);

    // Remove the 4th element i.e. 13
    m.erase(it);

    for (auto i : m)
        cout << i << " ";
    return 0;
}

Output
9 11 11 45 

Time Complexity: O(log n), where n is the number of elements in multiset.
Auxiliary Space: O(1)

Erase All Occurrences of a Value

We can also remove all occurrences of a value from multiset using multiset::erase() function. We have to pass the value which is to be deleted.

Syntax

m.erase(val);

Parameters

  • val: Value which we have to erase.

Return Value

  • Returns the number of elements removed.

Example

C++
// C++ Program to erase all occurences of a
// value using std::multiset::erase()
#include <bits/stdc++.h>
using namespace std;
int main() {
    multiset<int> m = {9, 11, 11, 13, 45};

    // Remove all occurence of element 11 with
    m.erase(11);

    for (auto i : m)
        cout << i << " ";
    return 0;
}

Output
9 13 45 

Time Complexity: O(k + log n), where n is the number of elements and k is the occurrences of deleted element.
Auxiliary Space: O(1)

Erase Multiple Elements in a Range

We can remove multiple continuous elements of the multiset using multiset::erase() function.

Syntax

m.erase(first, last)

Parameters

  • first: Iterator to the first element of range.
  • last: Iterator to the element just after the last element of range.

Return Value

  • Returns an iterator which point to the element that is just after the last element of the range.

Example

C++
// C++ Program to erase multiple elements
// using multiset::erase() function
#include <bits/stdc++.h>
using namespace std;
int main() {
    multiset<int> m = {9, 11, 11, 13, 45};

    // Specify the range
    auto first = m.begin();
    auto last = next(m.begin(), 3);

    // Erase the specified range
    m.erase(first, last);

    for (auto i : m)
        cout << i << " ";
    return 0;
}

Output
13 45 

Time Complexity: O(k + log n), where n is the number of elements and k is the number of elements in the range.
Auxiliary Space: O(1)


Next Article

Similar Reads