The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at the position according to the container’s criterion (since it uses different hashing functions). This effectively increases the container size by the number of elements inserted.
Syntax:
unordered_set_name.insert (Value)
or,
unordered_set_name.insert (InputIterator first, InputIterator last)
Parameters:
- Value: It specifies the value which is to be inserted in the container.
- first, last: Iterators specifying a range of elements. Copies of the elements in the range [first, last) are inserted in the unordered_set container. Keep in mind that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Return Value: The function returns a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed.
Below are programs that illustrate the above function:
Time Complexity: insert() method takes O(1).
Program 1:
CPP
#include<iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string> mySet = { "first" , "third" };
string myString = "tenth" ;
mySet.insert(myString);
cout << "My set contains:"
<< endl;
for ( const string& x : mySet) {
cout << x
<< " " ;
}
cout << endl;
return 0;
}
|
Output
My set contains:
tenth first third
Program 2:
CPP
#include <array>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<std::string> mySet = { "first" ,
"third" , "second" };
array<std::string, 2> myArray = { "tenth" ,
"seventh" };
string myString = "ninth" ;
mySet.insert(myString);
mySet.insert(myArray.begin(), myArray.end());
mySet.insert({ "fourth" , "sixth" });
cout << "myset contains:"
<< endl;
for ( const string& x : mySet) {
cout << x
<< " " ;
}
cout << endl;
return 0;
}
|
Output
myset contains:
sixth fourth seventh first tenth second third ninth
Program 3:
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void display_elements(unordered_set< int > &u_set)
{
cout<< "the elements int the unordered set are: " ;
for ( auto it:u_set)
{
cout<<it << " " ;
}
cout<<endl;
}
int main() {
unordered_set< int > u_set;
cout<< "u_set.insert(1).second: " <<u_set.insert(1).second<<endl;
cout<< "*(u_set.insert(1).first): " <<*(u_set.insert(1).first)<<endl;
cout<< "u_set.insert(1).second: " <<u_set.insert(1).second<<endl;
cout<< "*(u_set.insert(1).first): " <<*(u_set.insert(1).first)<<endl;
cout<< "u_set.insert(2).second: " <<u_set.insert(2).second<<endl;
cout<< "*(u_set.insert(2).first): " <<*(u_set.insert(2).first)<<endl;
display_elements(u_set);
return 0;
}
|
Output
u_set.insert(1).second: 1
*(u_set.insert(1).first): 1
u_set.insert(1).second: 0
*(u_set.insert(1).first): 1
u_set.insert(2).second: 1
*(u_set.insert(2).first): 2
the elements int the unordered set are: 2 1
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 :
23 Jun, 2022
Like Article
Save Article