A Set is a container built in C++ with a concept comparable to the set described in mathematics. The set differs from other containers in that it includes only distinct elements, which may be traversed in sorted order. A good understanding of sets is important in competitive programming and overcoming algorithmic issues. In this article, we will discuss the set insertion and deletion in C++.
Set Insertion
We can insert elements into the set container by using two member methods of std::set :
- Making use of the insert function
- Making use of the emplace function
1. Insertion using the insert Function
Syntax: 1
set_name.insert(elements);
The insert method is used to insert the set's components. After insertion, the elements are reordered, and the set is sorted. This function can be executed in three different ways.
This function adds an element to the set. The insertion occurs when the element passed is not already in the set. It gives back a pointer pair . The first element refers to elements that already exist or have been added. The second element yields "true" or "false" as the boolean result.
Syntax: 2
set_name.insert(position, elements);
The indication of the pointer is given along with the element to be inserted in this implementation. A hincpp tutorialer assists insert in determining where the insertion should occur. As a result, trying to shorten the time required to allocate the element. The hincpp tutorialer doesn't require insertion at a specified location. This function returns the place where the element was inserted as a pointer.
Syntax: 3
set_name.insert(begining_iterator, ending_iterator);
This insertion type must be used to add elements from other containers to the set. If the repeated components have been found in the source container, they are not added.
Example:
Let's take an example to demonstrate the set insertion using the insert function in C++:
// code for implementation of insert function in set
#include <iostream>
#include <set> // for performing set operations
using namespace std;
int main()
{
//set initialisation
set<int> sst;
// iterators declaration
set<int>::iterator ite = sst.begin();
set<int>::iterator ite1, ite2;
//declaring a pair for the set's return value
// Initialize iterator and bool
pair<set<int>::iterator, bool> pt;
// insert() function for inserting element
pt = sst.insert(30);
// checking if the element was already present or newly
// inserted
if (pt.second)
cout << "Newly inserted element";
else
cout << "The element was inserted already";
// display of set elements after insertion
cout << "\nThe elements of the set after the first insertion are ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
//inserting the elements using hint
sst.insert(ite, 34);
// display of set elements after insertion
cout << "\nThe elements of the set after the second insertion are: ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
//elements inserting
// the element 34 is not inserted
int array[3] = { 25, 34, 26 };
sst.insert(array, array + 3);
// display of set elements after insertion
cout << "\nThe elements of the set after the third insertion are: ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
}
Output:
Newly inserted element
The elements of the set after the first insertion are 30
The elements of the set after the second insertion are: 30 34
The elements of the set after the third insertion are: 25 26 30 34
2. Making use of the emplace function
The emplace function also serves to insert the component into the Set. This function is similar to "insert" explained above, with the only difference becoming that the element is constructed "in-place" at the exact location of element insertion, as opposed to insert , which copies objects that already exist.
Syntax of emplace:
set_name.emplace(element);
This method increases the total number of elements of the set by one. It returns an object with a pointer pair, the initial one of which is an iterator pointing to the current location of the inserted element and the second of which is a variable with a boolean value indicating if the element is already existent or freshly generated.
Syntax emplace_hint:
set_name.emplace(hint_position, element);
Iterates over a "hint_iterator" to get a hint of the insertion point, possibly decreasing the time necessary to insert the inserted element. It does not affect the insertion position. It occurs where it is defined locally.
Example:
Let's take an example to demonstrate the set insertion using the emplace function in C++:
// C++ code to demonstrate the working of emplace()
// and emplace_hint()
#include <iostream>
#include <set> // for set operations
using namespace std;
int main()
{
// set initialisation
set<int> sst;
// iterators declaration
set<int>::iterator ite = sst.begin();
set<int>::iterator ite1, ite2;
//declaring a pair for the set's return value
// Initialize iterator and bool
pair<set<int>::iterator, bool> pt;
// insert using emplace() function for inserting element
pt = sst.emplace(25);
//checking if the element was already present or newly
// inserted
if (pt.second)
cout << "Newly inserted element";
else
cout << "The element was inserted already";
// display of set elements after insertion
cout << "\nThe elements of the set after the first insertion are : ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
//inserting the element 25 // not inserted
pt = sst.emplace(25);
// determining if the element was previously present
// or newly inserted returns false. already present
if (pt.second)
cout << "\nNewly inserted element";
else
cout << "\The element was inserted already";
// display of set elements after insertion
cout << "\nThe elements of the set after the second insertion are : ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
//inserting the elements using hint
sst.emplace_hint(ite, 29);
// display of set elements after insertion
cout << "\nThe elements of the set after the third insertion are: ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
}
Output:
Newly inserted element
The elements of the set after the first insertion are: 25
The element was inserted already
The elements of the set after the second insertion are: 25
The elements of the set after the third insertion are: 25 29
Set Deletion
Using the erase function , we can remove elements from a set container. It is a function in the std::set class. It has the following applications:
Syntax 1:
set_name.erase(number);
Removes the value specified in the argument. After deletion, reorder the set.
Syntax 2:
set_name.erase(iterator);
Removes the value from the position indicated by the iterator in its parameter.
Syntax 3:
set_name.erase(begining_iterator, ending_iterator);
Removes the elements from "beginingiterator" to "endingiterator".
Example:
Let's take an example to demonstrate the set deletion using the erase function in C++:
Filename: Deletion. cpp
// code for implementation of deletion in the set using erase() method
#include <iostream>
#include <set> // for set operations
using namespace std;
int main()
{
// set declaration
set<int> sst;
// declaration of the iterators
set<int>::iterator ite;
set<int>::iterator ite1;
set<int>::iterator ite2;
// the return value declaration of the set
pair<set<int>::iterator, bool> pt;
// inserting values in the set
for (int i = 1; i < 10; i++)
sst.insert(i * 2);
// display of initial elements in the set
cout << "The elements of the set after the insertion are: ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
ite = sst.begin();
cout << endl;
// deleting element with an iterator
// removes the second element, i.e., 4
++ite;
sst.erase(ite);
//display of elements after the deletion
cout << "The elements of the set after the first deletion are :";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
// erasing by value
sst.erase(40);
//display of elements after the deletion
cout << "\nThe elements of the set after the second deletion are :";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
++ite;
++ite;
++ite;
++ite;
// by using the range
sst.erase(ite, sst.end());
// display of elements after the 3rd deletion
cout << "\nThe elements of the set after the third deletion are :";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";
cout << endl;
}
Output:
The elements of the set after the insertion are: 2 4 6 8 10 12 14 16 18
The elements of the set after the first deletion are: 6 8 10 12 14 16 18
The elements of the set after the second deletion are:2 6 8 10 12 14 16 18
The elements of the set after the third deletion are:2 6 8