zoukankan      html  css  js  c++  java
  • STL的equal_range()

    equal_range()根据键值,返回一对迭代器的pair对象。

      如果该键值在容器中存在,则pair对象中的第一个迭代器指向该键关联的第一个实例,第二个迭代器指向该键关联的最后一个实例的下一位置。

      如果找不到匹配的元素,则pair对象中的两个迭代器都将指向此键应该插入的位置。

    算法lower_bound返回区间A的第一个迭代器,算法upper_bound返回区间A的最后一个元素的下一个位置,equal_range以pair的形式返回迭代器位置区间[lower_bound, upper_bound);

    例子1:

    #include <iostream>
    #include <map>
    using namespace std;
    int main ()
    {
      map<char,int> mymap;
      pair<map<char,int>::iterator,map<char,int>::iterator> ret;
     
      mymap['a']=10;
      mymap['b']=20;
      mymap['c']=30;
     
      ret = mymap.equal_range('b');
     
      cout << "lower bound points to: ";
      cout << ret.first->first << " => " << ret.first->second << endl;
     
      cout << "upper bound points to: ";
      cout << ret.second->first << " => " << ret.second->second << endl;
      system("pause");
      return 0;
    }

    输出:

    例子2:

    #include <iostream>
    #include <map>
    using namespace std;
    int main()
    {
        std::multimap<char, int> my_multimap;
        my_multimap.insert(std::make_pair('a', 10));
        my_multimap.insert(std::make_pair('a', 20));
        my_multimap.insert(std::make_pair('a', 30));
        my_multimap.insert(std::make_pair('a', 40));
        my_multimap.insert(std::make_pair('b', 10));
        my_multimap.insert(std::make_pair('c', 10));
        my_multimap.insert(std::make_pair('c', 20));
        std::cout << my_multimap.size() << std::endl;
        std::pair<std::multimap<char, int>::iterator, std::multimap<char, int>::iterator> ret;
        ret = my_multimap.equal_range('a');
        std::multimap<char, int>::iterator it = ret.first;
        for (; it != ret.second; it++) {
            std::cout << it->first << " => " << it->second << '
    ';
        }
        cout << "out :" << endl;
        std::cout << it->first << " => " << it->second << '
    ';
        system("pause");
        return 0;
    }

    输出:

  • 相关阅读:
    python 协程之Greenlet
    python 协程
    python 多进程通信之Manger
    python 多线程通信之Queue
    python 多进程
    python threading之queue
    python threading之同步条件(Event)
    python threading之条件变量同步(condition)
    python之字符串常用方法
    python之字典操作
  • 原文地址:https://www.cnblogs.com/Brickert/p/13277453.html
Copyright © 2011-2022 走看看