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

multiset lower_bound() ใน C++ STL

คำอธิบาย

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

ตัวอย่าง

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

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

เอาต์พุต

Elements: 1 1 2 2 4
lower_bound(2): 2
lower_bound(3): 4
lower_bound(7): end()