According to the C ++ StandardProgramThe find () member function of map can be used to search for the first element with a key, but cannot search for the element with a specific value.
If you search for a specific value, you need to use find_if () or traverse it cyclically.
The following example shows how to use find_if ():
# Include <iostream> # include <map> # include <string> # include <algorithm> typedef STD: Map <STD: String, int> map_type; Template <class K, class V> class map_find_func {PRIVATE: V value; public: map_find_func (const V & V): Value (v) {} bool operator () (STD: pair <const K, v> & ELEM) {return (ELEM. second = value) ;}}; int main () {map_type col1; col1.insert (STD: make_pair ("1234", 1); col1.insert (STD :: make _ pair ("2234" , 2); map_type: iterator it = STD: find_if (col1.begin (), col1.end (), map_find_func <STD: String, int> (1 )); if (it! = Col1.end () {STD: cout <it-> first <STD: Endl ;}}
Output result:
1234
Press any key to continue