C++ judges whether the key value in the map exists

1. Count function

The count function is used to count the number of times the key value appears in the map. The key of the map is not allowed to be repeated, so if the key exists, it returns 1 and if it does not exist, it returns 0.

if (mp.count(key) == 0)
    cout << "no this key" << endl;

2. Find function
If the key exists, find returns the iterator corresponding to the key. If the key does not exist, find returns the iterator after the end. End()

if (mp.find(key) == mp.end())
    cout << "no this key" << endl;

Guess you like

Origin blog.csdn.net/chaokudeztt/article/details/114444493
Recommended