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:
- Using insert() function
- 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++
#include <iostream>
#include <set> // for set operations
using namespace std;
int main()
{
set< int > st;
set< int >::iterator it = st.begin();
set< int >::iterator it1, it2;
pair<set< int >::iterator, bool > ptr;
ptr = st.insert(20);
if (ptr.second)
cout << "The element was newly inserted" ;
else
cout << "The element was already present" ;
cout << "\nThe set elements after 1st insertion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
st.insert(it, 24);
cout << "\nThe set elements after 2nd insertion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
int arr[3] = { 25, 24, 26 };
st.insert(arr, arr + 3);
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++
#include <iostream>
#include <set> // for set operations
using namespace std;
int main()
{
set< int > st;
set< int >::iterator it = st.begin();
set< int >::iterator it1, it2;
pair<set< int >::iterator, bool > ptr;
ptr = st.emplace(24);
if (ptr.second)
cout << "The element was newly inserted" ;
else
cout << "The element was already present" ;
cout << "\nThe set elements after 1st insertion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
ptr = st.emplace(24);
if (ptr.second)
cout << "\nThe element was newly inserted" ;
else
cout << "\nThe element was already present" ;
cout << "\nThe set elements after 2nd insertion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
st.emplace_hint(it, 25);
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++
#include <iostream>
#include <set> // for set operations
using namespace std;
int main()
{
set< int > st;
set< int >::iterator it;
set< int >::iterator it1;
set< int >::iterator it2;
pair<set< int >::iterator, bool > ptr;
for ( int i = 1; i < 10; i++)
st.insert(i * 5);
cout << "The set elements after insertion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
it = st.begin();
cout << endl;
++it;
st.erase(it);
cout << "The set elements after 1st deletion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
st.erase(40);
cout << "\nThe set elements after 2nd deletion are : " ;
for (it1 = st.begin(); it1 != st.end(); ++it1)
cout << *it1 << " " ;
++it;
++it;
++it;
++it;
st.erase(it, st.end());
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)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Mar, 2023
Like Article
Save Article