find [C++ Reference]
Keywords: stl,map,find
Date: 2009-06-06T01:16:12-0700

[[find]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## find

Syntax:

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

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)

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •