多重集 upper_bound() 和 C++ STL 的示例


在本教程中,我們將討論一個程式來理解 C++ STL 中的多重集 upper_bound()。

函式 upper_bound() 返回一個元素的指標,該元素大於作為引數提供的那個元素,否則它返回容器中最後一個元素的指標。

示例

 實際演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   multiset<int> s;
   s.insert(1);
   s.insert(3);
   s.insert(3);
   s.insert(5);
   s.insert(4);
   cout << "The multiset elements are: ";
   for (auto it = s.begin(); it != s.end(); it++)
      cout << *it << " ";
   auto it = s.upper_bound(3);
   cout << "\nThe upper bound of key 3 is ";
   cout << (*it) << endl;
   it = s.upper_bound(2);
   cout << "The upper bound of key 2 is ";
   cout << (*it) << endl;
   it = s.upper_bound(10);
   cout << "The upper bound of key 10 is ";
   cout << (*it) << endl;
   return 0;
}

輸出

The multiset elements are: 1 3 3 4 5
The upper bound of key 3 is 4
The upper bound of key 2 is 3
The upper bound of key 10 is 5

更新於:2020 年 4 月 6 日

231 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

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