Xを越える最初のコンテナの要素へのイテレーターを返す
毎回関数オブジェクト使ってたんだけどlower_boundってのがあるんだなって。
lower_boundは"以上(>=)"なんだけど、"より大きい(>)"をやるには同様にupper_boundってのがあるのか。
#include <iostream> #include <vector> #include <algorithm> int main() { //適当なデータ std::vector<double> x = { 1, 3, 5, 7, 8, 9 }; //プレディケード版 struct Pred{ Pred(double x) : x_(x){} bool operator()(double x){ return x > x_; } double x_; }; std::vector<double>::const_iterator it1 = std::find_if(x.begin(), x.end(), Pred(3.3)); std::cout << "3.3を越える最初のデータ:" << *it1 << std::endl; //lower_bound版 std::vector<double>::const_iterator it2 = std::lower_bound(x.begin(), x.end(), 3.3); std::cout << "3.3を越える最初のデータ:" << *it2 << std::endl; return 0; }
3.3を越える最初のデータ:5 3.3を越える最初のデータ:5