# 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.

## Parameter

first: 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 value

It 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.

## Complexity

On average, complexity is logarithmic in the distance between first and last: performs up to log2 (N) + 1 element comparisons Where N = last - first.

## Data races

The object in the range [first, last) are accessed.

## Exceptions

This function throws an exception if either an element comparison or an operation on iterator throws an exception.

## Example 1

Let's see the simple example to demonstrate the use of lower_bound():

Output:

```4, pos = 2
```

## Example 2

Let's see another simple example:

Output:

```lower_bound at position 3
upper_bound at position 6
```

## Example 3

Let's see another simple example:

Output:

```4 4 4
4 found at index 2
```

## Example 4

Let'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