Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

multiset upper_bound() ใน C++ STL

คำอธิบาย

upper_bound(key) คืน iterator ชี้ไปยัง element แรกที่ มากกว่า key อย่างเคร่งครัด หากไม่มี จะคืน end()

ตัวอย่าง

#include <bits/stdc++.h>
using namespace std;

int main() {
    multiset<int> ms = {1, 3, 3, 4, 5};
    
    cout << "Elements: ";
    for(int x : ms) cout << x << " ";
    cout << endl;
    
    auto it = ms.upper_bound(3);
    cout << "upper_bound(3): " << *it << endl; // 4 (ตัวแรกที่ > 3)
    
    it = ms.upper_bound(2);
    cout << "upper_bound(2): " << *it << endl; // 3
    
    it = ms.upper_bound(10);
    cout << "upper_bound(10): " << (it == ms.end() ? "end()" : to_string(*it)) << endl;
    
    return 0;
}

เอาต์พุต

Elements: 1 3 3 4 5
upper_bound(3): 4
upper_bound(2): 3
upper_bound(10): end()