📜  按元素进行向量搜索 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:47.285000             🧑  作者: Mango

代码示例1
#include  // vector 
#include  // find 
#include  // cout 
using namespace std;
int main()
{
    vector nums = {1,2,3,4,5,6,7,8,9};
    bool isSorted = is_sorted(nums.begin(), nums.end());
    if(isSorted){
        cout << "Using binary search: " << endl;
        if(binary_search(nums.begin(), nums.end(), 9))
            cout << "found it" << endl;
        else
            cout << "not here" << endl;
    }
    else{
        cout << "Using std::find";
        if(std::find(nums.begin(), nums.end(), 9) != nums.end())
            cout << "found it" << endl;
        else
            cout << "not here" << endl;
    }
}