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 }
  	  	  	  	  	  	  	  	  	  	  	  	});

Advertisements

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

 

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