Syntax:
#include <map> iterator begin(); const_iterator begin() const;
begin()関数は、マップの最初の要素へのイテレータを返します。 もし、マップが空の場合は、end()と同じ動作をします。
begin()関数は、常に一定の処理時間で処理するでしょう。
たとえば、次のコードは、リスト走査用イテレータの初期化に begin()関数を使用しています。
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; }
次の入力が与えられたときは、
here are some words and here are some more words
…上記のコードは、この出力を生成します。
word: and, count: 1
word: are, count: 2
word: here, count: 2
word: more, count: 1
word: some, count: 2
word: words, count: 2