find

Syntax:

    #include <map>
    iterator find( const key_type& key );
    const_iterator find( const key_type& key ) const;

The find() function returns an iterator to the matching key/value pair, or an iterator to the end of the map if key is not found.

find() runs in logarithmic time.

For example, the following code uses the find() function to determine how many times a user entered a certain word:

    map<string,int> stringCounts;
    string str;
 
    while( cin >> str ) ++stringCounts[str];
 
    map<string,int>::iterator iter = stringCounts.find("spoon");
    if( iter != stringCounts.end() ) {
      cout << "You typed '" << iter->first << "' " << iter->second << " time(s)" << endl;
    }

When run with this input:

  my spoon is too big.  my spoon is TOO big!  my SPOON is TOO big!  I am a BANANA!

…the above code produces this output:

  You typed 'spoon' 2 time(s)