[OI] C ++ STL preliminary sorting and retrieval

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.
Sorted By: fast row . The time complexity is n * log2 (n), the higher the efficiency.
Default ascending order (CMP parameter is less <> ())
To achieve the descending order, the default is not CMP, fill greater <data type> () (descending means)
The target array a [n] sorted in ascending order, with this: Sort (A, + n-A);
(Understood that: a first address of the target array, n-number of, i.e., from start to finish.)
Vector for the target container, using: Sort (v.begin (), v.end ());
Further, if the data type to give the self-defined ordering, we need to be defined (overload) <operator, and of passing cmp.

Second, the search (bound class)
Remember the premise of the search is finished sorting an array! ! (The principle is binary search)
To quote the article CSDN bloggers:

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.

Guess you like

Origin www.cnblogs.com/nowonder/p/STL_search.html
Recommended