Insertion and Deletion in STL Set C++

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

Insertion
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 << " ";
  
}
chevron_right

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 

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 << " ";
}
chevron_right

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 

Deletion
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

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 

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





Improved By : nidhi_biet

Article Tags :
C++
Practice Tags :