If all the values in vector are less than or equal to M then "i" will point to v.end().
auto i = upper_bound(v.begin(), v.end(), M)- v.begin();
Now if I want to do certain action if "i" points to the end of vector, I tried
if(i == v.end()){ // Code }
But it doesn't work, throwing error: no match for ‘operator==’ (operand types are ‘long int’ and ‘std::vector::iterator’)
You should just write auto i = upper_bound(v.begin(), v.end(), M). If you subtract v.begin() from it what you'll get will be the index corresponding to the iterator you wanted.
Use
auto i = upper_bound(v.begin(), v.end(), M);
is a way mentioned above. In the statementi
is avector<int>::iterator
.And you can use
if(i == v.size()){ // Code }
too. Herev.size()
is equal tov.end() - v.begin()
.define i as long long or int
if its pointing at end that means its the largest element of vector or what data structure you are using
Yeah, you can use another way but it doesn't matter
int
orauto
is here.Yeah its correct