Search Index in Vector

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> v = {1,2,3,3,3,3,3,3,3,3,3,4,5,6,7,8,9,10};

    auto t = lower_bound(v.begin(), v.end(), 3);
    if(t == v.end())
        cout << "not found." << endl << endl;
    else
        cout << t-v.begin() << endl << endl;

    t = upper_bound(v.begin(), v.end(), 3);
    if(t == v.end())
        cout << "not found." << endl;
    else
        cout << t-v.begin()-1 << endl;

    return 0;
}

Comments