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
需求
標頭:<algorithm>
命名空間: std