Set insert function in C++
In C++, the set data structure is a container that stores unique elements in a sorted order. The set data structure is part of the Standard Template Library (STL), and it provides a way to store elements in a sorted order without duplicates. The set container provides several member functions to insert and manipulate the elements. In this blog post, we will focus on the set insert function in C++.
The set insert function is used to insert a new element into the set. The syntax of the set insert function is as follows:
std::set<datatype>set_name;
set_name.insert(value);
In the above syntax, the first line declares a set container named set_name. The second line inserts a new element into the set. The value parameter is the element that needs to be inserted into the set. If the element is already present in the set, the insert function does not insert the element and returns an iterator to the existing element.
Example:
Let's consider an example to understand the set insert function in C++.
#include <iostream>
#include <set>
int main()
{
std::set<int> numbers;
// Inserting elements into the set
numbers.insert(10);
numbers.insert(20);
numbers.insert(30);
numbers.insert(40);
numbers.insert(50);
// Printing elements of the set
std::cout<< "Set elements are: ";
for (auto i : numbers)
std::cout<<i<< " ";
// Inserting a duplicate element into the set
auto it = numbers.insert(30);
// Printing elements of the set
std::cout<< "\nSet elements after inserting duplicate element: ";
for (auto i : numbers)
std::cout<<i<< " ";
// Checking if the insert function inserted the element or not
if (it.second == false)
std::cout<< "\nElement already present in the set at position " << distance(numbers.begin(), it.first) + 1;
else
std::cout<< "\nNew element inserted in the set at position " << distance(numbers.begin(), it.first) + 1;
return 0;
}
Output:
Set elements are: 10 20 30 40 50
Set elements after inserting duplicate element: 10 20 30 40 50
Element already present in the set at position 3
Explanation:
In the above example, we have created a set named numbers and inserted five elements into the set. After that, we have printed the elements of the set using a for loop. In the next step, we have tried to insert a duplicate element into the set using the insert function. As expected, the insert function did not insert the duplicate element and returned an iterator to the existing element. After that, we have printed the elements of the set after inserting the duplicate element. Finally, we have checked if the insert function has inserted the element or not and printed the position of the element in the set.
In the output, we can see that the set insert function did not insert the duplicate element, and it returned an iterator to the existing element. The iterator returned by the insert function is a pair of an iterator and a boolean value. The iterator points to the existing element, and the boolean value is true if the element is inserted into the set, and false otherwise.
Apart from the basic usage, the set insert function can be used in various scenarios. One such scenario is when we need to insert multiple elements into the set. In such cases, we can use the insert function in a loop to insert all the elements into the set.
For example, consider the following code:
std::set<int> numbers;
std::vector<int>vec = {1, 2, 3, 4, 5};
for (auto i : vec)
numbers.insert(i);
In the above code, we have created a set named numbers and a vector named vec. After that, we have used a for loop to insert all the elements of the vector into the set.
Another scenario is when we need to insert elements into the set in sorted order where the insert function is useful. By default, the set container stores the elements in sorted order. Therefore, when we insert elements into the set using the insert function, they are automatically sorted.
Example:
Let’s consider the following code in the above example:
std::set<int> numbers = {40, 20, 10, 50, 30};
numbers.insert(15);
Output:
Set elements are: 10 15 20 30 40 50
Set elements after inserting duplicate element: 10 15 20 30 40 50
Element already present in the set at position 2
Explanation:
In the above code, we have created a set named numbers and inserted five elements into the set. After that, we have inserted a new element 15 into the set. As expected, the new element is inserted into the set in a sorted order, and the final set contains {10, 15, 20, 30, 40, 50}.
Conclusion:
In this blog post, we have discussed the set insert function in C++. Here, we utilized the syntax and usage of the insert function with an example. The set insert function is a useful function for inserting elements into a set. It ensures that the elements are inserted in a sorted order without duplicates.