STL容器使用DEMO-multimap

Code:
  1. //   
  2. //  CopyRight(c) 2009, YOYO, All Rights Reserved.   
  3. //  Author: LIN YiQian   
  4. //  Created: 2009/08/24   
  5. //  Describe: STL multimap 使用DEMO   
  6. //  
  7. #include <iostream>  
  8. #include <map>  
  9. #include <string>   
  10.   
  11. using namespace std;   
  12.   
  13. typedef multimap<intstring> STR_MMAP;   
  14.   
  15. //  打印MultiMap   
  16. void PrintMap(STR_MMAP strMap)   
  17. {   
  18.     for (STR_MMAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)   
  19.     {   
  20.         cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;   
  21.     }   
  22.     cout << endl;   
  23. }   
  24.   
  25. //  反向打印MultiMap   
  26. void PrintMapReverse(STR_MMAP strMap)   
  27. {   
  28.     for (STR_MMAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)   
  29.     {   
  30.         cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;   
  31.     }   
  32.     cout << endl;   
  33. }   
  34.   
  35. //  打印指定范围的MultiMap   
  36. void PrintMapRange(STR_MMAP strMap, STR_MMAP::key_type low, STR_MMAP::key_type up)   
  37. {   
  38.     for (STR_MMAP::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_MMAP strMap;   
  48.     cout << "New StringMultiMap: " << endl; PrintMap(strMap);   
  49.   
  50.     //  insert()   
  51.     {   
  52.         strMap.insert(STR_MMAP::value_type(80, "张三"));   
  53.         strMap.insert(STR_MMAP::value_type(70, "李四"));   
  54.         strMap.insert(STR_MMAP::value_type(90, "王五"));   
  55.         strMap.insert(STR_MMAP::value_type(75, "赵六"));   
  56.         cout << "After Insert: " << endl;   
  57.         PrintMap(strMap);   
  58.     }   
  59.   
  60.     //  test insert() again   
  61.     {   
  62.         strMap.insert(STR_MMAP::value_type(80, "张A"));   
  63.         strMap.insert(STR_MMAP::value_type(85, "DFA"));   
  64.         strMap.insert(STR_MMAP::value_type(90, "王C"));   
  65.         strMap.insert(STR_MMAP::value_type(95, "ASDF"));   
  66.         cout << "After Insert again: " << endl;   
  67.         PrintMap(strMap);   
  68.     }   
  69.   
  70.     //  Print Range Map   
  71.     {   
  72.         cout << "Print Map in Range: low-80, up-90" << endl;   
  73.         PrintMapRange(strMap, 80, 90);   
  74.     }   
  75.   
  76.     //  Print Reverse Map   
  77.     {   
  78.         cout << "Print Map Reverse: " << endl;   
  79.         PrintMapReverse(strMap);   
  80.     }   
  81.   
  82.     //  find()   
  83.     {   
  84.         cout << "Find Value 80?: " << boolalpha << (strMap.find(80) != strMap.end()) << endl;   
  85.         cout << "Find Value 82?: " << boolalpha << (strMap.find(82) != strMap.end()) << endl;   
  86.         cout << endl;   
  87.     }   
  88.   
  89.     //  erase()   
  90.     {   
  91.         strMap.erase(85);   
  92.         strMap.erase(91);   
  93.         cout << "After Erase 85, 91: " << endl; PrintMap(strMap);   
  94.   
  95.         strMap.erase(strMap.find(70));   
  96.         cout << "After Erase 70: " << endl; PrintMap(strMap);   
  97.     }   
  98.   
  99.     //  count()   
  100.     {   
  101.         cout << "Value 80 counts: " << strMap.count(80) << endl;   
  102.         cout << "Value 75 counts: " << strMap.count(75) << endl;   
  103.         cout << "Value 60 counts: " << strMap.count(60) << endl;   
  104.         cout << endl;   
  105.     }   
  106.   
  107.     //  size() & max_size()   
  108.     {   
  109.         cout << "Map size: " << strMap.size() << endl;   
  110.         cout << "Map max_size: " << strMap.max_size() << endl;   
  111.         cout << endl;   
  112.     }   
  113.   
  114.     //  empty()   
  115.     {   
  116.         cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;   
  117.         cout << endl;   
  118.     }   
  119.   
  120.     //  clear()   
  121.     {   
  122.         strMap.clear();   
  123.         cout << "After Clear: " << endl;   
  124.         PrintMap(strMap);   
  125.   
  126.         cout << "Map size: " << strMap.size() << endl;   
  127.         cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;   
  128.     }   
  129.   
  130.     system("pause");   
  131. }  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值