#include <iostream>     // cout
#include <algorithm>    // binary_search, sort
#include <vector>       // 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;
}

 

相关文章:

  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-06-19
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2021-10-11
  • 2022-12-23
  • 2021-10-11
  • 2021-05-08
  • 2021-05-28
  • 2021-08-30
  • 2021-06-21
相关资源
相似解决方案