Open1

lower_bound, upper_bound の復習用

log5log5

まだまだ覚えたてなので定期的に忘れる。
なので、書いて、動かして、思い出す。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()

int main() {

    vector<int> arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
//    vector<int> arr = {2, 3, 5, 7, 7, 13, 23, 23, 23, 29 };
//    vector<int> arr = { 8,8,8,8,8,16,16,16,16,16 };
//    vector<int> arr = { 9,9,9,9,9,9,9,9,9,9 };

    rep(i, 31) {
        int tar = i;
        __gnu_cxx::__normal_iterator<int *, vector<int>> lb = lower_bound(all(arr), tar);
        __gnu_cxx::__normal_iterator<int *, vector<int>> ub = upper_bound(all(arr), tar);
        string lbs = lb == arr.end() ? "end()" : to_string(*lb);
        string ubs = ub == arr.end() ? "end()" : to_string(*lb);

        cout << "finding: " << tar << endl;
        cout << "lower bound pos(val): " << lb - arr.begin() << " (" << lbs << ")" << endl;
        cout << "upper bound pos(val): " << ub - arr.begin() << " (" << ubs << ")" << endl;
    }

    return 0;
}