Horje
vector search by element Code Example
vector search by element
#include <vector> // vector 
#include <algorithm> // find 
#include <iostream> // cout 
using namespace std;
int main()
{
    vector<int> 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;
    }
}




Cpp

Related
string iterator in c++ Code Example string iterator in c++ Code Example
how to send email in c++ program Code Example how to send email in c++ program Code Example
maximum value in map in c++ Code Example maximum value in map in c++ Code Example
set size of a vector c++ Code Example set size of a vector c++ Code Example
differentialble programming Code Example differentialble programming Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7