// マップの簡単な使用例 #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; int i; // マップにペアを挿入する for(i=0; i<26; i++) m.insert(pair< char, int>('A'+i, 65+i)); char ch; cout << "Enter key: "; cin >> ch; map< char, int>::iterator p; // 与えられたキーに対応する値を探す p = m.find(ch); if(p != m.end()) cout << "Its ASCII value is " << p->second; else cout << "Key not in map.\n"; return 0; }
// 反復子を使ってマップを巡回する #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; int i; // マップにペアを挿入する for(i=0; i<26; i++) m.insert(pair< char, int>('A'+i, 65+i)); map< char, int>::iterator p; // マップの内容を表示する for(p = m.begin(); p != m.end(); p++) { cout << p->first << " has ASCII value of "; cout << p->second << endl; } return 0; }
// マップを逆順に巡回する #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; int i; // マップにペアを挿入する for(i=0; i<26; i++) m.insert(pair< char, int>('A'+i, 65+i)); map< char, int>::reverse_iterator p; // マップの内容を表示する for(p = m.rbegin(); p != m.rend(); p++) { cout << p->first << " has ASCII value of "; cout << p->second << endl; } return 0; }
// []を使用する #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; int i; // マップにペアを挿入する for(i=0; i<26; i++) m.insert(pair< char, int>('A'+i, 65+i)); char ch; cout << "Enter key: "; cin >> ch; // 与えられたキーに対応する値を探す cout << "Its ASCII value is " << m[ch]; return 0; }
// []は自動的に要素を挿入する #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; cout << "Initial size of map: " << m.size() << endl; // 与えられたキーに対応する値を探す cout << "The value associated with A is " << m['A']; cout << endl; cout << "Size of map is now: " << m.size() << endl; return 0; }
// []を使って要素を挿入する #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; int i; // []を使ってペアを挿入する for(i=0; i<26; i++) m['A'+i] = 65+i; char ch; cout << "Enter key: "; cin >> ch; // 与えられたキーに対応する値を探す cout << "Its ASCII value is " << m[ch]; return 0; }
// マップには重複するキーを格納できない #include < iostream> #include < map> using namespace std; int main() { map< char, int> m; pair< map< char,int>::iterator, bool> res; // Aを挿入する res = m.insert(pair< char, int>('A', 65)); if(res.second) cout << "Insertion occured.\n"; // 再びAを挿入しようと試みる res = m.insert(pair< char, int>('A', 99)); if(!res.second) cout << "Duplicate not allowed.\n"; map< char, int>::iterator p; // 与えられたキーに対応する値を探す p = m.find('A'); cout << "Its ASCII value is " << p->second; return 0; }
// マップを使って電話帳を作成する #include < iostream> #include < map> #include < string> using namespace std; // キーのクラス class name { string str; public: name() { str = ""; } name(string s) { str = s; } string get() { return str; } }; // nameオブジェクトに対する<演算子が必要 bool operator<(name a, name b) { return a.get() < b.get(); } // 値のクラス class phoneNum { string str; public: phoneNum() { str = ""; } phoneNum(string s) { str = s; } string get() { return str; } }; int main() { map< name, phoneNum> directory; // 名前と電話番号をマップに挿入する directory.insert(pair< name, phoneNum>(name("Tom"), phoneNum("555-4533"))); directory.insert(pair< name, phoneNum>(name("Chris"), phoneNum("555-9678"))); directory.insert(pair< name, phoneNum>(name("John"), phoneNum("555-8195"))); directory.insert(pair< name, phoneNum>(name("Rachel"), phoneNum("555-0809"))); // 名前から電話番号を見つける string str; cout << "Enter name: "; cin >> str; map< name, phoneNum>::iterator p; p = directory.find(name(str)); if(p != directory.end()) cout << "Phone number: " << p->second.get(); else cout << "Name not in directory.\n"; return 0; }
// greater関数オブジェクトを使用する #include < iostream> #include < map> #include < functional> #include < string> using namespace std; int main() { map< string, int, greater< string> > m; m["Alpha"] = 20; m["Beta"] = 19; m["Gamma"] = 10; map< string, int, greater< string> >::iterator p; // マップの要素を表示する for(p = m.begin(); p != m.end(); p++) { cout << p->first << " has value of "; cout << p->second << endl; } return 0; }
// マルチマップの例 #include < iostream> #include < map> #include < string> using namespace std; int main() { multimap< string, string> names; string n; names.insert(pair< string, string>("Jones", "Fred")); names.insert(pair< string, string>("Jones", "Alice")); names.insert(pair< string, string>("Smith", "Tom")); names.insert(pair< string, string>("Smith", "Alicia")); names.insert(pair< string, string>("Smith", "Jon")); names.insert(pair< string, string>("Doe", "Harvey")); names.insert(pair< string, string>("Doe", "Wilma")); names.insert(pair< string, string>("Doe", "Rachel")); names.insert(pair< string, string>("Johnson", "J.T.")); multimap< string, string>::iterator p; cout << "Enter last name: "; cin >> n; // 最初に一致する反復子を取得する p = names.find(n); if(p != names.end()) { // 名前を見つける do { cout << n << ", " << p->second; cout << endl; p++; } while (p != names.upper_bound(n)); } else cout << "Name not found.\n"; return 0; }
// マルチマップを使って電話帳を改版する #include < iostream> #include < map> #include < string> using namespace std; // キーのクラス class name { string str; public: name() { str = ""; } name(string s) { str = s; } string get() { return str; } }; // nameオブジェクトに対する<演算子が必要 bool operator<(name a, name b) { return a.get() < b.get(); } // 値のクラス class phoneNum { string str; public: phoneNum() { str = ""; } phoneNum(string s) { str = s; } string get() { return str; } }; int main() { multimap< name, phoneNum> directory; // 名前と電話番号をマルチマップに挿入する directory.insert(pair< name, phoneNum>(name("Tom"), phoneNum("555-4533"))); directory.insert(pair< name, phoneNum>(name("Tom"), phoneNum("555-9999"))); directory.insert(pair< name, phoneNum>(name("Chris"), phoneNum("555-9678"))); directory.insert(pair< name, phoneNum>(name("John"), phoneNum("555-8195"))); directory.insert(pair< name, phoneNum>(name("Rachel"), phoneNum("555-0809"))); directory.insert(pair< name, phoneNum>(name("Rachel"), phoneNum("555-3434"))); directory.insert(pair< name, phoneNum>(name("Rachel"), phoneNum("555-7776"))); // 名前から電話番号を見つける string str; cout << "Enter name: "; cin >> str; multimap< name, phoneNum>::iterator p; p = directory.find(str); if(p != directory.end()) { do { cout << "Phone number: " << p->second.get(); cout << endl; p++; } while(p != directory.upper_bound(str)); } else cout << "Name not in directory.\n"; return 0; }