equal_range
给定有序的范围,请查找所有与给定值等效的元素所在的子范围。
template<class ForwardIterator, class Type>
pair<ForwardIterator, ForwardIterator> equal_range(
ForwardIterator first,
ForwardIterator last,
const Type& val
);
template<class ForwardIterator, class Type, class Predicate>
pair<ForwardIterator, ForwardIterator> equal_range(
ForwardIterator first,
ForwardIterator last,
const Type& val,
Predicate comp
);
参数
first
寻址要搜索的范围中的第一个元素的位置的向前迭代器。last
寻址要搜索的范围中最后一个元素的下一位置的向前迭代器。val
在顺序的范围要搜索的值。comp
用户定义的谓词函数对象定义一个元素小于另一个。
返回值
指定包含在搜索范围内的子范围的一对向前迭代器,在该子范围中,所有元素在使用的二进制谓词定义的含义上都等效于 val(或者等效于 comp 或者少于默认)。
如果范围内没有元素与 val 等值,向前迭代器的返回对将相等,并在不打乱范围顺序的情况下在其中能插入 val 的点。
备注
通过算法返回的对的第一个迭代器是 lower_bound,第二个迭代器是 upper_bound。
必须基于提供给 equal_range 的谓词排序该范围。 例如,如果要使用大于号谓词,则范围必须按降序排序。
在由 equal_range 返回的一对迭代器定义的可能为空的子范围中的元素在由谓词定义的意义上与 val 相同。
算法的复杂性与随机访问迭代器是对数关系,而与其他则是线性关系,还与步骤数成比例 (last – first)。
示例
// alg_equal_range.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // greater<int>()
#include <iostream>
#include <string>
using namespace std;
template<class T> void dump_vector( const vector<T>& v, pair< typename vector<T>::iterator, typename vector<T>::iterator > range )
{
// prints vector v with range delimited by [ and ]
for( typename vector<T>::const_iterator i = v.begin(); i != v.end(); ++i )
{
if( i == range.first )
{
cout << "[ ";
}
if( i == range.second )
{
cout << "] ";
}
cout << *i << " ";
}
cout << endl;
}
template<class T> void equal_range_demo( const vector<T>& original_vector, T val )
{
vector<T> v(original_vector);
sort( v.begin(), v.end() );
cout << "Vector sorted by the default binary predicate <:" << endl << '\t';
for( vector<T>::const_iterator i = v.begin(); i != v.end(); ++i )
{
cout << *i << " ";
}
cout << endl << endl;
pair< vector<T>::iterator, vector<T>::iterator > result
= equal_range( v.begin(), v.end(), val );
cout << "Result of equal_range with val = " << val << ":" << endl << '\t';
dump_vector( v, result );
cout << endl;
}
template<class T, class F> void equal_range_demo( const vector<T>& original_vector, T val, F pred, string predname )
{
vector<T> v(original_vector);
sort( v.begin(), v.end(), pred );
cout << "Vector sorted by the binary predicate " << predname << ":" << endl << '\t';
for( typename vector<T>::const_iterator i = v.begin(); i != v.end(); ++i )
{
cout << *i << " ";
}
cout << endl << endl;
pair< typename vector<T>::iterator, typename vector<T>::iterator > result
= equal_range( v.begin(), v.end(), val, pred );
cout << "Result of equal_range with val = " << val << ":" << endl << '\t';
dump_vector( v, result );
cout << endl;
}
// Return whether absolute value of elem1 is less than absolute value of elem2
bool abs_lesser( int elem1, int elem2 )
{
return abs(elem1) < abs(elem2);
}
// Return whether string l is shorter than string r
bool shorter_than(const string& l, const string& r)
{
return l.size() < r.size();
}
int main()
{
vector<int> v1;
// Constructing vector v1 with default less than ordering
for( int i = -1; i <= 4; ++i )
{
v1.push_back(i);
}
for( int i =-3; i <= 0; ++i )
{
v1.push_back( i );
}
equal_range_demo( v1, 3 );
equal_range_demo( v1, 3, greater<int>(), "greater" );
equal_range_demo( v1, 3, abs_lesser, "abs_lesser" );
vector<string> v2;
v2.push_back("cute");
v2.push_back("fluffy");
v2.push_back("kittens");
v2.push_back("fun");
v2.push_back("meowmeowmeow");
v2.push_back("blah");
equal_range_demo<string>( v2, "fred" );
equal_range_demo<string>( v2, "fred", shorter_than, "shorter_than" );
}
Output
Vector sorted by the default binary predicate <:
-3 -2 -1 -1 0 0 1 2 3 4
Result of equal_range with val = 3:
-3 -2 -1 -1 0 0 1 2 [ 3 ] 4
Vector sorted by the binary predicate greater:
4 3 2 1 0 0 -1 -1 -2 -3
Result of equal_range with val = 3:
4 [ 3 ] 2 1 0 0 -1 -1 -2 -3
Vector sorted by the binary predicate abs_lesser:
0 0 -1 1 -1 2 -2 3 -3 4
Result of equal_range with val = 3:
0 0 -1 1 -1 2 -2 [ 3 -3 ] 4
Vector sorted by the default binary predicate <:
blah cute fluffy fun kittens meowmeowmeow
Result of equal_range with val = fred:
blah cute fluffy [ ] fun kittens meowmeowmeow
Vector sorted by the binary predicate shorter_than:
fun cute blah fluffy kittens meowmeowmeow
Result of equal_range with val = fred:
fun [ cute blah ] fluffy kittens meowmeowmeow
要求
标头: <算法>
命名空间: std