Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

multiset upper_bound() trong C++ STL với ví dụ


Trong hướng dẫn này, ta thảo luận về multiset upper_bound() trong C++ STL.

Hàm upper_bound() trả về iterator trỏ đến phần tử đầu tiên lớn hơn key cho trước, hoặc end() nếu không có.

Ví dụ

#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 " << (*it) << endl; it = s.upper_bound(2); cout << "The upper bound of key 2 is " << (*it) << endl; it = s.upper_bound(10); cout << "The upper bound of key 10 is " << (*it) << endl; return 0; }

Đầu ra

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