#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string, string> mapAdrs;
mapAdrs.insert(pair<string, string>("Anne", "1111-1111"));
mapAdrs.insert(pair<string, string>("Charlie", "3333-3333"));
mapAdrs.insert(pair<string, string>("Bob", "2222-2222"));
mapAdrs.insert(pair<string, string>("Debby", "4444-4444"));
map<string, string>::iterator p;
p = mapAdrs.find("Anne");
if(p != mapAdrs.end()){
cout << p->first << "の電話番号は";
cout << p->second << "です" << endl;
}
else{
cout << "Anneの電話番号が見つかりません" << endl;
}
cout << endl << "電話番号一覧" << endl;
for(p = mapAdrs.begin(); p != mapAdrs.end(); p++){
cout << p->first << "\t: " << p->second << endl;
}
return 0;
}
実行結果
Anneの電話番号は1111-1111です
電話番号一覧
Anne : 1111-1111
Bob : 2222-2222
Charlie : 3333-3333
Debby : 4444-4444