当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ Algorithm equal_range()用法及代码示例


C++ 算法 equal_range() 函数是二分查找的版本。该函数用于返回子范围的下界和上界,该子范围包括范围[first, last)中所有等价于val的元素。

其中子范围由两个迭代器定义,一个指向不小于 val 的第一个元素,另一个指向大于 val 的第一个元素。

  • 第一个版本使用运算符 < 来比较元素,第二个版本使用给定的比较函数,即 comp。
  • 范围 [first, last) 必须根据与 val 的比较进行分区,即它必须满足以下所有条件:
    • 相对于 element < val 或 comp(element, val) 进行分区
    • 相对于 !(val < element) 或 !comp(val, element) 进行分区
    • 对于所有元素,如果 element < val 或 comp(element, val) 为真,则 !(val < element) 或 !comp(val, element) 也为真。

用法

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);

参数

first:指向要搜索的范围中的第一个元素的前向迭代器。

last:指向要搜索范围中过去最后一个元素的前向迭代器。

补偿: 一个用户定义的二元谓词函数,它接受两个参数,如果两个参数按顺序返回真,否则返回假。它遵循严格的弱排序来对元素进行排序。

val: 用于比较范围内元素的上限值。

返回值

它返回两个迭代器,一个指向第一个不小于 val 的元素,另一个指向第一个大于 val 的元素。

如果没有找到这样的元素,那么它最后返回。

复杂度

平均而言,复杂性在 first 和 last 之间的距离上是对数的:最多执行 2*log2 (N) + 1 次元素比较,其中 N = last - first。

数据竞争

访问范围 [first, last) 中的对象。

异常

如果元素比较或迭代器上的操作引发异常,则此函数将引发异常。

请注意,无效参数会导致未定义的行为。

例子1

让我们看一个简单的例子来演示 equal_range() 的使用:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {3, 1, 4, 2, 5};

  sort(v.begin(), v.end());

  auto result = equal_range(v.begin(), v.end(), 3);

  cout << "Lower Bound of 3 is:"<<*result.first << endl;
  cout << "Upper Bound of 3 is:"<<*result.second << endl;
  
  return 0;
}

输出:

Lower Bound of 3 is:3
Upper Bound of 3 is:4

例子2

让我们看另一个使用 operator< 比较元素的简单示例:

#include <algorithm>
#include <vector>
#include <iostream>
 
using namespace std;
 
struct S
{
    int number;
    char name;
 
    S ( int number, char name  )
       :number ( number ), name ( name )
    {}
 
    // only the number is relevant with this comparison
    bool operator< ( const S& s ) const
    {
        return number < s.number;
    }
};
 
 
int main()
{
    // note:not ordered, only partitioned w.r.t. S defined below
    vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} };
 
    S value ( 2, '?' );
 
    auto p = equal_range(vec.begin(),vec.end(),value);
 
    for ( auto i = p.first; i != p.second; ++i )
        cout << i->name << ' ';
        
        return 0;
}

输出:

B C D

在上面的例子中,运算符 < 用于比较元素并返回等于 2 的范围内的所有元素。

例子3

让我们看另一个使用比较函数比较元素的简单示例:

#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;
 
struct S
{
    int number;
    char name;
 
    S ( int number, char name  )
       :number ( number ), name ( name )
    {}
 
    // only the number is relevant with this comparison
    bool operator< ( const S& s ) const
    {
        return number < s.number;
    }
};
 
struct Comp
{
    bool operator() ( const S& s, int i )
    {
        return s.number < i;
    }
 
    bool operator() ( int i, const S& s )
    {
        return i < s.number;
    }
};
 
int main()
{
    // note:not ordered, only partitioned w.r.t. S defined below
    vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} };
 
    auto p = equal_range(vec.begin(),vec.end(),2,Comp());
 
    for ( auto i = p.first; i != p.second; ++i )
        cout << i->name << ' ';
        
        return 0;
}

输出:

B C D

示例 4

让我们看另一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a[] = {2, 3, 5, 6, 7, 7, 7,  8, 9, 10};
  vector<int> v(a, a+10);
  cout <<"\nHere are the contents of v:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  pair<vector<int>::iterator, vector<int>::iterator> bounds;
 
  bounds = equal_range(v.begin(), v.end(), 3);
  if (bounds.first != v.end())
    cout <<"\nLower bound of 3 in v = "<<*bounds.first;
  if (bounds.first != v.end())
    cout <<"\nUpper bound of 3 in v = "<<*bounds.second;
 
  bounds = equal_range(v.begin(), v.end(), 4);
  if (bounds.first != v.end())
    cout <<"\nLower bound of 4 in v = "<<*bounds.first;
  if (bounds.first != v.end())
    cout <<"\nUpper bound of 4 in v = "<<*bounds.second;
 
  bounds = equal_range(v.begin(), v.end(), 5);
  if (bounds.first != v.end())
    cout <<"\nLower bound of 5 in v = "<<*bounds.first;
  if (bounds.first != v.end())
    cout <<"\nUpper bound of 5 in v = "<<*bounds.second;
 
  bounds = equal_range(v.begin(), v.end(), 7);
  if (bounds.first != v.end())
    cout <<"\nLower bound of 7 in v = "<<*bounds.first;
  cout <<"\nThis is the first of the three 7's, since the value "
         "before this 7 is "<<*(bounds.first-1)<<".";
  if (bounds.first != v.end())
    cout <<"\nUpper bound of 7 in v = "<<*bounds.second;
 
  bounds = equal_range(v.begin(), v.end(), 0);
  if (bounds.first != v.end())
    cout <<"\nLower bound of 0 in v = "<<*bounds.first;
  if (bounds.first != v.end())
    cout <<"\nUpper bound of 0 in v = "<<*bounds.second;
 
  bounds = equal_range(v.begin(), v.end(), 15);
  if (bounds.first != v.end())
    cout <<"\nLower bound of 15 in v = "<<*bounds.first;
  if (bounds.first != v.end())
    cout <<"\nUpper bound of 15 in v = "<<*bounds.second;
  cout <<"\nNote that both the lower and upper bound locations "
         "\nof 15 are the end (one-past-the-last) vector position.";
 
  return 0;
}

输出:

Here are the contents of v:
2 3 5 6 7 7 7 8 9 10 
Lower bound of 3 in v = 3
Upper bound of 3 in v = 5
Lower bound of 4 in v = 5
Upper bound of 4 in v = 5
Lower bound of 5 in v = 5
Upper bound of 5 in v = 6
Lower bound of 7 in v = 7
This is the first of the three 7's, since the value before this 7 is 6.
Upper bound of 7 in v = 8
Lower bound of 0 in v = 2
Upper bound of 0 in v = 2
Note that both the lower and upper bound locations 
of 15 are the end (one-past-the-last) vector position. 





相关用法


注:本文由纯净天空筛选整理自 C++ Algorithm equal_range()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。