Open In App

set::erase in C++ STL

Last Updated : 18 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the std::set::erase() is a built-in member function of std::set container that is used to remove the element(s) from the container. In this article, we will learn how we can use set::erase() function in our C++ program.

The set::erase() can be used in different ways to erase element(s) from the set.

Remove a Given Element

The set::erase() function can be used for removing the element with given value from the set container.

Syntax

set_name.erase(val);

Parameters

  • val: The value which we have to delete.

Return Value

  • It returns the number of elements which is deleted from the set container. In case of set, it will always be 1 or 0.

Example: Removing the Given Element Using set::erase()

C++
// C++ program to illustrate how to use set::erase
// to remove an element wfrom the set
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> st = {10, 20, 30, 40};

    // Remove the element with value 20
    st.erase(20);

    for (auto i : st)
        cout << i << ' ';
    return 0;
}

Output
10 30 40 

Time Complexity: O(log n)
Auxiliary Space: O(1)

Remove an Element from the Given Position

set::erase() can also delete an element from the set using its position instead of value.

Syntax

set_name.erase(pos);

Parameters

  • pos: The position at which the element we have to delete.

Return Value

  • This version returns the iterator to the next element after the removed element.

Example: Removing the Element at the Given Position using set::erase()

C++
// C++ program to illustrate how to use set::erase
// to remove an element from the set using its position
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> st = {10, 20, 30, 40};

    // Find the iterator pointing to the second element
    auto it = next(st.begin(), 1);

    // Remove the element at the second position
    st.erase(it);

    for (auto i : st)
        cout << i << ' ';
    return 0;
}

Output
10 30 40 

Time Complexity: O(1)
Auxiliary Space: O(1)

Remove the Range of Elements

set::erase() function is also capable of deleting multiple elements from the set container. We can define a range [first, last) inside the map and delete all the elements in this range using the following syntax:

Syntax

set_name.erase(first, last)

Parameters

  • first: Iterator to the starting position of a range.
  • last: Iterator to the position just after the last element in the range.

Return Value

  • This version returns the iterator to the next element after the removed element.

Example: Removing Multiple Elements From Map using set::erase()

C++
// C++ program to illustrate how to use set::erase
// to remove multiple elements from the set using a range
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> st = {10, 20, 25, 28, 29, 30, 40};

    // Define the range to erase second to fifth elements
    auto first = next(st.begin(), 1);
    auto last = next(st.begin(), 5);

    // Remove all elements in the range [first, last)
    st.erase(first, last);

    for (auto i : st)
        cout << i << ' ';
    return 0;
}

Output
10 30 40 

Time Complexity: O(n), where n is the number of elements in the range to be deleted.
Auxiliary Space: O(1)
 


Next Article
Article Tags :
Practice Tags :

Similar Reads