map in c++stl

In C++ programming, map is a container class template used to store data in the form of key-value pairs. Its essence is an associative container that searches and accesses values ​​through keys.

The key-value pairs in the map are automatically sorted according to the key. The value can be found based on the key name, and all key-value pairs are supported. When searching for elements, map uses a red-black tree (RB tree) data structure to implement it, so the search efficiency is quite high.

When using map in C++, you need to introduce the header file <map>, in which the key and value types can be specified arbitrarily. Common key-value pair types include string-int, int-double, etc.

Here is an example of using map to store strings and integers:

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

int main() {
    map<string, int> m;
    m["apple"] = 1;
    m["orange"] = 2;
    m["banana"] = 3;

    cout << "m contains:" << endl;
    for (auto it = m.begin(); it != m.end(); it++) {
        cout << it->first << ": " << it->second << endl;
    }

    return 0;
}

In this program, we define an object m of map type and insert three key-value pairs into this map. Then we iterate through this map and output the key-value pairs. The output result is:

```
m contains:
apple: 1
banana: 3
orange: 2
```

You can see that the keys in the output result are sorted in alphabetical order, and the values ​​correspond to the key names one-to-one. This is one of the characteristics of the map container.

So how to use map specifically?

When using map, you need to pay attention to the following points:

1. The key-value pairs in the map are automatically sorted according to the key, so the key type must support the less than comparison operator, or the user needs to provide a comparison function (or functor) of the key type to sort.
2. The keys in the map must be unique. If you want to store duplicate keys, you need to use a multimap container.
3. The map iterator is a bidirectional iterator that supports forward and backward movement, but does not support random access.
4. Use the subscript operator [] to easily insert, access and modify elements. If the element does not exist, a new element with the key as the subscript will be automatically inserted, and the value will be initialized to the default value of the element type.

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

int main() {
    map<string, int> m;

    // 插入元素
    m.insert(make_pair("apple", 10));
    m.insert(make_pair("orange", 20));
    m["banana"] = 30;

    // 查找元素
    if (m.find("apple") != m.end()) {
        cout << "apple: " << m["apple"] << endl;
    } else {
        cout << "apple not found" << endl;
    }

    // 遍历元素
    cout << "m contains:" << endl;
    for (auto it = m.begin(); it != m.end(); it++) {
        cout << it->first << ": " << it->second << endl;
    }

    // 修改元素
    m["apple"] = 50;

    // 删除元素
    m.erase("orange");

    return 0;
}

In this program, we define a map type object m and insert three key-value pairs into it. We can then use the find function to find out whether a key exists, or use the subscript operator [] to access a value. Then we traverse the map and output the key-value pairs. Then we can modify the value corresponding to a key through the subscript operator []. Finally, we use the erase function to delete the specified key-value pair.

The output result is:

```
apple: 10
m contains:
apple: 10
banana: 30
orange: 20
```
 

Guess you like

Origin blog.csdn.net/m0_74153798/article/details/131837771
Recommended