Блог пользователя srijith1402

Автор srijith1402, история, 4 года назад, По-английски

I was studying std::upper_bound from documentation of c++ and I came across the fact that this might run in linear time on non-random access iterators.

I need to use this for a sorted vector. Now I don't know what are non-random access iterators and whether this will run in logarithmic time on the sorted vector.

Can anyone clear this for me that how to implement lower_bound() on a sorted vector in log(n) time. Thank you

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Vectors have random access iterators, so you can use upper_bound() and lower_bound() with logarithmic time complixities.

»
4 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

A set has non-random iterators, because auf this you should not use

set<int> myset;
...
auto it=lower_bound(myset.begin(), myset.end(), someval);

Because this above does not work good, set has its own version of lower_bound which works in $$$O(logn)$$$

set<int> myset;
...
auto it=myset.lower_bound(someval);