Dictionaries in C++ | All you need to know

Dictionaries in C++

Dictionaries are very useful data structures. If you have worked in python, you must know about it. But unlike python, which has an inbuilt data structure for dictionary, C++ has a map data structure in the C++ standard template library (STL). What is STL you ask? It is a collection of useful data structures and algorithms that can be included in a C++ program and used to make the program efficient.

If this is too much of an information overload for you, don’t worry. We will discuss everything needed to understand dictionaries so that you completely understand the concept of dictionaries in C++ programming and how it is used. So, let’s dive right into it!

What is a Dictionary?

When we talk about dictionary, most people understand it as a Python Dictionary. A python dictionary is a key-value data structure that is very useful to hold, well, key-value pairs where the order in which the pairs are stored does not matter. 

An example of this would be storing ratings for different movies by name. In this scenario, we are not concerned with which movie is stored at what index in the memory, but we are concerned with the rating for each movie. Therefore, a dictionary is very handy in such cases.

While C++ does not have dictionaries, it has a Map data structure. Let’s know about the map in detail.

What is Map in C++?

A map in C++ is a data structure that stores key-value pairs, through which values can be accessed directly using their respective keys.

Syntax for Map:

map <key_data_type, value_data_type> map_name;

Some important things to know about maps:

  • The data type of keys should remain same for all key value-pairs
  • The data type of values should remain same for all key value-pairs
  • The data type of key and value may differ from each other

How to Create a Dictionaries in C++?

Now that we know what is a map and understand what its uses are, let’s see how we create a map and use it. To create a map in C++, we need to follow some steps.

Step 1 : Including the map header file

To create a map, we first need  to import the map header file. What is a header file ? Basically, it is a file which stores the code for some specific functions and classes that can be included in a program and used as per needed. 

In our case, the map header file stores the code for declaration and functioning for the map data structure.             

#include<map>

Step 2 : Creating the Map

Now it’s time to create our map. We will declare a map with data type String for keys (movie names) and float for values (ratings)

map<string, float> m;

Step 3 : Assigning values to map

Now we need to add some key-value pairs to our map. There are 3 ways to assign values. Let’s see them one by one.

Method 1 : Using Array Syntax

m["Black Panther"] = 7.3;

Method 2 : Using Curly braces

m.insert({"Spider-Man: No Way Home", 8.3});

Method 3 : Using the Insert Function and Pair Data Structure

The pair data structure does exactly what it sounds like, it provides a pair of two variables. In fact, the method 2 shown above is just a shorthand syntax to directly pass a pair value.

In pair data structure, we can access the first value using .first and the second value using .second

m.insert(pair<string, float> ("Kantara", 8.5));

Step 4 : Traversing the map to access the values

Now that our map is declared and the values have been assigned to keys, we now have to learn how to access these values. We can do that using a loop for all the values, or using the array syntax for a single value.

Here we will use two functions of map header file, begin() and end(). They point to the first and last element of the pair respectively. We also use an iterator which can be used to traverse over the map.

// creating iterator for map traversal
    map<string, float>::iterator it;
 
    cout << "Keys and Values are as follows : " << endl;
    for (it = m.begin(); it != m.end(); it++)
    {
        cout << (*it).first << " : " << (*it).second << "\n";
    }

A Complete Example

Now, we will see how a complete program using the above steps looks like. We will be implementing the same map as in the steps we discussed just now.

#include <iostream>
#include <map>
using namespace std;
 
int main()
{
    map<string, float> m;
    m["Black Panther"] = 7.3;
 
    m.insert({"Spider-Man: No Way Home", 8.3});
 
    m.insert(pair<string, float>("Kantara", 8.5));
 
    // creating iterator for map traversal
    map<string, float>::iterator it;
 
    cout << "Keys and Values are as follows : " << endl;
    for (it = m.begin(); it != m.end(); it++)
    {
        cout << (*it).first << " : " << (*it).second << "\n";
    }
 
    return 0;
}

Output:

Keys and Values are as follows : 

Black Panther : 7.3

Kantara : 8.5

Spider-Man: No Way Home : 8.3

Conclusion

Programmers are constantly solving new problems. Having a useful tool like dictionary in your arsenal needs no further persuasion. Maps are just a way to implement dictionaries in C++. While they seem easy on the front, they might give problems while applying in a complex program. Hence, we suggest the concepts frequently so that you can apply these concepts wherever needed.