Sintassi:
#include <map> iterator find( const key_type& key ); const_iterator find( const key_type& key ) const;
Il metodo find() ritorna un iteratore alla coppia chiave/valore relativa al valore key della chiave oppure un iteratore che punta alla fine della mappa (che può quindi essere confrontato con end()) se la coppia relativa a al valore della chiave non viene trovato.
find() gira a tempo logaritmico.
Per esempio, il codice seguente usa find() per determinare quante volte un utente ha dato all'input una certa parola:
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; }
Se l'input immesso è il seguente
my spoon is too big. my spoon is TOO big! my SPOON is TOO big! I am a BANANA!
il codice produce questo risultato
You typed 'spoon' 2 time(s)