我有以下程序
int main()
{
int myints[] = {1, 2, 3, 3, 4, 6, 7};
vector<int> v(myints,myints+7);
vector<int>::iterator low,up;
sort (v.begin(), v.end());
low=lower_bound (v.begin(), v.end(), 5); ^
up= upper_bound (v.begin(), v.end(), 20); ^
cout << "lower_bound at position " << int(low- v.begin()) << endl;
cout << "upper_bound at position " << int(up - v.begin()) << endl;
return 0;
}
上面我有以下输出
lower_bound at position 5 upper_bound at position 7 Press any key to continue . . .
我的问题是,在上述情况下,如何检查返回值的上限,没有大于 20 的值?
谢谢!
最佳答案
你只需要检查上限的迭代器是否等于 v.end():
if (up == v.end())
// there is no value greater than your upper bound
有关 upper_bound 的更多信息,请参阅:http://www.cplusplus.com/reference/algorithm/upper_bound/
关于c++ - 检查 STL 排序 vector 中 upper_bound 的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193236/