samedi 30 janvier 2016

Searching a portion of vector in c++

I have a vector which contains unsigned integers in sorted order. Now I want to apply binary search on the vector from a particular position of the vector to the end of it.

For example, my vector is:

vector<unsigned> vec;
vec.push_back(1); vector.push_back(2), vector.push_back(3); 

vector.push_back(4); 
vector.push_back(5); vector.push_back(6); vector.push_back(7); vector.push_back(8); 
vector.push_back(9); vector.push_back(10); vector.push_back(11); vector.push_back(12);

Now, I want to apply binary search on "vec" starting from the 6th position to the size of vector, i.e. I want to binary search on only the vector: starting from pos:6 to pos:11.

I know I can apply std:lower_bound(vec.begin(),vec.end(),9) on the entire vector, but I am not able to understand as to how can I apply it on a portion of the vector. I want to find the position of "9" in the vector vec. I'll greatly appreciate any help on this

Aucun commentaire:

Enregistrer un commentaire