Different Ways to initialize an unordered_map

In this article we will discuss the different ways to initialize an unordered_map.


unordered_map provides different overloaded constructors. We will use some of them to initialize an unordered_set in 3 different ways i.e.

  • Initializing through an initializer_list
  • Initializing through a iterable range.
  • Initializing through an another map.

Lets see them in detail,

Initializing an unordered_map using initializer_list

unordered_map provides an over loaded constructor that can accept an initializer_list as an argument and can initialize  an unordered_map.

Let’s create an unordered_map of key type string and value type int. While creation it will initialized with 3 elements through an initializer_list i.e.

// Initialize an unordered_map through initializer_list
  std::unordered_map<std::string, int> wordMap({
												  { "First", 1 },
												  { "Second", 2 },
												  { "Third", 3 }
  	  	  	  	  	  	  	  	  	  	  	  	});

Initializing an unordered_map through a range

We can also initialize an unordered_map through a range of std::pair elements i.e

  // Initialize an unordered_map through another range of elements of type std::pair
  std::unordered_map<std::string, int> wordMap_2(wordMap.begin(),
      wordMap.end());

Initializing an unordered_map through other unordered_map

We can also initialize an unordered_map through an existing unordered_map i.e.

  // Initialize an unordered_map through other unordered_map
  std::unordered_map<std::string, int> wordMap_3(wordMap);

Complete working example, is as follows,

#include <iostream>
#include <unordered_map>
#include <string>

int main()
{
	// Initialize an unordered_map through initializer_list
	std::unordered_map<std::string, int> wordMap(
	{
	{ "First", 1 },
	{ "Second", 2 },
	{ "Third", 3 } });

	// Iterate over an unordered_map and display elements
	for (std::pair<std::string, int> element : wordMap)
		std::cout << element.first << " :: " << element.second << std::endl;

	std::cout << "*******************" << std::endl;

	// Initialize an unordered_map through another range of elements of type std::pair
	std::unordered_map<std::string, int> wordMap_2(wordMap.begin(),
			wordMap.end());

	// Iterate over an unordered_map and display elements
	for (std::pair<std::string, int> element : wordMap_2)
		std::cout << element.first << " :: " << element.second << std::endl;

	std::cout << "*******************" << std::endl;

	// Initialize an unordered_map through other unordered_map
	std::unordered_map<std::string, int> wordMap_3(wordMap);

	// Iterate over an unordered_map and display elements
	for (std::pair<std::string, int> element : wordMap_3)
		std::cout << element.first << " :: " << element.second << std::endl;

	return 0;
}

Ouput:

Third :: 3
First :: 1
Second :: 2
*******************
Second :: 2
Third :: 3
First :: 1
*******************
Third :: 3
First :: 1
Second :: 2

 

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top