#include <iostream> #include <vector> #include <algorithm> using namespace std; class Print { public: void operator()(int i) { cout << i << endl; } }; int main() { vector<int> v; for(int i = 0; i < 10; i++) { v.push_back(i); } bool ret = binary_search(v.begin(), v.end(), 5); if(ret == true) { cout << "找到了" << endl; } else { cout << "未找到" << endl; } return 0; }
$ ./a.out 找到了
注:二分查找法效率很高,但查找的容器中元素必须是有序序列