STL algorithm算法equal_range(13)

原文地址:http://www.cplusplus.com/reference/algorithm/equal_range/

function template
<algorithm>

std::equal_range

default (1)
template <class ForwardIterator, class T>
  pair<ForwardIterator,ForwardIterator>
    equal_range (ForwardIterator first, ForwardIterator last, const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>
  pair<ForwardIterator,ForwardIterator>
    equal_range (ForwardIterator first, ForwardIterator last, const T& val,
                  Compare comp);
Get subrange of equal elements
Returns the bounds of the subrange that includes all the elements of the range [first,last) with values equivalent to val.
返回一个pair,表示和val相等的元素的范围。

The elements are compared using operator< for the first version, and comp for the second. Two elements, a and b are considered equivalent if (!(a<b) && !(b<a)) or if (!comp(a,b) && !comp(b,a)).
该函数使用operator<来比较。相等的条件为(!(a<b) && !(b<a))为真。

The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val.
范围内的元素应该是已经排序的。

If val is not equivalent to any value in the range, the subrange returned has a length of zero, with both iterators pointing to the nearest value greater than val, if any, or to last, if val compares greater than all the elements in the range.
如果没有任何一个匹配,那么返回的范围长度为0,返回的迭代器均指向比value大一点的值。
例子:
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;

void equalrange(){
    vector<int> vi{1,5,7,8,9,9,8,5,9};
    array<int,4> ai{1,5,7,8};

    sort(vi.begin(),vi.end());
    cout<<"vi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;
    auto ev=equal_range(vi.begin(),vi.end(),5);
    cout<<"ev.first="<<*ev.first<<"   ,ev.second="<<*ev.second<<endl;
    cout<<"ev elements is :";
    for_each(ev.first,ev.second,[](int i){cout<<i<<" ";});
    cout<<endl;


    cout<<"ai=";
    for(int &i:ai)
        cout<<i<<" ";
    cout<<endl;

    auto ea=equal_range(ai.begin(),ai.end(),6);
    cout<<"ea.first="<<*ea.first<<"   ,ea.second="<<*ea.second<<endl;
    cout<<"ea elements is :";
    for_each(ea.first,ea.second,[](int i){cout<<i<<" ";});
    cout<<endl;



}

运行截图:


The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template <class ForwardIterator, class T>
  pair<ForwardIterator,ForwardIterator>
    equal_range (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it = std::lower_bound (first,last,val);
  return std::make_pair ( it, std::upper_bound(it,last,val) );
}



Parameters

first, last
Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
比较的范围。
val
Value of the subrange to search for in the range.
For (1)T shall be a type supporting being compared with elements of the range [first,last) as either operand of operator<.
要比较的值。
comp
Binary function that accepts two arguments of the type pointed by ForwardIterator (and of type T), and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
一个二元函数比较器。

Return value

pair object, whose member pair::first is an iterator to the lower bound of the subrange of equivalent values, andpair::second its upper bound.
The values are the same as those that would be returned by functions lower_bound and upper_bound respectively.
返回一个pair对象,第一个成员first是指向范围内第一个匹配元素的位置,second指向匹配的最后一个元素的下一个位置。 返回值分别和 lower_bound 和upper_bound 一致。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// equal_range example
// equal_range example
#include <iostream>     // std::cout
#include <algorithm>    // std::equal_range, std::sort
#include <vector>       // std::vector

bool mygreater (int i,int j) { return (i>j); }

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);                         // 10 20 30 30 20 10 10 20
  std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;

  // using default comparison:
  std::sort (v.begin(), v.end());                              // 10 10 10 20 20 20 30 30
  bounds=std::equal_range (v.begin(), v.end(), 20);            //          ^        ^

  // using "mygreater" as comp:
  std::sort (v.begin(), v.end(), mygreater);                   // 30 30 20 20 20 10 10 10
  bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); //       ^        ^

  std::cout << "bounds at positions " << (bounds.first - v.begin());
  std::cout << " and " << (bounds.second - v.begin()) << '\n';

  return 0;
}


Output:
bounds at positions 2 and 5

Complexity

On average, up to twice logarithmic in the distance between first and last: Performs approximately 2*log2(N)+1 element comparisons (where N is this distance).
On non-random-access iterators, the iterator advances produce themselves an additional up to twice linear complexity in N on average.

Data races

The objects in the range [first,last) are accessed.

Exceptions

Throws if either an element comparison or an operation on an iterator throws.
Note that invalid arguments cause undefined behavior.


——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-11

于GDUT

——————————————————————————————————————————————————————————————————




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值