- //
- // CopyRight(c) 2009, YOYO, All Rights Reserved.
- // Author: LIN YiQian
- // Created: 2009/08/24
- // Describe: STL map 使用DEMO
- //
- #include <iostream>
- #include <map>
- #include <string>
- using namespace std;
- typedef map<int, string> STR_MAP;
- // 打印Map
- void PrintMap(STR_MAP strMap)
- {
- for (STR_MAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)
- {
- cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
- }
- cout << endl;
- }
- // 反向打印Map
- void PrintMapReverse(STR_MAP strMap)
- {
- for (STR_MAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)
- {
- cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;
- }
- cout << endl;
- }
- // 打印指定范围的Map
- void PrintMapRange(STR_MAP strMap, STR_MAP::key_type low, STR_MAP::key_type up)
- {
- for (STR_MAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)
- {
- cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
- }
- cout << endl;
- }
- void main(void)
- {
- STR_MAP strMap;
- cout << "New StringMap: " << endl; PrintMap(strMap);
- // insert()
- {
- strMap.insert(STR_MAP::value_type(0, "Zero"));
- strMap.insert(STR_MAP::value_type(1, "One"));
- strMap.insert(STR_MAP::value_type(2, "Two"));
- strMap.insert(STR_MAP::value_type(6, "Six"));
- strMap.insert(STR_MAP::value_type(4, "Four"));
- strMap.insert(STR_MAP::value_type(8, "Eight"));
- strMap.insert(STR_MAP::value_type(9, "Nine"));
- cout << "After Insert: " << endl;
- PrintMap(strMap);
- }
- // test insert() again
- {
- strMap.insert(STR_MAP::value_type(1, "One")); // fail
- strMap.insert(STR_MAP::value_type(3, "Three")); // success
- strMap.insert(STR_MAP::value_type(4, "Four")); // fail
- strMap.insert(STR_MAP::value_type(8, "Eight")); // fail
- strMap.insert(STR_MAP::value_type(7, "Seven")); // success
- strMap.insert(STR_MAP::value_type(5, "Five")); // success
- cout << "After Insert again: " << endl;
- PrintMap(strMap);
- }
- // Print Range Map
- {
- cout << "Print Map in Range: low-2, up-7" << endl;
- PrintMapRange(strMap, 2, 7);
- }
- // Print Reverse Map
- {
- cout << "Print Map Reverse: " << endl;
- PrintMapReverse(strMap);
- }
- // find()
- {
- cout << "Find Value 3?: " << boolalpha << (strMap.find(3) != strMap.end()) << endl;
- cout << "Find Value 12?: " << boolalpha << (strMap.find(12) != strMap.end()) << endl;
- cout << endl;
- }
- // erase()
- {
- strMap.erase(9);
- strMap.erase(19);
- cout << "After Erase 9, 19: " << endl; PrintMap(strMap);
- strMap.erase(strMap.find(2));
- cout << "After Erase 2: " << endl; PrintMap(strMap);
- }
- // count()
- {
- cout << "Value 4 counts: " << strMap.count(4) << endl;
- cout << "Value 9 counts: " << strMap.count(9) << 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;
- }
- // A Demo for Using Map to Search
- {
- string inKey = "";
- while (true)
- {
- cout << "Input 'q' to exit, or Input a Number: ";
- cin >> inKey;
- if (inKey == "q")
- {
- break;
- }
- string::size_type strLen = inKey.length();
- string::size_type index = 0;
- for (; index < strLen; ++index)
- {
- string::reference ch = inKey[index];
- STR_MAP::key_type key = ch - STR_MAP::key_type('0');
- STR_MAP::iterator strMapIter = strMap.find(key);
- if (strMapIter == strMap.end())
- {
- cout << "[not found] " ;
- }
- else
- {
- cout << "[" << (*strMapIter).second << "] ";
- }
- }
- cout << 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");
- }