Deletion in STL Set

We can delete elements from a set container using erase() function. It is a member function of std::set class. It can be used in the following ways:

Syntax 1:

set_name.erase(value);

Erases the value mentioned in its argument. reorders the set after deletion.

Syntax 2:

set_name.erase(iterator);

Erases the value at the position pointed by the iterator mentioned in its argument.

Syntax 3:

set_name.erase(begin_iterator, end_iterator);

Erases the range of elements starting from “begin_iterator” to the “end_iterator”.

Example:

C++




// C++ code to demonstrate the working of erase()
#include <iostream>
#include <set> // for set operations
using namespace std;
  
int main()
{
    // declaring set
    set<int> st;
  
    // declaring iterators
    set<int>::iterator it;
    set<int>::iterator it1;
    set<int>::iterator it2;
  
    // declaring pair for return value of set containing
    // set iterator and bool
    pair<set<int>::iterator, bool> ptr;
  
    // inserting values in set
    for (int i = 1; i < 10; i++)
        st.insert(i * 5);
  
    // printing initial set elements
    cout << "The set elements after insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    it = st.begin();
  
    cout << endl;
  
    // erasing element using iterator
    // erases 2nd element i.e., 10
    ++it;
    st.erase(it);
  
    // printing set elements after deletion
    cout << "The set elements after 1st deletion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    // erasing element using value
    st.erase(40);
  
    // printing set elements after deletion
    cout << "\nThe set elements after 2nd deletion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    ++it;
    ++it;
    ++it;
    ++it;
  
    // erasing element using range iterator
    // deletes 25 - last(45)
    st.erase(it, st.end());
  
    // printing set elements 3rd deletion
    cout << "\nThe set elements after 3rd deletion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    cout << endl;
}


Output

The set elements after insertion are : 5 10 15 20 25 30 35 40 45 
The set elements after 1st deletion are : 5 15 20 25 30 35 40 45 
The set elements after 2nd deletion are : 5 15 20 25 30 35 45 
The set elements after 3rd deletion are : 5 15 20 

Time Complexity of Deletion in Set: O(logN)



C++ STL Set Insertion and Deletion

Prerequisite: Set

A Set is a container implemented in C++ language in STL and has a concept similar to how the set is defined in mathematics. The fact that separates the set from the other containers is that it contains only the distinct elements and elements can be traversed in sorted order. Having the stronghold on sets is useful in competitive programming and solving algorithmic problems. The insertion and deletion in STL sets are discussed in this article.

Similar Reads

Insertion in STL Set

We can insert elements in an STL set container using two member functions of std::set:...

Deletion in STL Set

...