lower_bound C++

The $\texttt{lower_bound}$ function finds the first position where a value could be inserted in without breaking sorter order. Its time complexity is $\texttt{O(log n)}$.

For example:

vector<int> v = {1, 3, 3, 5, 7};

auto it = lower_bound(v.begin(), v.end(), 3); // it will point to 3

$\texttt{it}$ refers to the address of the value. $\texttt{*it}$ refers to the value, and to get the index, use $\texttt{it-v.begin()}$.

Leave a Reply