使用方法
#pragma warning(disable:4786) // VisualC++6.0の警告対策
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main() {
map<int, string> names; // キーがint、値がchar*のmap
// 要素を追加する
names.insert( map<int, string>::value_type( 10, "aaa" ) );
names.insert( map<int, string>::value_type( 30, "ccc" ) );
names.insert( map<int, string>::value_type( 20, "bbb" ) );
// 要素を出力する
map<int, string>::iterator it = names.begin();
while( it != names.end() ) {
cout << (*it).first << ":" << (*it).second << endl;
++it;
}
// 要素数を出力する
cout << "要素数:" << (unsigned int)names.size() << endl;
// 要素の全削除
names.clear();
[]演算子によるアクセス
[]の中に添字ではなく、要素のキーを記述する。
names[50] = "fff";
もし存在しないキーを指定してアクセスした場合には、そのキーを自動的に登録する。そして、そのキーに対応する値は、その値の型のデフォルトコンストラクタによって初期化される。
.
最終更新:2007年06月26日 18:52