C++ Algorithm lower_bound()C++ Algorithm lower_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in an ordered range [first, last) that is not less than (i.e. greater than or equal to) to the specified value val. The first version uses operator < to compare the elements and the second version uses comp function. SyntaxParameterfirst: A forward iterator pointing to the first element in the range to be searched. last: A forward iterator pointing to the past last element in the range to be searched. comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements. val: A value of the lower bound to compare the elements in the range. Return valueIt returns an iterator pointing to the first element of the range that is not less than val or last if no such element is found. ComplexityOn average, complexity is logarithmic in the distance between first and last: performs up to log2 (N) + 1 element comparisons Where N = last - first. Data racesThe object in the range [first, last) are accessed. ExceptionsThis function throws an exception if either an element comparison or an operation on iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of lower_bound(): Output: 4, pos = 2 Example 2Let's see another simple example: Output: lower_bound at position 3 upper_bound at position 6 Example 3Let's see another simple example: Output: 4 4 4 4 found at index 2 Example 4Let's see another simple example: Output: First element which is greater than 'C' is b First element which is greater than 'C' is d All elements are less than 'z'. Next TopicC++ Algorithm |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India