1沒有第四個參數的方法,
vector<int> array;
int a;
auto result = lower_bound(array.begin(), array.end(),a);
//查找第一個大于等于a的數
auto result = upper_bound(array.begin(), array.end(), a);
//查找第一個大于a的數。
2帶有第四個參數時,這裡其實不帶第四個參數時有預設的第四個參數,用的時<号即如下所示
bool com(const node &a, const node &b){
return a.x < b.x;
}
這裡對結構體進行查找
#include "iostream"
#include "algorithm"
#include "vector"
using namespace std;
struct node{
int x;
int y;
node(int x, int y):x(x),y(y){}
};
bool com(const node &a, const node &b){
return a.x < b.y;
}
int main(){
vector<node> array;
for(int i = 0; i < 9; i++){
array.push_back(node(2*i, i*3));
}
for(auto be = array.begin(); be != array.end(); be++){
cout<<(*be).x<<" "<<(*be).y<<endl;
}
auto result = lower_bound(array.begin(), array.end(), node(10,11), com);
// auto result = upper_bound(array.begin(), array.end(), node(10,11), com);
cout<<(*result).x<<" "<<(*result).y<<endl;
return 0;
}
//第一個auto result傳回的是12,18,com(array, node(10,11)) 12<11
//第二個auto result傳回的是8,12,com(node(10,11), array)) 10<12
//這裡是不同的調用,很重要