Skip to content

[C++] How to Use “map” of STL

map is the Associative Container of Standard Template Library (STL), it is a container that has key-value correspondence.

This key-value correspondence is one-to-one mapping. Each key exists only once in the map object, and each key corresponds to a specific value. If you accidentally reinsert the new key, the original key will be overwritten by the new one.

In C++ STL, map is implemented using red-black tree, and unordered_map is implemented using hash structures.

The following I record how to use map.


The usage of map container

Let’s take a simplest example:

#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
    map<string, int> map_data = {
        {"Clay", 26},
        {"Atlas", 25},
        {"Mary", 19},
    };

    for (auto &m: map_data) {
        cout << m.first << ": "<< m.second << endl;
    }

    return 0;
}


Output:

Atlas: 25
Clay: 26
Mary: 19


We declared map<string, int> variable map_data, the data types of the key-value representing this map container are string and int. In simple terms, a set of strings is mapped to an integer.

Then below I use &m to take out the value in the map container, where:

  • m.first is key
  • m.second is value

Is it simple?


insert()

Of course we can use insert() function to insert new data in map container.

#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
    // Init
    map<string, int> map_data = {
        {"Clay", 26},
        {"Atlas", 25},
        {"Mary", 19},
    };

    // Insert
    map_data.insert(pair<string, int>("Zack", 35));

    // Print
    for (auto &m: map_data) {
        cout << m.first << ": "<< m.second << endl;
    }

    return 0;
}


Output:

Atlas: 25
Clay: 26
Mary: 19
Zack: 35


we need to use pair object to pass in insert().


find()

If you want to check there is a specific data existed or not, you can use find() function to search.

  • If exist: return the data position
  • If not exist: return the end() position

Let’s take a simple example:

#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
    // Init
    map<string, int> map_data = {
        {"Clay", 26},
        {"Atlas", 25},
        {"Mary", 19},
    };

    // find()
    auto find_01 = map_data.find("Clay");
    auto find_02 = map_data.find("John");

    // Print
    if (find_01 == map_data.end()) {
        cout << "Not found!" << endl;
    }
    else {
        cout << find_01->first << ": " << find_01->second << endl;
    }

    if (find_02 == map_data.end()) {
        cout << "Not found!" << endl;
    }
    else {
        cout << find_02->first << ": " << find_02->second << endl;
    }
    
    return 0;
}


Output:

Clay: 26
Not found!


As you can see, we can find the Clay data in map_data, but you cannot find the John.


erase()

In addition to being able to insert data and search for data, map_data can also use the erase() to delete the specified data.

Suppose we want to delete the Clay data:

#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
    // Init
    map<string, int> map_data = {
        {"Clay", 26},
        {"Atlas", 25},
        {"Mary", 19},
    };

    // erase()
    map_data.erase(map_data.find("Clay"));

    // Print
    for (auto &m: map_data) {
        cout << m.first << ": " << m.second << endl;
    }

    return 0;
}


Output:

Atlas: 25
Mary: 19


In addition, there are the following commonly used functions:

  • empty(): Confirm whether the map container is empty
  • clear(): Completely empty the map container

References


Read More

Tags:

Leave a Reply