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.

Initializing an unordered_map through a range

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

Initializing an unordered_map through other unordered_map

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

Complete working example, is as follows,



Python Resources

C++11 / C++14 Resources

Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,