在本教程中,我们将讨论一个程序,以了解C ++ STL中的多集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