Open In App

C++ STL Set Insertion and Deletion

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Insertion in STL Set

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

  1. Using insert() function
  2. Using emplace() function

1. Insertion Using insert() Function

The insert() function is used to insert the elements in the set. After insertion, the reordering of elements takes place and the set is sorted. This function can be implemented in 3 ways.

Syntax 1:

set_name.insert(element);

This function inserts the element in the set. The insertion only takes place when the element passed is not already in the set. It returns a pointer pair. The first element points to the elements already present or newly inserted. The second element returns the boolean status “true” or “false”.

Syntax 2:

set_name.insert(hint_position, element);

In this implementation, the hint pointer is sent with the element to be inserted. The use of a hint pointer is to help insert() know where the actual insertion has to take place. Hence, trying to reduce the time to allocate the element. The hint pointer does not force the insertion at a specific position. This function returns the pointer to the position where the element is inserted.

Syntax 3:

set_name.insert(begin_iterator, end_iterator);

This type of insertion is required to insert the elements of other containers into the set. The repeated elements are not inserted if they are present in the source container.

Example:

C++




// C++ code to demonstrate the working of insert()
#include <iostream>
#include <set> // for set operations
using namespace std;
  
int main()
{
    // declaring set
    set<int> st;
  
    // declaring iterators
    set<int>::iterator it = st.begin();
    set<int>::iterator it1, it2;
  
    // declaring pair for return value of set containing
    // set iterator and bool
    pair<set<int>::iterator, bool> ptr;
  
    // using insert() to insert single element
    // inserting 20
    ptr = st.insert(20);
  
    // checking if the element was already present or newly
    // inserted
    if (ptr.second)
        cout << "The element was newly inserted";
    else
        cout << "The element was already present";
  
    // printing set elements after insertion
    cout << "\nThe set elements after 1st insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    // inserting set elements using hint
    st.insert(it, 24);
  
    // printing set elements after insertion
    cout << "\nThe set elements after 2nd insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    // inserting array elements in set
    // 24 is not inserted again
    int arr[3] = { 25, 24, 26 };
    st.insert(arr, arr + 3);
  
    // printing set elements after insertion
    cout << "\nThe set elements after 3rd insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
}


Output

The element was newly inserted
The set elements after 1st insertion are : 20 
The set elements after 2nd insertion are : 20 24 
The set elements after 3rd insertion are : 20 24 25 26 

2. Insertion Using emplace() Function

The emplace() is also used to insert the element into the Set. This function is similar to “insert()” discussed above, the only difference being that the “in-place” construction of the element takes place at the position of element insertion contrary to insert() which copies or movies existing objects.

Syntax of emplace():

set_name.emplace(element);

It increases the size of the set by 1 and returns a pointer pair whose first element of which is an iterator pointing to the position of the inserted element and the second returns a boolean variable indicating an already present or newly created element.

Syntax of emplace_hint():

set_name.emplace(hint_position, element);

Takes a “hint_iterator” to get a hint of the position of insertion to possibly reduce the time required to insert the element inserted. This does not affect the position of insertion. It takes place where it is defined internally.

Example:

C++




// C++ code to demonstrate the working of emplace()
// and emplace_hint()
#include <iostream>
#include <set> // for set operations
using namespace std;
  
int main()
{
    // declaring set
    set<int> st;
  
    // declaring iterators
    set<int>::iterator it = st.begin();
    set<int>::iterator it1, it2;
  
    // declaring pair for return value of set containing
    // set iterator and bool
    pair<set<int>::iterator, bool> ptr;
  
    // using emplace() to insert single element
    // inserting 24
    ptr = st.emplace(24);
  
    // checking if the element was already present or
    // newly inserted returns true. newly inserted
    if (ptr.second)
        cout << "The element was newly inserted";
    else
        cout << "The element was already present";
  
    // printing set elements after insertion
    cout << "\nThe set elements after 1st insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    // using emplace() to insert single element
    // inserting 24 // not inserted this time
    ptr = st.emplace(24);
  
    // checking if the element was already present or
    // newly inserted returns false. already inserted
    if (ptr.second)
        cout << "\nThe element was newly inserted";
    else
        cout << "\nThe element was already present";
  
    // printing set elements after insertion
    cout << "\nThe set elements after 2nd insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
  
    // inserting set elements using hint
    st.emplace_hint(it, 25);
  
    // printing set elements after insertion
    cout << "\nThe set elements after 3rd insertion are : ";
    for (it1 = st.begin(); it1 != st.end(); ++it1)
        cout << *it1 << " ";
}


Output

The element was newly inserted
The set elements after 1st insertion are : 24 
The element was already present
The set elements after 2nd insertion are : 24 
The set elements after 3rd insertion are : 24 25 

Time Complexity of Insertion in Set: O(logN)

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)



Last Updated : 09 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads