zoukankan      html  css  js  c++  java
  • std::equal_range

    std::equal_range - huycwork的日志 - 网易博客

    std::equal_range   

    2010-04-19 21:03:35|  分类: CPPREFERENCE私房 |  标签:  |字号 订阅

    equal_range

    原型:

     #include <algorithm>
     pair
    <forward_iterator,forward_iterator> equal_range( forward_iterator first, forward_iterator last, const TYPE& val );
     pair
    <forward_iterator,forward_iterator> equal_range( forward_iterator first, forward_iterator last, const TYPE& val, CompFn comp );

    函数equal_range()返回first和last之间等于val的元素区间. 此函数假定first和last区间内的元素可以使用<操作符或者指定的comp执行比较操作.

    equal_range()可以被认为是lower_boundupper_bound的结合, pair中的第一个迭代器由lower_bound返回, 第二个则由upper_bound返回.

    例如, 下面的代码使用equal_range()探测一个有序的vector中的可以插入数字8的位置:

     vector<int> nums;
     nums.push_back( -242 );
     nums.
    push_back( -1 );
     nums.push_back( 0 );
     nums.
    push_back( 5 );
     nums.
    push_back( 8 );
     nums.
    push_back( 8 );
     nums.push_back( 11 );
     pair<vector<int>::iterator, vector<int>::iterator> result;
     int new_val = 8;  
     result
    = equal_range( nums.begin(), nums.end(), new_val );
    cout
     << "The first place that " << new_val
     
    << " could be inserted is before " << *result.first
     
    << ", and the last place that it could be inserted is before " << *result.second
     
    << endl;

    上面的代码将产生如下输出:

     The first place that 8 could be inserted is before 8, and the last place that it could be inserted is before 11

    相关主题: binary_search, lower_bound, upper_bound

  • 相关阅读:
    git 管理
    SVN 管理
    为什么要做静态库
    Sqlite3
    CocoaPod
    内存管理
    readline的用法
    join合并字符串时使用生成器表达式
    pandas DataFrame数据转为list
    Jenkins自动化CI CD流水线之4--Master-Slave架构
  • 原文地址:https://www.cnblogs.com/lexus/p/2854756.html
Copyright © 2011-2022 走看看