STL容器使用DEMO-map

Code:
  1. //   
  2. //  CopyRight(c) 2009, YOYO, All Rights Reserved.   
  3. //  Author: LIN YiQian   
  4. //  Created: 2009/08/24   
  5. //  Describe: STL map 使用DEMO   
  6. //   
  7. #include <iostream>   
  8. #include <map>   
  9. #include <string>   
  10.   
  11. using namespace std;   
  12.   
  13. typedef map<int, string> STR_MAP;   
  14.   
  15. //  打印Map   
  16. void PrintMap(STR_MAP strMap)   
  17. {   
  18.     for (STR_MAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)   
  19.     {   
  20.         cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;   
  21.     }   
  22.     cout << endl;   
  23. }   
  24.   
  25. //  反向打印Map   
  26. void PrintMapReverse(STR_MAP strMap)   
  27. {   
  28.     for (STR_MAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)   
  29.     {   
  30.         cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;   
  31.     }   
  32.     cout << endl;   
  33. }   
  34.   
  35. //  打印指定范围的Map   
  36. void PrintMapRange(STR_MAP strMap, STR_MAP::key_type low, STR_MAP::key_type up)   
  37. {   
  38.     for (STR_MAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)   
  39.     {   
  40.         cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;   
  41.     }   
  42.     cout << endl;   
  43. }   
  44.   
  45. void main(void)   
  46. {   
  47.     STR_MAP strMap;   
  48.     cout << "New StringMap: " << endl; PrintMap(strMap);   
  49.   
  50.     //  insert()   
  51.     {   
  52.         strMap.insert(STR_MAP::value_type(0, "Zero"));   
  53.         strMap.insert(STR_MAP::value_type(1, "One"));   
  54.         strMap.insert(STR_MAP::value_type(2, "Two"));   
  55.         strMap.insert(STR_MAP::value_type(6, "Six"));   
  56.         strMap.insert(STR_MAP::value_type(4, "Four"));   
  57.         strMap.insert(STR_MAP::value_type(8, "Eight"));   
  58.         strMap.insert(STR_MAP::value_type(9, "Nine"));   
  59.         cout << "After Insert: " << endl;   
  60.         PrintMap(strMap);   
  61.     }   
  62.   
  63.     //  test insert() again   
  64.     {   
  65.         strMap.insert(STR_MAP::value_type(1, "One"));   //  fail   
  66.         strMap.insert(STR_MAP::value_type(3, "Three")); //  success   
  67.         strMap.insert(STR_MAP::value_type(4, "Four"));  //  fail   
  68.         strMap.insert(STR_MAP::value_type(8, "Eight")); //  fail   
  69.         strMap.insert(STR_MAP::value_type(7, "Seven")); //  success   
  70.         strMap.insert(STR_MAP::value_type(5, "Five"));  //  success   
  71.         cout << "After Insert again: " << endl;   
  72.         PrintMap(strMap);   
  73.     }   
  74.   
  75.     //  Print Range Map   
  76.     {   
  77.         cout << "Print Map in Range: low-2, up-7" << endl;   
  78.         PrintMapRange(strMap, 2, 7);   
  79.     }   
  80.   
  81.     //  Print Reverse Map   
  82.     {   
  83.         cout << "Print Map Reverse: " << endl;   
  84.         PrintMapReverse(strMap);   
  85.     }   
  86.   
  87.     //  find()   
  88.     {   
  89.         cout << "Find Value 3?: " << boolalpha << (strMap.find(3) != strMap.end()) << endl;   
  90.         cout << "Find Value 12?: " << boolalpha << (strMap.find(12) != strMap.end()) << endl;   
  91.         cout << endl;   
  92.     }   
  93.   
  94.     //  erase()   
  95.     {   
  96.         strMap.erase(9);   
  97.         strMap.erase(19);   
  98.         cout << "After Erase 9, 19: " << endl; PrintMap(strMap);   
  99.   
  100.         strMap.erase(strMap.find(2));   
  101.         cout << "After Erase 2: " << endl; PrintMap(strMap);   
  102.     }   
  103.   
  104.     //  count()   
  105.     {   
  106.         cout << "Value 4 counts: " << strMap.count(4) << endl;   
  107.         cout << "Value 9 counts: " << strMap.count(9) << endl;   
  108.         cout << endl;   
  109.     }   
  110.   
  111.     //  size() & max_size()   
  112.     {   
  113.         cout << "Map size: " << strMap.size() << endl;   
  114.         cout << "Map max_size: " << strMap.max_size() << endl;   
  115.         cout << endl;   
  116.     }   
  117.   
  118.     //  empty()   
  119.     {   
  120.         cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;   
  121.         cout << endl;   
  122.     }   
  123.   
  124.     //  A Demo for Using Map to Search   
  125.     {   
  126.         string inKey = "";   
  127.         while (true)   
  128.         {   
  129.             cout << "Input 'q' to exit, or Input a Number: ";   
  130.             cin >> inKey;   
  131.   
  132.             if (inKey == "q")   
  133.             {   
  134.                 break;   
  135.             }   
  136.   
  137.             string::size_type strLen = inKey.length();   
  138.             string::size_type index = 0;   
  139.   
  140.             for (; index < strLen; ++index)   
  141.             {   
  142.                 string::reference ch = inKey[index];   
  143.                 STR_MAP::key_type key = ch - STR_MAP::key_type('0');   
  144.   
  145.                 STR_MAP::iterator strMapIter = strMap.find(key);   
  146.   
  147.                 if (strMapIter == strMap.end())   
  148.                 {   
  149.                     cout << "[not found] " ;   
  150.                 }   
  151.                 else  
  152.                 {   
  153.                     cout << "[" << (*strMapIter).second << "] ";   
  154.                 }   
  155.             }   
  156.   
  157.             cout << endl;   
  158.         }   
  159.   
  160.         cout << endl;   
  161.     }   
  162.   
  163.     //  clear()   
  164.     {   
  165.         strMap.clear();   
  166.         cout << "After Clear: " << endl;   
  167.         PrintMap(strMap);   
  168.   
  169.         cout << "Map size: " << strMap.size() << endl;   
  170.         cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;   
  171.     }   
  172.   
  173.     system("pause");   
  174. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值