现在的位置: 首页 > 综合 > 正文

binary_search

2013年09月17日 ⁄ 综合 ⁄ 共 557字 ⁄ 字号 评论关闭
// binary_search example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j){
     return (i<j);
}

int main () {
  int myints[] = {1,2,3,4,5,4,3,2,1};
  vector<int> v(myints, myints+9);   // 1 2 3 4 5 4 3 2 1

  // using default comparison:
  sort (v.begin(), v.end());
  
  cout << "looking for a 3... ";
  if (binary_search(v.begin(), v.end(), 3)){
    cout << "found!\n";
  }
  else {
    cout << "not found.\n";
  }
  // using myfunction as comp:
  sort (v.begin(), v.end(), myfunction);

  cout << "looking for a 6... ";
  if (binary_search (v.begin(), v.end(), 6, myfunction)){
    cout << "found!\n";
  }
  else {
      cout << "not found.\n";
  }
  return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.