Purple Book from over, but the contents of the book talking about relatively simple, do a bit of supplementary notes.
A sort (sort function)
Header: <algorithm>
Syntax: the Sort (Start, End, cmp);
start, end must, cmp does not have to.
parameter
(1) start indicates the start address of the array to be sorted;(2) end end of the array represents the next address;Method (3) cmp for sorting a predetermined, time to fill, the default ascending.
Second, the search (bound class)
In small to large sort the array,
lower_bound (begin, end, num): begin from a position in the array to the end-1 position of the first binary search num equal to or greater than a number, the number returned to find the address end does not exist is returned. Begin by subtracting the start address of the return address, to obtain the numerical subscript found in the array.
upper_bound (begin, end, num): begin from a position in the array to the end-1 position of the binary search num greater than the first number, and returns this number to find the address does not exist end returns. Begin by subtracting the start address of the return address, to obtain the numerical subscript found in the array.
In descending sorted array, the overload lower_bound () and upper_bound, ()
lower_bound (begin, end, num, greater <type> ()): from the begin position of the array to the end-1 position of the binary search of a less than or equal to the num number, find returns the digital address, there is no end return . Begin by subtracting the start address of the return address, to obtain the numerical subscript found in the array.
upper_bound (begin, end, num, greater <type> ()): begin from a position in the array to the end-1 position of a first binary search num less than the number of returns that number to find the address end does not exist is returned. Begin by subtracting the start address of the return address, to obtain the numerical subscript found in the array.
---------------------
Author: brandong
Source: CSDN
Original: https: //blog.csdn.net/qq_40160605/article/details/80150252
On lower_bound, he's looking for is the first x is greater than equal to the number, then the situation can not be found even if the return is not necessarily correct. So to return to again head judge it.
int p = lower_bound (a, a + n, x) - a; // index obtained at this time
if (a[p] == x) ....
Probably so, and incomplete. After the encounter, etc. to add.