Insertion and Deletion in STL Set in C++



The STL set is a container that stores unique elements in a sorted order. Insertions and deletions are common operations performed in containers like std::set and std::vector. In this article, we will learn how to insert and delete elements in a C++ STL set.

Insertion in STL Set

To insert a new element into a std::set you can use the insert method or the emplace method. Let's understand how to use these methods with examples.

1. Using insert Method

The insert method is used to insert a new element into the set by copying or moving the object. A new element will be added only if it is not already present in the set.

// Create a set of integers
set<int> Set;

// Insert elements into the set
Set.insert(10);
Set.insert(20);
Set.insert(10); // Duplicate, will not be added

In the example below, we have created a set of pairs (integer and string) and inserted elements into it using the insert method.

#include <iostream>
#include <set>

using namespace std;

int main() {
    // Create a set of pairs
    set<pair<int, string>> mySet;

    // Insert elements into the set
    mySet.insert(make_pair(1, "Alice"));
    mySet.insert(make_pair(2, "Bob"));

    // Display the elements in the set
    cout << "Set elements:" << endl;
    for (const auto& p : mySet) {
        cout << p.first << ": " << p.second << endl;
    }

    return 0;
}

The output of the above code will be:

Set elements:
1: Alice
2: Bob

2. Using emplace Method

The emplace method is used to insert a new element into the set by constructing it in place. This helps to avoid unnecessary coping of the object to be inserted, and hence it is more efficient than the insert method.

// Create a set of integers
set<int> Set;

// Insert elements into the set using emplace
Set.emplace(10);
Set.emplace(20);
Set.emplace(10); // Duplicate, will not be added

In the example below, we will create a set of pairs (integer and string) and insert elements into it using the emplace method.

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

int main() {
    // Create a set of pairs
    set<pair<int, string>> mySet;

    // Insert elements into the set using emplace
    mySet.emplace(1, "Alice");
    mySet.emplace(2, "Bob");

    // Display the elements in the set
    cout << "Set elements:" << endl;
    for (const auto& p : mySet) {
        cout << p.first << ": " << p.second << endl;
    }

    return 0;
}

The output of the above code will be:

Set elements:
1: Alice
2: Bob
Note: When you try to add a duplicate element to a set, you will not get any error, the element just won't be added.

Deletion in STL Set

To delete an element from a std::set, you can use the erase method. The syntax for the erase method is as follows:

// Delete an element from the set for a specific value
set_name.erase(value);

// Delete an element from the set for an iterator
set_name.erase(iterator);

// Delete a range of elements from the set
set_name.erase(iterator1, iterator2);

Example

In the example code below, we will create a set of integers and then delete an element from it using the erase method.

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

int main() {
    // Create a set of integers
    set<int> mySet;

    // Insert elements into the set
    mySet.insert(10);
    mySet.insert(20);
    mySet.insert(30);

    // Display the elements in the set before deletion
    cout << "Set elements before deletion:" << endl;
    for (const auto& elem : mySet) {
        cout << elem << " ";
    }
    cout << endl;

    // Delete an element from the set
    mySet.erase(20);

    // Display the elements in the set after deletion
    cout << "Set elements after deletion:" << endl;
    for (const auto& elem : mySet) {
        cout << elem << " ";
    }
    cout << endl;

    return 0;
}

The output of the above code will be:

Set elements before deletion:
10 20 30
Set elements after deletion:
10 30
Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-06-04T18:48:48+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started