C++ STL map get the value corresponding to the key

Time:2022-6-2

1、 Method

1. Obtained through []

Similar to accessing array elements.

std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };
string strData = mapInfo[1];  
std::cout << "strData: " << strData << std::endl;

The results are as follows:
C++ STL map get the value corresponding to the key
be careful:
1) Only when there is a key value pair containing the specified key in the map container, the overloaded [] operator can successfully obtain the value corresponding to the key
2) If there is no key value pair containing the specified key in the current map container, the [] operator will no longer access the elements in the container, but instead add a key value pair to the map container

std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };
mapInfo[5] = "li";
std::map<int, string>::iterator mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << "key: " << mapIter->first << " value: " << mapIter->second << std::endl;
}

The results are as follows:
C++ STL map get the value corresponding to the key

3) The key of the key value pair uses the key specified in the [] operator, and its corresponding value depends on the data type of the value in the key value pair specified by the map container. If it is a basic data type, the value is 0; If it is a string type, its value is’ ‘, that is, an empty string (that is, the default value of this type is used as the value of the key value pair)
4) [] has the premise of adding new key value pairs: the newly added key does not exist in the current map container
5) If the corresponding key already exists in the map container, execute map[key] = value to modify the value of the key instead of adding a new key value pair to the map container

mapInfo[5] = "wne";
mapIter = mapInfo.begin();
for (; mapIter != mapInfo.end(); mapIter++)
{
    std::cout << "mod key: " << mapIter->first << " value: " << mapIter->second << std::endl;
}

The results are as follows:
C++ STL map get the value corresponding to the key

2、at()

You need to specify a key to find the value corresponding to the key in the container; If the search fails in the current container, the method will not add a new key value pair to the container, but directly throw out_ of_ Range exception.

std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };
mapInfo[5] = "li";

try
{
    std::cout << mapInfo.at(3) << std::endl;
    std::cout << mapInfo.at(10) << std::endl;
       
}
catch (const std::exception&)
{
    Std:: cout < < call at to get the value corresponding to the key is abnormal < < std:: endl;
}

The results are as follows:
C++ STL map get the value corresponding to the key

3、find

If the search is successful, the iterator points to the found key value pair; Otherwise, it points to the position after the last key value pair of the map container (the same as the iterator returned by the end () successful method).

std::map<int, string> mapInfo{ {1,"test"},{2,"lin"},{3,"wei"} };
mapInfo[5] = "li";

std::map<int, string>::iterator mapIter = mapInfo.find(5);
if (mapIter != mapInfo.end())
{
    std::cout << mapIter->first << "  " << mapIter->second << std::endl;
}

The results are as follows:
C++ STL map get the value corresponding to the key

4. If none of the above methods can get the value, you can only traverse the entire map container to get the value
The specific code is not written here