Delete an element or a range from a set in C++ STL

Updated

Delete an element from the Set by passing its value :

Approach :

In this method, an element is deleted by using the erase function and calling it with the value of that element as its argument. Syntax :

size_type erase (const value_type& valueOfElementToBeDeleted);

Below is the implementation of the above approach: Program 2:

// C++ program to delete an element
// of a Set by passing its value

#include <iostream>
#include <set>
using namespace std;

// Function to print the set
void printSet(set<int> myset)
{

    // Get the iterator
    set<int>::iterator it;

    // printing all the elements of the set
    for (it = myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';
}

// Function to delete the element of set
void deleteByValue (set<int> myset, int valueOfElementToBeDeleted)
{

    // printing all the elements of the set
    cout << "\nSet originally: ";
    printSet(myset);

    // Erase the element that is equal to
    // the value passed as the parameter
    myset.erase(valueOfElementToBeDeleted);

    // printing all the elements of the set
    cout << "Set after deletion: ";
    printSet(myset);
}

// Driver code
int main()
{
    set<int> myset;

    // Get the set
    for (int i = 1; i < 10; i++)
        myset.insert(i * 10);

    // Get the valueOfElementToBeDeleted
    int valueOfElementToBeDeleted = 50;

    // Delete an element from the Set
    deleteByValue (myset, valueOfElementToBeDeleted);

    return 0;
}

Output :

Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 60 70 80 90

Delete a range of values from the Set using Iterator :

Approach :

In this method, a range of elements are deleted from the set. This is done with the help of two iterators. The first iterator points at the starting element of the range and the second iterator points at the last element of the range. The first iterator is inclusive while the last iterator is exclusive which means that the element pointed by the last iterator will not get deleted. Syntax :

iterator erase (const_iterator startPositionIterator_inclusive, 
                const_iterator endingPositionIterator_exclusive);

Below is the implementation of the above approach: Program:

// C++ program to delete an element
// of a Set by passing its value

#include <iostream>
#include <set>
using namespace std;

// Function to print the set
void printSet(set<int> myset)
{

    // Get the iterator
    set<int>::iterator it;

    // printing all the elements of the set
    for (it = myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';
}

// Function to delete the element of set
void deleteRange (set<int>& myset,
             set<int>::iterator start_itr,
             set<int>::iterator end_itr)
{

    // printing all the elements of the set
    cout << "\nSet originally: ";
    printSet(myset);

    // Erase the elements in the range
    // of the iterators passed as the parameter
    myset.erase(start_itr, end_itr);

    // printing all the elements of the set
    cout << "Set after deletion: ";
    printSet(myset);
}

// Driver code
int main()
{
    set<int> myset;

    // Get the set
    for (int i = 1; i < 10; i++)
        myset.insert(i * 10);

    // Get the starting Iterator at 3rd element
    set<int>::iterator start_itr = ++(++myset.begin());

    // Get the ending Iterator at 2nd last element
    set<int>::iterator end_itr = --(--myset.end());

    // Delete an element from the Set
    deleteRange (myset, start_itr, end_itr);

    return 0;
}

Output :

Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 80 90
Comment
Next tutorial
Mithlesh Upadhyay Published 6 Nov 2026 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials