参考《C++ Primer》
//所有容器适用(O(log(n))) 已序区间查找算法
lower_bound() //找第一个符合的元素,返回位置迭代器,返回val出现的第一个位置
upper_bound() //找最后一个符合的元素,返回位置迭代器,返回Val出现的最后一个位置的下一个位置
equal_range() //找一对迭代器pair(<>,<>),等效于lower_bound()和upper_bound().
关联式容器有等效的成员函数,性能更佳
#include
#include
#include
#include
#include
using namespace std;
/*************************************************************************************
std::lower_bound 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value );
template
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
//eg:
template
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
const T& value );
template
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
//eg:
template
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (!(value<*it)) // or: if (!comp(value,*it)), for the comp version
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
*************************************************************************************/
/*************************************************************************************
std::equal_range 所有排序容器适用 algorithm
--------------------------------------------------------------------------------------
template
pair
equal_range ( ForwardIterator first, ForwardIterator last, const T& value );
template
pair
equal_range ( ForwardIterator first, ForwardIterator last, const T& value,
Compare comp );
//eg:
template
pair
equal_range ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it = lower_bound (first,last,value);
return make_pair ( it, upper_bound(it,last,value) );
}
*************************************************************************************/
bool mygreater (int i,int j){ return (i>j); }
int main()
{
int myints[] = {10,20,30,30,20,10,10,20};
vector v(myints,myints+8); // 10 20 30 30 20 10 10 20
vector::iterator low,up;
sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
cout<<"10 10 10 20 20 20 30 30\n";
low=lower_bound (v.begin(), v.end(), 20); // ^
up= upper_bound (v.begin(), v.end(), 20); // ^
cout << "20 lower_bound at position " << int(low- v.begin()) + 1 << endl;
cout << "20 upper_bound at position " << int(up - v.begin()) + 1 << endl;
cout << endl;
/**-------------------------------------------------------------------------------**/
pair::iterator,vector::iterator> bounds;
// using default comparison:
// sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
bounds=equal_range (v.begin(), v.end(), 20); // ^ ^
cout<<"10 10 10 20 20 20 30 30\n";
cout << "20 bounds at positions " << int(bounds.first - v.begin());
cout << " and " << int(bounds.second - v.begin()) << endl;
// using "mygreater" as comp:
sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds=equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^
cout<