Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator
unordered_map
insert()

How to use the unordered_map::insert() function in C++

Harsh Jain

Introduction

The unordered_map::insert() function is available in the <unordered_map> header file in C++.

The unordered_map::insert() function is used to insert the element with a specific key in the unordered map.

Syntax

There are five variations of the unordered_map::insert() function. The syntax for all of the three variations is given below:

Iterator insert(K key, E element); 
Iterator insert(Iterator position, {Key, Element});
Iterator insert(Iterator position1, Iterator position2);
Iterator insert(L list);
Iterator insert(P pair);

Parameter

  1. The first variation of the unordered_map::insert(K key, E element) function accepts the following parameters:

    • Key: This is the key that needs to be inserted.
    • Element: This is the value that is to be associated with the key.
  2. The second variation of the unordered_map::insert(Iterator position, {Key, Element}) function accepts the following parameters:

    • Position: This is the iterator that points to the position from where the insertion of the elements needs to be done.
    • Key: This is the key that needs to be inserted.
    • Element: This is the value that is to be associated with the key.
  3. The third variation of the unordered_map::insert(Iterator position1, Iterator position2) function accepts the following parameters:

    • Iterator_position1: This is the iterator that points to the starting position of the range for inserting elements in the map.
    • last: This is the iterator that points to the ending position of the range for inserting elements in the map.
  4. The fourth variation of the unordered_map::insert(L list) function accepts the following parameters:

    • List: This is the initializer list that needs to be inserted in the map.
  5. The fifth variation of the unordered_map::insert(P pair) function accepts the following parameters:

    • Pair: This is the pair that needs to be inserted in the map.

Return

  • The first and second variations of the unordered_map::insert() function return an iterator that points to the new element in the container.

  • The third and fourth variations of the unordered_map::insert() function return no values.

  • The fifth variation of the unordered_map::insert() function returns an iterator that points to the new element in the container.

Code

Now, let’s look at the code:

#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
  unordered_map<int, int> objUnorderedMap, objUnorderedMapOne, objUnorderedMapTwo;

  objUnorderedMap.insert({ 20, 13 }); // First Variation unordered_map::insert(K key, E element)
  objUnorderedMap.insert({ 10, 41 }); // First Variation unordered_map::insert(K key, E element)
  objUnorderedMap.insert({ 131, 80 }); // First Variation unordered_map::insert(K key, E element)

  auto objElement = objUnorderedMap.find(10);

  objUnorderedMap.insert(objElement, {78, 34}); // Second Variation unordered_map::insert(Iterator position, {Key, Element})

  cout << "Unordered_map objUnorderedMap has:" << endl;
  for (auto iterator = objUnorderedMap.begin(); iterator != objUnorderedMap.end(); ++iterator) 
    cout << iterator -> first << " -> " << iterator -> second << endl;

  objUnorderedMapOne.insert(objUnorderedMap.begin(), objUnorderedMap.end()); // Third Variation unordered_map::insert(Iterator position1, Iterator position2)
  
  cout << "Unordered_map objUnorderedMapOne has:" << endl;
  for (auto iterator = objUnorderedMapOne.begin(); iterator != objUnorderedMapOne.end(); ++iterator) 
    cout << iterator -> first << " -> " << iterator -> second << endl;

  objUnorderedMapTwo.insert({{1,67},{57,98},{8,43}}); // Fourth Variation unordered_map::insert(L list)
 
  pair<int,int> objPair (56,890);
  objUnorderedMapTwo.insert(objPair); // Fifth Variation unordered_map::insert(P pair)

  cout << "Unordered_map objUnorderedMapTwo has:" << endl;
  for (auto iterator = objUnorderedMapTwo.begin(); iterator != objUnorderedMapTwo.end(); ++iterator) 
    cout << iterator -> first << " -> " << iterator -> second << endl;

  return 0;
}
Use the "unordered_map::insert()" function in C++

Explanation

  • In lines 1 and 2, we import the required header files.

  • In line 5, we make a main() function.

  • In line 7, we initialize three unordered maps with integer type keys and integer type values.

  • From lines 9 to 11, we use the first variation of the unordered_map::insert() function to insert the key-value pairs in the map.

  • In line 13, we define an iterator, using the find() function for a key.

  • In line 15, we define the starting position using the iterator and the key-value pair for insertion using the second variation of the unordered_map::insert() function.

  • From lines 18 to 19, we use a loop to access the elements of the map and display them.

  • In line 21, we use the third variation of the unordered_map::insert() function to insert the elements in it. We use the begin() and end() functions to obtain the iterators from the map displayed before and insert the elements in the new map and display it, using a loop from lines 23 to 25.

  • In line 27, we insert the elements in another map, using the fourth variation.

  • In line 30, we use the pair for insertion with the help of the fifth variation of the unordered_map::insert() function.

  • From lines 33 to 34, we use a loop to display the elements of this map.

This is how we use the unordered_map::insert() method in C++.

RELATED TAGS

c++
communitycreator
unordered_map
insert()
RELATED COURSES

View all Courses

Keep Exploring