vector search by element

Solutions on MaxInterview for vector search by element by the best coders in the world

showing results for - "vector search by element"
Lia
17 Aug 2019
1#include <vector> // vector 
2#include <algorithm> // find 
3#include <iostream> // cout 
4using namespace std;
5int main()
6{
7    vector<int> nums = {1,2,3,4,5,6,7,8,9};
8    bool isSorted = is_sorted(nums.begin(), nums.end());
9    if(isSorted){
10        cout << "Using binary search: " << endl;
11        if(binary_search(nums.begin(), nums.end(), 9))
12            cout << "found it" << endl;
13        else
14            cout << "not here" << endl;
15    }
16    else{
17        cout << "Using std::find";
18        if(std::find(nums.begin(), nums.end(), 9) != nums.end())
19            cout << "found it" << endl;
20        else
21            cout << "not here" << endl;
22    }
23}