C++ Standard Library C++ STL Library

C++ set - erase() Function



The C++ set::erase function is used to delete either a single element or a range of elements from the set. It reduces the size of the set by number of elements deleted from the container.

Syntax

//Version 1 - Delete element at position
void erase (iterator position);

//Version 2 - Delete val from the set
size_type erase (const value_type& val);

//Version 3 - Delete a range of elements in [first, last)
void erase (iterator first, iterator last);
//Version 1 - Delete element at position
iterator  erase (const_iterator position);

//Version 2 - Delete val from the set
size_type erase (const value_type& val);

//Version 3 - Delete a range of elements in [first, last)
iterator  erase (const_iterator first, const_iterator last);

Parameters

position Iterator specifying position of the element in the set to be removed.
val Value to be removed from the set.
first Iterator specifying position of the first element of the range in the set. Elements in [first, last) position range will be deleted.
last Iterator specifying position of the last element of the range in the set. Elements in [first, last) position range will be deleted.

Return Value

For version 2: the function returns number of elements erased.
C++ 98: For other versions, the function returns nothing.
C++ 11: For other versions, the function returns an iterator pointed to the element that follows the last element removed. If the last element is removed, then the iterator will point to the set::end.

Time Complexity

For version 1: Constant.
For version 2: Logarithmic in container size.
For version 3: Linear in the distance between first and last..

Example:

In the example below, the set::erase function is used to delete a single element from MySet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> MySet{10, 20, 30, 40, 50};
  set<int>::iterator it;

  cout<<"MySet contain: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  //version 1: deletes element at position = 2
  it = MySet.begin();
  it++;
  MySet.erase(it);

  //version 2: deletes 40 from the set
  MySet.erase(40);

  cout<<"\nMySet contain: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MySet contain: 10 20 30 40 50 
MySet contain: 10 30 50 

Example:

A range of elements can also be deleted from the set. Consider the example below:

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> MySet{10, 20, 30, 40, 50};
  set<int>::iterator start_it;
  set<int>::iterator stop_it;
  set<int>::iterator it;

  cout<<"MySet contain: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  //setting the start position at 
  //third element of the set
  start_it = MySet.begin();
  start_it++;
  start_it++;

  //setting the stop position at end of the set
  stop_it = MySet.end();

  //version 3: erase a range of elements
  MySet.erase(start_it, stop_it);

  cout<<"\nMySet contain: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MySet contain: 10 20 30 40 50 
MySet contain: 10 20 

❮ C++ <set> Library