C++ Program to Initializing a Dictionary


C++ does not have dictionaries by the same name as it is in python, but it has the same data structure having similar capabilities. C++ has support for maps, which is available in the STL class std::map. A map object contains a pair of values in each entry, one is a key value and the other one is the mapped value. The key value is used to search and uniquely identify an entry in the map. Whereas the mapped value is not always unique, the key value has to be always unique in a map. Let’s take a look at how to work with maps.

First, let’s see how to define a map data structure in C++.

Syntax

#include 
map <data_type 1, data_type 2> myMap;

Let’s take an example and see how to do this −

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //initialising the map
   map <int, string> myMap;

   //inserting two key-value pairs
   myMap.insert({1, "Hello"});
   myMap.insert({2, "World"});

   //displaying the key-value pairs
   for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
      cout << itr->first << " " << itr->second << endl;
   }

   return 0;
}

Output

1 Hello
2 World

Maps in C++ can be initialized in different ways. The algorithm for it is easy.

Algorithm

  • Create a map object.

  • Assign values to the object while it is being declared.

Initializing a map using an initializer list

Initializing a map using an initializer list is the same as initializing an array in C++. We just assign the key-value pairs while we are declaring the map, enclosed with braces in this {key, value} format. The syntax is given below.

Syntax

#include <map>
map <data_type 1, data_type 2> myMap = {{key1, value1}, {key2, value2}};

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //initialising the map
   map <int, string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

   //displaying the key-value pairs
   for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
      cout << itr->first << " " << itr->second << '\n';
   }

   return 0;
}

Output

1 One
2 Two
3 Three

Initializing a map using the assignment operator

This is similar to assigning a value to a specific index in an array. Instead of mentioning the index, we put the key value in the map subscript as we do in an array.

Syntax

#include 
map <data_type 1, data_type 2> myMap;
myMap[key1] = value1;

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //declaring the map
   map <int, string> myMap;
   myMap[1] = "One";
   myMap[2] = "Two";
   myMap[3] = "Three";

   //displaying the key-value pairs
   for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
      cout << itr->first << " " << itr->second << '\n';
   }

   return 0;
}

Output

1 One
2 Two
3 Three

Initializing a map from another map

There may be a requirement to copy a map into another map, so to do that we can initialize a map from another map. We utilize the copy constructor of the map class by passing a map object to the copy constructor of the map while declaration.

Syntax

#include <map>
map <data_type 1, data_type 2> myMap1(myMap2);

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //declaring the map
   map <int, string> myMap;
   myMap[1] = "One";
   myMap[2] = "Two";
   myMap[3] = "Three";

   //copying using copy constructor
   map <int, string> myMap2(myMap);

   //displaying the key-value pairs
   for (auto itr = myMap2.begin(); itr != myMap2.end(); ++itr) {
      cout << itr->first << " " << itr->second << '\n';
   }

   return 0;
}

Output

1 One
2 Two
3 Three

Conclusion

Map in C++ is an ordered collection, which means the elements in the map are sorted according to the key values. This makes it slower compared to other similar data structures like unordered maps where the key−value pairs aren’t sorted. All the operations in the map have a logarithmic complexity and in memory are implemented as red−black trees. However, in practice maps are very useful as it provides very much flexibility to store data in a key−value fashion. We have discussed all the main methods to initialize a map; although there are more methods to initialize, these are the most intuitive ways to operate.

Updated on: 14-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements