Different ways to insert elements in an unordered_map

Ways to insert elements in an unordered_map

In this article we will discuss various ways to add items to unordered_map.

Unordered_map provides a variety of overloaded types of input () member functions to insert pairs of key values ​​in unordered_map.

Inserting multiple elements in unordered_map through an initializer_list :

unordered_map provides the most complete type of input that accepts initializer_list i.e.

void insert ( initializer_list<value_type> il );

Includes the number of multiple keys on the map i.e.

// An empty unordered_map is creating
std::unordered_map<std::string, int> wordMap;
 //  Elements are inserting through an initializer_list 
wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } );

With it we can put many things on the map. However, since the map contains unique keys, so what if we insert things with a duplicate key. This version of insert() returns void, so there is no way to know which items are added and which are not. Therefore, in such cases we need a separate version full of overlapped with insert() i.e.

Inserting a single element in an unordered_map and check result :

unordered_map provides the most complete input type that accepts std :: pair of key – value and inserts an object on the map i.e.

pair<iterator,bool> insert ( const value_type&val);

Returns the Iterator and bool. the rotation of the bool in pairs will be true if one item is successfully installed the other will be false. If the item is successfully installed then the paired iterator will point to the newly installed item on the map.

Let’s look at an example,

typedef std::unordered_map<std::string, int>::iterator UOMIterator;
// By pairing of Map Iterator and bool value
std::pair< UOMIterator , bool> result;
//  Elements are inserting through pair
result = wordMap.insert(std::make_pair<std::string, int>("Second", 6));
if(result.second == false)
std::cout<<"Element 'Second' not inserted again"<<std::endl;
Also, instead of pairing and inserting, we can also add value_type using the same input version i.e.
// The Elements are inserting through value_type
result = wordMap.insert({"Fourth", 4});

Complete example :

# Program :

#include <iostream>
#include <unordered_map>
#include <string>
int
main()
{
// To create an empty unordered_map
std::unordered_map<std::string, int> wordMap;
// elements are inserted through an initializer_list
wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } );
typedef std::unordered_map<std::string, int>::iterator UOMIterator;
// Map Iterator and bool value are in pair format
std::pair< UOMIterator , bool> output;
// To insert an element through pair
output = wordMap.insert(std::make_pair<std::string, int>("Second", 6));
if(output.second == false)
std::cout<<"The Element 'Second' is not inserted again"<<std::endl;
  // an element is inserting through value_type
  output = wordMap.insert({"Fourth", 4});
    if(output.second == false)
    {
        std::cout<<"Element 'Fourth' not inserted again"<<std::endl;
    }
    else
    {
      // Element are sucessfully inserted, so first value in pair
      // is the iterator of newly inserted element
      std::cout<<"The Element is Inserted : ";
      std::cout<<output.first->first<<"::"<<output.first->second<<std::endl;
    }
  for (std::pair<std::string, int> element : wordMap)
      std::cout << element.first << " :: " << element.second << std::endl;
  return 0;
}
Output :
The Element 'Second' is not inserted again
The Element is Inserted : Fourth::4
Fourth :: 4
Third :: 3
Second :: 2
First :: 1