The unordered_map::insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the unordered_map container. This function increases container size by 1. This function does not insert duplicate entries. There are following variant of this function. All are overloaded functions.
Syntax-1:
iterator unordered_map_name.insert({key, element})
Parameters : This function takes two arguments as input parameters. key and its value to be inserted.
Return type : The function returns an iterator pointing to the new element in the container.
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map< int , int > ump;
ump.insert({ 20, 130 });
ump.insert({ 100, 410 });
ump.insert({ 31, 60 });
cout << "KEY\tELEMENT\n" ;
for ( auto itr = ump.begin(); itr != ump.end(); itr++) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output:
KEY ELEMENT
31 60
20 130
100 410
Syntax-2:
iterator unordered_map_name.insert(iterator position, {key, element})
This function insert element in unordered_map after at specified position.
Parameters : The parameters key and elements are same as in function of type 1 but position is from where searching operation is performed for insertion of element into the container.
Return value The function returns an iterator pointing to the new element in the container.
Below program illustrate above syntax clearly.
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map< char , int > ump;
ump.insert({ 'a' , 1 });
ump.insert({ 'b' , 2 });
auto it = ump.find( 'a' );
ump.insert(it, { 'c' , 3 });
cout << "KEY\tELEMENT\n" ;
for ( auto itr = ump.begin(); itr != ump.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output:
KEY ELEMENT
c 3
a 1
b 2
Syntax-3:
iterator unordered_map_name.insert(iterator position1, iterator position2)
Parameters : This function accepts two parameters position1 and position2 which specifies the range all elements between this range are inserted into another container including element at position1 but excluding element at position2.
Return value The function returns an iterator pointing to the new element in the container.
Below program illustrate above syntax clearly.
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map< int , int > ump, ump1;
ump.insert({ 2, 20 });
ump.insert({ 1, 10 });
ump.insert({ 3, 30 });
ump1.insert(ump.begin(), ump.end());
cout << "Elements in ump1 are\n" ;
cout << "KEY\tELEMENT\n" ;
for ( auto itr = ump1.begin(); itr != ump1.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output:
Elements in ump1 are
KEY ELEMENT
1 10
2 20
3 30
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 :
19 Dec, 2018
Like Article
Save Article