# C++ Binary Functions (binary_search, lower_bound, upper_bound) Binary Search is known to all. If you don't know about Binary Search, you can visit this website to learn about Binary Search.
https://blog.csdn.net/SkeletonKing233/article/details/99447270

Some classic binary search topics like templates require long code to be written once at a time, which is a waste of time.To avoid wasting valuable time, the algorithm header file comes up with three binary search functions.

```# include <algorithm>
```

These are the binary_search, lower_bound, and upper_bound functions, which are log2(n) level functions that use binary lookups to complete tasks and are all defined in the algorithm header file.

## binary_search function

```binary_search(a + begin, a + end, k, cmp);
```

Necessary condition: The sequence from a[begin] to a[end-1] is ordered.

Function: Compare the cmp function from a[begin] to a[end-1] in array a to find if there is one number equal to k, and if there is one return value is true, otherwise it is false.

Return value: bool

Parameters:

1. First address (a + begin) required
2. Last address (a + end) required
3. Values that need to be searched are necessary
4. A comparison function represents how a sequence is ordered (in most cases, suitable for searching for structures) selection

## lower_bound function

```lower_bound(a + begin, a + end, k, cmp);
```

Necessary condition: The sequence from a[begin] to a[end-1] is ordered.

Function: From a[begin] to a[end-1] in the array a, do a binary comparison according to the cmp function to find the location of the first number greater than or equal to k, and return the address of the number if the first number greater than or equal to k, otherwise return the address of a[end].

Return value: Address.If converting to int, subtract array a.

Parameters:

1. First address (a + begin) required
2. Last address (a + end) required
3. Values to compare are necessary
4. A comparison function represents how a sequence is ordered (in most cases, suitable for searching for structures) selection

## upper_bound function

```upper_bound(a + begin, a + end, k, cmp);
```

Necessary condition: The sequence from a[begin] to a[end-1] is ordered.

Function: From a[begin] to a[end-1] in the array a, do a binary comparison according to the cmp function to find the location of the first number greater than or equal to k, and return the address of the number if the first number greater than k, otherwise return the address of a[end].

Return value: Address.If converting to int, subtract array a.

Parameters:

1. First address (a + begin) required
2. Last address (a + end) required
3. Values to compare are necessary
4. A comparison function represents how a sequence is ordered (in most cases, suitable for searching for structures) selection

Note: The essential difference between upper_bound and lower_bound is that upper_bound finds the position of the first element greater than k, while lower_bound finds the position of the first element greater than or equal to K.

## Run Instance

For reference only.

```# include <cstdio>
# include <iostream>
# include <cmath>
# include <cstring>
# include <algorithm>
using namespace std;

# define FOR(i, a, b) for(int i = a; i <= b; i++)
# define _FOR(i, a, b) for(int i = a; i >= b; i--)

const int NR = 100;

int n = 6;
int a[NR + 10] = {0, 1, 7, 7, 7, 13, 100};

int main()
{
cout << "a Array from areach a[n]this n Is there 7 out of the number?" << binary_search(a + 1, a + n + 1, 7) << endl;
cout << "a Array from areach a[n]this n 12 out of the number?" << binary_search(a + 1, a + n + 1, 12) << endl;
cout << "a Array from areach a[n]this n Is there zero in the number?" << binary_search(a + 1, a + n + 1, 0) << endl;

cout << "a Array from areach a[n]this n The number of digits in the first number greater than or equal to 10 is" << lower_bound(a + 1, a + n + 1, 10) - a << endl;
cout << "a Array from areach a[n]this n The number of digits in the first number greater than or equal to 13 is" << lower_bound(a + 1, a + n + 1, 13) - a << endl;
cout << "a Array from areach a[n]this n The number of digits in the first number greater than or equal to 7 is" << lower_bound(a + 1, a + n + 1, 7) - a << endl;
cout << "a Array from areach a[n]this n The number of digits in the first number greater than or equal to 10,000 is" << lower_bound(a + 1, a + n + 1, 10000) - a << endl;

cout << "a Array from areach a[n]this n The number of digits in the first number greater than 10 is" << upper_bound(a + 1, a + n + 1, 10) - a << endl;
cout << "a Array from areach a[n]this n The number of digits in the first number greater than 13 is" << upper_bound(a + 1, a + n + 1, 13) - a << endl;
cout << "a Array from areach a[n]this n The number of digits in the first number greater than 7 is" << upper_bound(a + 1, a + n + 1, 7) - a << endl;
cout << "a Array from areach a[n]this n The number of digits in the first number greater than 10,000 is" << upper_bound(a + 1, a + n + 1, 10000) - a << endl;
return 0;
}
```

God Bless You For Ever!

Posted by tisource on Tue, 13 Aug 2019 18:43:45 -0700