Insertion and Deletion in STL Set C++

4

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

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

  • insert(ele) : This function inserts the element in set. The insertion only takes place when the element passed is not already in set. It returns a pointer pair . First element pointing to the element already present or newly inserted. Second element returning the boolean status “true” or “false”.
  • insert(hint, ele) : In this implementation, the hint pointer is sent with the element to be inserted. The use of hint pointer is to help insert() know where the actual insertion has to take place. Hence, trying to reduce time to allocate the element. Hint pointer does not force the insertion at specific position. This function returns the pointer to the position where element is inserted.
  • insert(beg_ptr, end_ptr) : This type of insertion is required to insert the elements of other container into set. The repeated elements are not inserted if they are present in the source container.
// 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 

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

  • emplace() : Inserts element using in-place construction strategy. Increases the size of set by 1. returns a pointer pair. 1st element of which is iterator pointing to the position of inserted element. 2nd returns a boolean variable indicating an already present or newly created element.
  • emplace_hint() : Takes a “hint_iterator” to get a hint of position of insertion to possibly reduce the time required to insert the element inserted. This does not effect the position of insertion. It takes place where it is defined to internally.
// 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 

Deletion

    Using erase() : erase() is used to erase the element in set mentioned in argument, either its position, its value or a range of number.

  • erase(num) : Erases the value mentioned in its argument. reorders the set after deletion.
  • erase(iter) : Erases the value at the position pointed by the iterator mentioned in its argument.
  • erase(strt_iter,end_iter) : Erases the range of elements starting from “strt_iter” to the “end_iter”.
// 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 

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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



4 Average Difficulty : 4/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.