Different ways to insert elements in an unordered_map

In this article we will discuss the different ways to insert elements in an unordered_map.

Unordered_map provides different overloaded versions of insert() member function to insert key value pairs in an unordered_map. Lets discuss them one by one.

Inserting multiple elements in unordered_map through an initializer_list

unordered_map provides an overloaded version of insert that accepts an initializer_list i.e.

void insert ( initializer_list<value_type> il );

It inserts the multiple key value pair in the map i.e.
// Creating an empty unordered_map
std::unordered_map<std::string, int> wordMap;

// Inserting elements through an initializer_list
wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } );

With the we can insert multiple elements in map. But, as map contains can contain only unique keys, so what if we add elements with duplicate key. This version of insert() function returns void , so there is no way to know which elements are added and which are not. Therefore, for such scenarios we need different overloaded version of insert() i.e.

Inserting a single element in an unordered_map and check result

unordered_map provides an overloaded version of insert that accepts an std::pair of key – value and inserts the element in map i.e.

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

It returns a pair of Iterator and bool.  bool variable in pair will be true if element is inserted successfully else it will be false. If element is inserted successfully then iterator in pair will point to the newly inserted element in map.

Advertisements

Let’s see an example,

typedef std::unordered_map<std::string, int>::iterator UOMIterator;
// Pair of Map Iterator and bool value
std::pair< UOMIterator , bool> result;

// Inserting an element 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 creating pair and then inserting, we can also insert the value_type using same version of insert i.e.
 // Inserting an element through value_type
  result = wordMap.insert({"Fourth", 4});

Complete working example is as follows,
#include <iostream>
#include <unordered_map>
#include <string>

int
main()
{
// Creating an empty unordered_map
std::unordered_map<std::string, int> wordMap;

// Inserting elements through an initializer_list
wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } );

typedef std::unordered_map<std::string, int>::iterator UOMIterator;

// Pair of Map Iterator and bool value
std::pair< UOMIterator , bool> result;

// Inserting an element 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;

  // Inserting an element through value_type
  result = wordMap.insert({"Fourth", 4});

    if(result.second == false)
    {
        std::cout<<"Element 'Fourth' not inserted again"<<std::endl;
    }
    else
    {
      // Element inserted sucessfully, so first value in pair
      // is the iterator of newly inserted element
      std::cout<<"Element Inserted : ";
      std::cout<<result.first->first<<"::"<<result.first->second<<std::endl;
    }


  for (std::pair<std::string, int> element : wordMap)
      std::cout << element.first << " :: " << element.second << std::endl;
  return 0;
}

Output:
Element 'Second' not inserted again
Element Inserted : Fourth::4
Fourth :: 4
Third :: 3
Second :: 2
First :: 1

 

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top