unordered_map insert in C++ STL - GeeksforGeeks

19 Dec, 2018
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.
// C++ program to illustrate// unordered_map::insert({key, element})#include <bits/stdc++.h>using namespace std;  int main(){    // initialize container    unordered_map<int, int> ump;      // insert elements in random order    ump.insert({ 20, 130 });    ump.insert({ 100, 410 });    ump.insert({ 31, 60 });      // prints the elements    cout << "KEY\tELEMENT\n";    for (auto itr = ump.begin(); itr != ump.end(); itr++) {        cout << itr->first             << '\t' << itr->second << '\n';    }    return 0;}
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.
// C++ program to illustrate// unordered_map::insert(iterator position, {key, element})#include <bits/stdc++.h>using namespace std;  int main(){      // initialize container    unordered_map<char, int> ump;      // insert elements in random order    ump.insert({ 'a', 1 });    ump.insert({ 'b', 2 });      auto it = ump.find('a');      // inserts {3, 6} starting the search from    // position where 2 is present    ump.insert(it, { 'c', 3 });      // prints the elements    cout << "KEY\tELEMENT\n";    for (auto itr = ump.begin(); itr != ump.end(); ++itr) {        cout << itr->first             << '\t' << itr->second << '\n';    }    return 0;}
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.
// C++ program to illustrate// unordered_map::insert(iterator position1, iterator position2)#include <bits/stdc++.h>using namespace std;  int main(){      // initialize container    unordered_map<int, int> ump, ump1;      // insert elements in random order    ump.insert({ 2, 20 });    ump.insert({ 1, 10 });    ump.insert({ 3, 30 });      // inserts all elements in range    // [begin, end) in mp1    // this function is used to copy elements    // between containers.    ump1.insert(ump.begin(), ump.end());      // prints the elements    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;}
Elements in ump1 are
KEY    ELEMENT
1    10
2    20
3    30

CPP-Functions
cpp-unordered_map
cpp-unordered_map-functions
Picked
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Inheritance in C++
C++ Classes and Objects
Constructors in C++
Bitwise Operators in C/C++
Socket Programming in C/C++
Operator Overloading in C++
Multidimensional Arrays in C / C++
Copy Constructor in C++
Virtual Function in C++
Templates in C++ with Examples