码迷,mamicode.com
首页 > 其他好文 > 详细

STL MAP 使用

时间:2015-05-21 22:31:46      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

对于map/multimap 查找可以有多种方式。

  1. find 返回一个iterator 是个pair
  2. count (对于map只可能是1或者0)
  3. lower_bound /upper_bound 返回 小于/大于给定value的iterator
  4. equal_range 返回一个pair 包含 lower 和upper的iterator 如果不存在都是 .end() 只有一个 则 返回的pair.second 是 .end() iterator

对于multimap 遍历查找结果,一般可根据count和上下界的iterator来遍历。

 

 1         multimap<string, string> mmpSS = { { "i", "good" }, { "i", "bad" }, { "you", "good" }, { "you", {"bad"} } };
 2         //根据count的数量 
 3         auto num = mmpSS.count("i");
 4         auto it = mmpSS.find("i");
 5         while (num)        
 6         {
 7             cout << it->second<<endl;
 8             it++;
 9             num--;
10         }
11         //根据iterator区间
12         auto ip = mmpSS.equal_range("you");
13         for (auto i = ip.first; i != ip.second ; i++)
14         {
15             cout << i->second << endl;
16         }

 

STL MAP 使用

标签:

原文地址:http://www.cnblogs.com/lugy/p/4520787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!