C++ unordered_set::insert() Function
The C++ std::unordered_set::insert() function is used to insert the elements into the unordered_set and extend the container size by one. This function does not insert duplicate elements, and it only inserts an element if the unordered_set container does not contain equivalent element to the specified element.
This function has 4 polymorphic variants: with using the hint, value, range, and ilist (you can find the syntaxes of all the variants below).
Syntax
Following is the syntax of std::unordered_set::insert() function.
pair<iterator,bool> insert (const value_type& val); or iterator insert (const_iterator hint, const value_type& val); or void insert (InputIterator first, InputIterator last); or void insert (initializer_list<value_type> il);
Parameters
- hint − It specify the hint for the position to insert the element.
- val − It indicates the value that we want to insert in the unordered_set
- first, last − It specify the range to insert the elements from the iterator first to the last.
- ilist − It specify the initializer_list object.
Return Value
The function returns an iterator pointing to the newly inserted element.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_set::insert() function.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(void) {
unordered_set<char> myUset = {'a', 'b'};
auto ins = myUset.insert('d');
cout<<"'d' was inserted: "<< boolalpha << ins.second << '\n';
cout<<" After insertion unordered_set contains"<<endl;
for (auto it: myUset)
cout<<" "<< it<<endl;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
'd' was inserted: true After insertion unordered_set contains d b a
Example 2
Consider the following example, where we are going to insert a value at the beginning of the current unordered_set.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(void) {
unordered_set<char> myUset = {'a', 'b'};
auto it = myUset.begin();
myUset.insert(it, 'd');
cout<<" After insertion unordered_set contains"<<endl;
for (auto it: myUset)
cout<<" "<< it<<endl;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
After insertion unordered_set contains d b a
Example 3
Inserting elements in the range −
Following is the another example of the usage of insert() function to insert the array element into the unordered_set.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(void) {
unordered_set<int> myUset = {1, 2};
array<int, 4> a = {10, 11, 12, 13};
myUset.insert (a.begin (), a.end ()); // insert range
cout<<" After insertion unordered_set contains"<<endl;
for (auto it: myUset)
cout<< it<<" ";
return 0;
}
Output
If we run the above code it will generate the following output −
After insertion unordered_set contains 13 12 11 10 2 1
Example 4
In the following example, we are going to consider the two unordered_set one is filled with elements and another one is empty and performing the insert() function.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(void) {
unordered_set<int> uSet = {1, 2, 3, 5};
//declare an empty unordered set
unordered_set<int> myUset;
//inserting element into the empty map from the first map
myUset.insert(uSet.begin(), uSet.end());
cout << "myUset contains following elements" << endl;
for (auto it: myUset)
cout <<it<<" ";
return 0;
}
Output
Following is the output of the above code −
myUset contains following elements 1 2 3 5
Example 5
Following is the example, where we are going to consider the empty unordered-set and inserting the elements using the iterator.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(void) {
//declare an empty unordered set
unordered_set<int> myUset;
myUset.insert ({20, 21, 22, 23}); // insert initializer_list
cout << "myUset contains following elements after insertion: " << endl;
for (auto it: myUset)
cout <<it<<" ";
return 0;
}
Output
Output of the above code is as follows −
myUset contains following elements after insertion: 23 22 21 20