- //
- // CopyRight(c) 2009, YOYO, All Rights Reserved.
- // Author: LIN YiQian
- // Created: 2009/08/24
- // Describe: STL multimap 使用DEMO
- //
- #include <iostream>
- #include <map>
- #include <string>
- using namespace std;
- typedef multimap<int, string> STR_MMAP;
- // 打印MultiMap
- void PrintMap(STR_MMAP strMap)
- {
- for (STR_MMAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)
- {
- cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
- }
- cout << endl;
- }
- // 反向打印MultiMap
- void PrintMapReverse(STR_MMAP strMap)
- {
- for (STR_MMAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)
- {
- cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;
- }
- cout << endl;
- }
- // 打印指定范围的MultiMap
- void PrintMapRange(STR_MMAP strMap, STR_MMAP::key_type low, STR_MMAP::key_type up)
- {
- for (STR_MMAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)
- {
- cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
- }
- cout << endl;
- }
- void main(void)
- {
- STR_MMAP strMap;
- cout << "New StringMultiMap: " << endl; PrintMap(strMap);
- // insert()
- {
- strMap.insert(STR_MMAP::value_type(80, "张三"));
- strMap.insert(STR_MMAP::value_type(70, "李四"));
- strMap.insert(STR_MMAP::value_type(90, "王五"));
- strMap.insert(STR_MMAP::value_type(75, "赵六"));
- cout << "After Insert: " << endl;
- PrintMap(strMap);
- }
- // test insert() again
- {
- strMap.insert(STR_MMAP::value_type(80, "张A"));
- strMap.insert(STR_MMAP::value_type(85, "DFA"));
- strMap.insert(STR_MMAP::value_type(90, "王C"));
- strMap.insert(STR_MMAP::value_type(95, "ASDF"));
- cout << "After Insert again: " << endl;
- PrintMap(strMap);
- }
- // Print Range Map
- {
- cout << "Print Map in Range: low-80, up-90" << endl;
- PrintMapRange(strMap, 80, 90);
- }
- // Print Reverse Map
- {
- cout << "Print Map Reverse: " << endl;
- PrintMapReverse(strMap);
- }
- // find()
- {
- cout << "Find Value 80?: " << boolalpha << (strMap.find(80) != strMap.end()) << endl;
- cout << "Find Value 82?: " << boolalpha << (strMap.find(82) != strMap.end()) << endl;
- cout << endl;
- }
- // erase()
- {
- strMap.erase(85);
- strMap.erase(91);
- cout << "After Erase 85, 91: " << endl; PrintMap(strMap);
- strMap.erase(strMap.find(70));
- cout << "After Erase 70: " << endl; PrintMap(strMap);
- }
- // count()
- {
- cout << "Value 80 counts: " << strMap.count(80) << endl;
- cout << "Value 75 counts: " << strMap.count(75) << endl;
- cout << "Value 60 counts: " << strMap.count(60) << endl;
- cout << endl;
- }
- // size() & max_size()
- {
- cout << "Map size: " << strMap.size() << endl;
- cout << "Map max_size: " << strMap.max_size() << endl;
- cout << endl;
- }
- // empty()
- {
- cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
- cout << endl;
- }
- // clear()
- {
- strMap.clear();
- cout << "After Clear: " << endl;
- PrintMap(strMap);
- cout << "Map size: " << strMap.size() << endl;
- cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
- }
- system("pause");
- }