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,

Ouput:

 

Click Here to Subscribe for more Articles / Tutorials like this.