sort
Arranges the elements in a specified range into a non-descending order or according to an ordering criterion specified (指定标准) by a binary predicate (二元谓词)
It generally takes 2 + 1 parameters
- The
1
one being the point of the vector from where the sorting needs tobegin
- The
2
parameter being thelength
up to which we want the vector to get sorted - The
(3)
parameter isoptional
and can be used in cases for comp
By default
, the sort() function sorts the elements in ascending order
If the equivalent elements exist , try stable_sort
instead
It can be called as ‘Introsort’ combine QuickSort \ HeapSort \ InsertionSort
Sort by Vector’s Subscript
|
|
Sort in a Particular order
We can also write our own comparator function and pass it as a third parameter.
This comparator
function also be called as 'binary predicate'
- convertible to
bool
- returns a value whether the passed “first” argument should be placed before the passed “second” argument or not
|
|
Why do we use the term “non-descending” instead of “ascending” in sort algorithm ?
non-ascending
and non-descending
include the possibility of adjacent equal
- for
[1,2,2]
’s attribute isnon-descending
notascending
Also we have these sort below
- sort_heap ( make_heap first ) -> the same usage of sort ( contain non-stable )
- stable_partition -> divide array by comparator with origin order -> same usage of sort
- stable_sort -> same of sort but stable -> slow than sort
swap
exchange two array’s element
|
|
swap_ranges
|
|
unique
Removes duplicate elements that are next to each other in a specified range
|
|
unique_copy
Copies elements from a「source range 」into a「 destination range 」except for the duplicate elements that are next to each other.
|
|
upper_bound
Finds the position of the first element in an ordered range that has a value that’s greater than a specified value.
A binary predicate specifies the ordering criterion (指定排序标准).
|
|
Starting vector v1 = ( -1 0 1 2 3 4 -3 -2 -1 0 ).
Original vector v1 with range sorted by the binary predicate less than is v1 = ( -3 -2 -1 -1 0 0 1 2 3 4 ).
Original vector v2 with range sorted by the binary predicate greater is v2 = ( 4 3 2 1 0 0 -1 -1 -2 -3 ).
Original vector v3 with range sorted by the binary predicate mod_lesser is v3 = ( 0 0 -1 -1 1 -2 2 -3 3 4 ).
The upper_bound in v1 for the element with a value of 3 is: 4.
The upper_bound in v2 for the element with a value of 3 is: 2.
The upper_bound in v3 for the element with a value of 3 is: 4.
对于v1,upper_bound 在 v1 中找到第一个大于3的元素,也就是4
对于v2,由于使用 greater
对于v3,使用自定义的 mod_lesser 谓词进行排序,upper_bound 实际上在查找大于3的第一个元素,即4
Also we have lower_bound
adjacent_find
Searches for two adjacent elements that are either equal or satisfy a specified condition.
|
|
fill
Assigns the same new value to every element in a specified range
|
|
full_n
Assigns a new value to a specified number of elements in a range beginning with a particular element
|
|
is_permutation
Returns true if both ranges contain the same elements, whether or not the elements are in the same order
|
|
Lambda表达式
|
|
- capture 是捕获列表,用于在 lambda 表达式中引入外部变量
- parameters 是函数参数列表
- return_type 是返回类型
- function body 是函数体
Also, we have「 is_function」such as:
- is_heap -> is_heap_until
- is_partitioned
- is_sorted -> is_sorted_until
max
Compare two element \ array \ expression
|
|
max_element
Finds the first occurrence of largest element in a specified range where the ordering criterion may be specified by a binary predicate
|
|
Also, we have min
and min_element
Even, we can use minmax
and minmax_element
which return pair<itn, itx>(min(), max())
merge
Combines all of the elements from two sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate
|
|
next_permutation
Reorders the elements in a range so that the original ordering is replaced by the lexicographically next greater permutation if it exists.
The sense of lexicographically next may be specified with a binary predicate
|
|
nth_element
Partitions a range of elements, correctly locating the nth element of the sequence in the range that meets these criteria: All the elements in front of it are less than or equal to it, and all the elements that follow it are greater than or equal to it
|
|
reverse
|
|
rotate
Exchanges the elements in two adjacent ranges
|
|