DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK

# bin_search(3C++)

bin_search -- search a sorted array for an element with a given value

## Synopsis

```   template <class T>
const T* bin_search(
const T& val,
const T* b,
const T* e
);
template <class T>
const T* bin_search_r(
int (*rel)(const T)*,
const T)*),
const T& val,
const T* b,
const T* e
);
```

## Assumptions

(1)
For the plain version, T::operator< defines a total ordering relation on T and the array is sorted w.r.t. that relation.

(2)
For the relational version, rel defines a total ordering relation on T and the array is sorted w.r.t. that relation.

## Description

These functions find the rightmost element in a sorted array equal to val and return a pointer to it. They return 0 if no such value exists.

```   template <class T>
const T* bin_search(
const T& val,
const T* b,
const T* e
);
```

Uses T::operator< to find the element.

```   template <class T>
const T* bin_search_r(
int (*rel)(const T)*, const T)*),
const T& val,
const T* b,
const T* e
);
```

Uses rel to find the element.

## Complexity

If N is the size of the array, then complexity is O(lgN). At most lgN tests of the relation are done.

## Notes

Because a Block (see Block(3C++)) can always be used wherever an array is called for, Array Algorithms can also be used with Blocks. In fact, these two components were actually designed to be used together.

## References

Array_alg(3C++), Block(3C++)