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 contains:";
   for (set<int>::iterator it = myset.begin(); it!=myset.end(); ++it)
   cout << ' ' << *it;
}

輸出

myset contains: 10 20 30 40 50 60 80 90

更新於: 30-Dec-2019

116 次瀏覽

開啟您的職業生涯

修完課程獲得認證

開始學習
廣告
© . All rights reserved.