C++ STL 中的 multiset lower_bound() 以及示例


在本教程中,我們將討論一段程式,瞭解 C++ STL 中的 multiset lower_bound()。

函式 lower_bound() 返回容器中第一個與給定引數相等的元素,若不存在,則返回緊接在該引數之後的元素。

示例

 演示

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

輸出

The multiset elements are: 1 1 2 2 4
The lower bound of key 2 is 2
The lower bound of key 3 is 4
The lower bound of key 7 is 5

更新於:06-Apr-2020

241 次瀏覽

開啟你的職業生涯

完成本課程即可獲得認證

開始入門
廣告
© . All rights reserved.