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
广告