7 minute read

Introduction

The std::map container in C++ is a sorted associative container that stores elements formed by a combination of a key value and a mapped value. The elements in a map are sorted by its key and it can be accessed by its key value. The std::map container is implemented as a balanced binary search tree, which means that its insertion and search operations have a logarithmic complexity.

Concept

A std::map container stores elements in the form of key-value pairs, where the key is used to access the element, and the value is the actual element stored in the container.

The keys in a std::map container must be unique and are used to sort the elements in the container. The keys in a std::map container are of a specific data type, and the values can be of any data type.

Creating a map

We can create a map just like shown below:

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;
    return 0;
}

We use the std::map<key type, value type> mapName syntax in order to initialize a map. Notice how the key will be a std::string type and its value (what it stores) will be an int.

Please note that we are importing the <map> library in order to initialize a map data structure in C++.

Adding key and value pairs to a map

In this case, insertion of key and value pairs is done using the [] operator in C++.

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;
    // Adding elements
    myMap["apple"] = 10;
    myMap["banana"] = 15;
    myMap["orange"] = 20;
    return 0;
}

We use the mapName[key] = value; syntax in order to add a key value pair to a map. Notice how this is similar to other programming languages such as Python.

Accessing values of a key in a map

In order to access a value of a certain key in a map, we will use the [] operator just like we did previously.

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;
    // Adding elements
    myMap["apple"] = 10;
    myMap["banana"] = 15;
    myMap["orange"] = 20;
    std::cout << "Price of apple: " << myMap["apple"] << std::endl;
    std::cout << "Price of banana: " << myMap["banana"] << std::endl;
    std::cout << "Price of orange: " << myMap["orange"] << std::endl;
    return 0;
}

This would output the following:

Price of apple: 10
Price of banana: 15
Price of orange: 20

Iterating through the keys of a map

There are many ways to iterate through the keys of a map, For instance, one way is to use a for (auto i = myMap.begin(); i != myMap.end(); i++) loop. Below, you will find an example of this:

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;
    // Adding elements
    myMap["apple"] = 10;
    myMap["banana"] = 15;
    myMap["orange"] = 20;
    for (auto i = myMap.begin(); i != myMap.end(); i++) {
        std::cout << i->first << " " << i->second << std::endl;
    }
    return 0;
}

It would go through every <key, value> pair and the output would look like this:

apple 10
banana 15
orange 20

Check if element in present in a map

In this scenario, we will use the mapName.count(keyName)" function. This function takes a keyName as a parameter and it will count how many times a key with that name was found. In other words, it will return 1 if it has been found or 0 if not.

Below, we will see an example of this function:

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;
    // Adding elements
    myMap["apple"] = 10;
    myMap["banana"] = 15;
    myMap["orange"] = 20;

    // Checking if an element is present in the map
    if (myMap.count("apple") > 0) {
        std::cout << "apple is present in the map" << std::endl;
    } else {
        std::cout << "apple is not present in the map" << std::endl;
    }

    if (myMap.count("pineapple") > 0) {
        std::cout << "pineapple is present in the map" << std::endl;
    } else {
        std::cout << "pineapple is not present in the map" << std::endl;
    }
    return 0;
}

Therefore, this will output the following:

apple is present in the map
pineapple is not present in the map

Full example

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;
    // Adding elements
    myMap["apple"] = 10;
    myMap["banana"] = 15;
    myMap["orange"] = 20;
    std::cout << "Price of apple: " << myMap["apple"] << std::endl;
    std::cout << "Price of banana: " << myMap["banana"] << std::endl;
    std::cout << "Price of orange: " << myMap["orange"] << std::endl;
    // Iterating through the keys
    for (auto i = myMap.begin(); i != myMap.end(); i++) {
        std::cout << i->first << " " << i->second << std::endl;
    }

    // Counting number of elements in the map
    std::cout << "Number of elements in the map: " << myMap.size() << std::endl;

    // Checking if an element is present in the map
    if (myMap.count("apple") > 0) {
        std::cout << "apple is present in the map" << std::endl;
    } else {
        std::cout << "apple is not present in the map" << std::endl;
    }
    return 0;
}

In this example, we create a std::map container with the key type as std::string and value type as int. We insert elements into the map using the [] operator and access the elements using the same operator.

We can also iterate through the elements in the map using an iterator, and use the size() function to get the number of elements in the map. The count() function is used to check if an element is present in the map.

Conclusion

The std::map container in C++ is a useful data structure that allows us to store and access elements in a sorted and efficient way. It stores elements in the form of key-value pairs, where the key is used to access the element, and the value is the actual element stored in the container. The keys in a std::map container must be unique and are used to sort the elements in the container.

This container also provides various member functions that allow us to perform various operations such as inserting, deleting, searching, and iterating through elements in the map.

It is commonly used in situations where we need to store and access elements in a sorted manner based on keys and it is implemented as a balanced binary search tree, which makes the insertion and search operations have logarithmic time complexity.

References

Read more about the std::map here: https://cplusplus.com/reference/map/map/