lower_bound、upper_bound、equal_range定义在头文件
同时,有序容器如set,map也会提供成员函数lower_bound、
函数声明如下:
ForwardIterator lower_bound(ForwardIterator beg,FowardIterator end,const T&value);
ForwardIterator lower_bound(ForwardIterator beg,FowardIterator end,const T&value,BinaryPredicate op);
ForwardIterator upper_bound(ForwardIterator beg,FowardIterator end,const T&value);
ForwardIterator upper_bound(ForwardIterator beg,FowardIterator end,const T&value,BinaryPredicate op);
pair<ForwardIterator,ForwardIterator> equal_range(ForwardIterator beg,FowardIterator end,const T&value);
pair<ForwardIterator,ForwardIterator> equal_range(ForwardIterator beg,FowardIterator end,const T&value,BinaryPredicate op);
lower_bound()返回第一个大于等于value的元素位置,若无,则返回end。
lower_bound()返回第一个大于value的元素位置,若无,则返回end。
op是可选的排序准则,即op(elem1,elem2);
pair为定义于C++
equal_range()与make_pair(lower_bound(…),upper_bound(…))等效。其中make_pair以lower_bound和upper_bound为参数,返回一个pair结构体。
下面展示lower_bound和upper_bound的使用:
#include
#include
#include
using namespace std;
template<typename T>
void printstl(T a)
{
for (auto& elem : a)
{
cout << elem << " ";
}
cout << endl;
}
int main()
{
//freopen("input.txt", "r", stdin);
vector<int>coll;
for (int i = 0; i < 9; ++i)
{
coll.push_back(i + 1);
coll.push_back(i + 1);
}
printstl(coll);
auto pos1 = lower_bound(coll.begin(), coll.end(), 5);
auto pos2 = upper_bound(coll.begin(), coll.end(), 5);
//distance(it1,it2)计算两个迭代器间的距离,比如,distance(coll.begin(),coll.end())则等于coll元素的个数。
cout << "5 could get position " << distance(coll.begin(), pos1) + 1 << " up to " << distance(coll.begin(), pos2) + 1
<< " without breaking the sorting" << endl;
coll.insert(lower_bound(coll.begin(), coll.end(), 3), 3);//不打乱顺序地插入3
coll.insert(upper_bound(coll.begin(), coll.end(), 7), 7);//不打乱顺序地插入7
printstl(coll);
}
输出如下:
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
5 could get position 9 up to 11 without breaking the sorting
1 1 2 2 3 3 3 4 4 5 5 6 6 7 7 7 8 8 9 9
lower_bound、upper_bound、equal_range也有对应的成员函数。成员函数的实现通常比通用算法快。
#include
#include
using namespace std;
int main()
{
//freopen("input.txt", "r", stdin);
set<int>c;
c.insert(1);
c.insert(2);
c.insert(4);
c.insert(5);
c.insert(6);
cout << "lower_bound(3): " << *c.lower_bound(3) << endl;
cout << "upper_bound(3): " << *c.upper_bound(3) << endl;
cout << "equal_range(3): " << *c.equal_range(3).first << " " << *c.equal_range(3).second << endl;
cout << endl;
cout << "lower_bound(5): " << *c.lower_bound(5) << endl;
cout << "upper_bound(5): " << *c.upper_bound(5) << endl;
cout << "equal_range(5): " << *c.equal_range(5).first << " " << *c.equal_range(5).second << endl;
cout << endl;
if (c.lower_bound(8) == c.end())
cout << "lower_bound(8) not found. Return c.end()" << endl;
if (c.upper_bound(8) == c.end())
cout << "upper_bound(8) not found. Return c.end()" << endl;
if (c.equal_range(8).first == c.end())
{
cout << "equal_range(8) not found. Return c.end()" << endl;
}
cout << endl;
}
输出结果如下:
lower_bound(3): 4
upper_bound(3): 4
equal_range(3): 4 4
lower_bound(5): 5
upper_bound(5): 6
equal_range(5): 5 6
lower_bound(8) not found. Return c.end()
upper_bound(8) not found. Return c.end()
equal_range(8) not found. Return c.end()