Sintassi:
#include <map> iterator begin(); const_iterator begin() const;
La funzione begin() ritorna un iteratore che punta al primo elemento della mappa. Se la mappa non contiene elementi allora begin() ritorna lo stesso valore di iteratore ritornato da end().
begin() gira in tempo costante.
Esempio: il codice qui mostrato usa begin() per inizializzare un iteratore che viene usato per percorrere una lista:
map<string,int> stringCounts; string str; while( cin >> str ) ++stringCounts[str]; map<string,int>::iterator iter; for( iter = stringCounts.begin(); iter != stringCounts.end(); ++iter ) { cout << "word: " << iter->first << ", count: " << iter->second << endl; }
Se questo testo viene dato come input
here are some words and here are some more words
…l'output generato è il seguente:
word: and, count: 1
word: are, count: 2
word: here, count: 2
word: more, count: 1
word: some, count: 2
word: words, count: 2