编程语言
首页 > 编程语言> > 查找算法binary_search

查找算法binary_search

作者:互联网

#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       
找到了

注:二分查找法效率很高,但查找的容器中元素必须是有序序列

标签:binary,search,cout,int,ret,查找,include
来源: https://www.cnblogs.com/zhangxuechao/p/16536947.html