A Set in C++ is a data structure that bears resemblance to the mathematical set concept. It distinguishes itself from other containers by exclusively accommodating unique elements that can be iterated over in a sorted manner. Proficiency in working with sets is crucial for excelling in competitive programming and resolving algorithmic challenges. This tutorial will delve into the insertion and deletion operations pertaining to sets in C++.
Set Insertion
We are able to add elements to the set container by employing two member functions of std::set:
- Utilizing the insert method
- Leveraging the emplace method
1. Insertion using the insert Function
Syntax: 1
set_name.insert(elements);
The insert function is employed to add elements to the set. Upon insertion, the elements undergo reordering, resulting in a sorted set. This method can be invoked in three distinct manners.
This function appends a new item to the collection. The addition takes place only if the item provided is not already present in the collection. It returns a pair of pointers. The initial pointer pertains to the existing or newly added items. The subsequent pointer indicates the boolean outcome as either "true" or "false".
Syntax: 2
set_name.insert(position, elements);
The pointer's position is specified along with the element to be added in this particular setup. A guide in hincpp aids insert in identifying the insertion point, thereby aiming to reduce the element allocation time. The hincpp guide does not mandate a specific insertion spot. Upon completion, this function provides a pointer indicating the insertion location of the element.
Syntax: 3
set_name.insert(begining_iterator, ending_iterator);
This method of insertion is necessary for incorporating elements from different containers into the set. In case duplicate elements are present in the source container, they will not be included.
Example:
Let's consider an illustration to showcase the insertion of a set using the insert method 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 method is additionally utilized to add the component to the Set data structure. This method bears resemblance to the previously discussed "insert" function, with the sole contrast being that the element is constructed directly at the precise point of insertion, contrary to insert, which duplicates pre-existing objects.
Syntax of emplace:
set_name.emplace(element);
This function increments the overall count of elements within the collection by one. It provides a result containing a pair of pointers, where the first one is an iterator pointing to the newly added element, and the second one is a variable with a boolean value indicating whether the element already existed or was newly created.
Syntax emplace_hint:
set_name.emplace(hint_position, element);
Iterates through a "hint_iterator" to obtain a clue about the insertion point, potentially reducing the time required for inserting the element. This operation has no impact on the insertion position and is confined to its local definition.
Example:
Let's consider an instance to showcase the addition of elements using the emplace method 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
By utilizing the erase method, elements can be deleted from a set container. This function belongs to the std::set class and serves various purposes such as:
Syntax 1:
set_name.erase(number);
Delete the specified value from the argument and then rearrange the set after removal.
Syntax 2:
set_name.erase(iterator);
Deletes the value at the index specified by the iterator in its argument.
Syntax 3:
set_name.erase(begining_iterator, ending_iterator);
Deletes the elements between the "beginingiterator" and the "endingiterator".
Example:
Let's consider an illustration to showcase the removal of a set element using the erase method 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