Recently, while working on a programming problem in C ++, I came across something interesting. My algorithm used a really large set and would use std :: lower_bound on it many times. However, after submitting my solution, contrary to the math I did on paper to prove that my code was fast enough, it turned out to be too slow. The code looked something like this:
using namespace std; set<int> s; int x;
However, having received a hint from the interlocutor to use set :: lower_bound, the algorithm in question ran waaaaaaay faster than before, and this followed my math. Binary search after change:
set<int>::iterator it = s.lower_bound(x);
My question is what is the difference between the two? Why does one work much faster than the other? Is lower_bound a binary search function that has O (log2 (n)) complexity? In my code, it turned out to be slower than that.
source share