zoukankan      html  css  js  c++  java
  • STL-multimap

    转自:http://www.cnblogs.com/xiaoka/archive/2011/08/09/2132342.html

    multimap提供了可以一种可以有重复键值的STL map类型。其插入方式和map相似,但是由于可以拥有重复键值所以在查找方面有些不同。

    查找

    1. 直接找到每种键值的所有元素的第一个元素的游标

    通过函数:lower_bound( const keytype& x ), upper_bound( const keytype& x ) 可以找到比指定键值x的小的键值的第一个元素和比指定键值x大的键值的第一个元素。返回值为该元素的游标。

    细节:当到达键值x已经是最大时,upper_bound返回的是这个multimap的end游标。同理,当键值x已经是最小了,lower_bound返回的是这个multimap的begin游标。

     2. 指定某个键值,进行遍历

    可以使用上面的lower_bound和upper_bound函数进行游历,也可以使用函数equal_range。其返回的是一个游标对。游标对pair::first是由函数lower_bound得到的x的前一个值,游标对pair::second的值是由函数upper_bound得到的x的后一个值。

    样例如下:

     1 multimap<int,int> a;
     2 a.insert(pair<int,int>(1,11));
     3 a.insert(pair<int,int>(1,12));
     4 a.insert(pair<int,int>(1,13));
     5 a.insert(pair<int,int>(2,21));
     6 a.insert(pair<int,int>(2,22));
     7 a.insert(pair<int,int>(3,31));
     8 a.insert(pair<int,int>(3,32));
     9 
    10 multimap<int,int>::iterator p_map;
    11 pair<multimap<int,int>::iterator, multimap<int,int>::iterator> ret;
    12 
    13 for(p_map = a.begin() ; p_map != a.end();)
    14 {
    15     cout<<p_map->first<<" =>";
    16     ret = a.equal_range(p_map->first);
    17     for(p_map = ret.first; p_map != ret.second; ++p_map)
    18         cout<<""<< (*p_map).second;
    19     cout<<endl;
    20 }

    结果:

    1 => 11 12 13
    2 => 21 22
    3 => 31 32

  • 相关阅读:
    反弹连接的shellcode
    md5反查网站
    一种新的Heap区溢出技术分析
    ckeditor漏洞
    Fedora Core 4下的一些小问题
    MYCCL特征码定位器详细使用之内存定位
    dedecms5.7最新注入和上传漏洞
    脱壳方法汇总
    一种小堆(heap)溢出的另类利用方法
    对付非法操作! 系统做了如下记录!
  • 原文地址:https://www.cnblogs.com/chris-cp/p/3738500.html
Copyright © 2011-2022 走看看