STD::在C++ STL中向量的上下界和STD::下限

点击此处查看 第一组 第二组 向量的数量。

null

向量- 上界 下界

迭代器下界(迭代器优先、迭代器最后、常量值) 迭代器上界(迭代器优先、迭代器最后、常量值) lower_bound返回一个迭代器,该迭代器指向[first,last]范围内具有值的第一个元素 小于“val”。

如果向量中不存在该值,则返回结束迭代器。 upper_bound返回一个迭代器,该迭代器指向[first,last]范围内的第一个元素,该元素的值大于“val”。

CPP

// lower_bound and upper_bound in vector
#include <algorithm> // for lower_bound, upper_bound and sort
#include <iostream>
#include <vector> // for vector
using namespace std;
int main()
{
int gfg[] = { 5, 6, 7, 7, 6, 5, 5, 6 };
vector< int > v(gfg, gfg + 8); // 5 6 7 7 6 5 5 6
sort(v.begin(), v.end()); // 5 5 5 6 6 6 7 7
vector< int >::iterator lower, upper;
lower = lower_bound(v.begin(), v.end(), 6);
upper = upper_bound(v.begin(), v.end(), 6);
cout << "lower_bound for 6 at position "
<< (lower - v.begin() + 1) << '' ;
cout << "upper_bound for 6 at position "
<< (upper - v.begin() + 1) << '' ;
return 0;
}


输出:

lower_bound for 6 at position 4upper_bound for 6 at position 7

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享