C ++中的upper_bound

在这里,我们将看到C ++ STL中的upper_bound()函数。此函数返回一个迭代器,该迭代器指向容器中的第一个元素,该元素被认为位于val之后。语法如下:

iterator upper_bound (const value_type& val);
const_iterator upper_bound (const value_type& val) const;

返回值是一个迭代器,指向容器中被认为位于val之后的第一个元素。

示例

#include <iostream>
#include <set>
using namespace std;
int main () {
   set<int> myset;
   set<int>::iterator itlow,itup;
   for (int i = 1; i < 10; i++) myset.insert(i*10);
   itup = myset.upper_bound (60);
   myset.erase(itup);
   cout << "myset包含:";
   for (set<int>::iterator it = myset.begin(); it!=myset.end(); ++it)
   cout << ' ' << *it;
}

输出结果

myset包含: 10 20 30 40 50 60 80 90