Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are these 2 exactly same –> "lower_bound(inc.rbegin(), inc.rend(), i , greater<int>())" & "lower_bound(dec.begin(), dec.end(), i, greater<int>())"

If i have say 2 vectors, one in increasing order, and other decreasing
vector<int> inc{1,2,3,4,6,7}, dec{7,6,4,3,2,1};

Then do these 2 always give the same result…or is there any difference in how they work:-

lower_bound(inc.rbegin(), inc.rend(), some_number, greater<int>()) // #1

and

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lower_bound(dec.begin(), dec.end(), some_number, greater<int>()) // #2

I feel they are the same but during a recent content on Codeforces, one got accepted while the other didn’t.

>Solution :

From the algorithm’s, std::lower_bounds‘s, perspective, there is no differance other than the iterator type (one is a reverse iterator and the other one is not), but it doesn’t care about that.

It will perform the same algorithmic steps in both cases and return an iterator pointing at an element that carries the same value in both cases – or the end()/rend() iterator.

One minor difference could be in speed. I’d expect the version using reverse iterators to be a tad slower, but I haven’t measured.

So, other than the difference in the returned iterator type and possibly one being slower than the other, you will get the same value out of both versions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading