zoukankan      html  css  js  c++  java
  • c++ 二分法查找(binary_search)

    #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!
    "; else cout << "not found.
    ";
        
        // 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!
    "; else cout << "not found.
    ";
        
        return 0;
    }

  • 相关阅读:
    Spring Boot入门
    Spring MVC文件上传和下载
    Spring MVC异常处理
    SpringMVC
    linux(2)
    linux(1)
    白盒测试
    LoadRunner(8)
    LoadRunner(7)
    LoadRunner(6)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/9815151.html
Copyright © 2011-2022 走看看