STL 算法集合

来源:互联网 发布:二极管功耗计算软件 编辑:程序博客网 时间:2024/04/28 02:21

C++ 算法(STL) 

非修改性序列操作(12个)

循环

for_each()

对序列中的每个元素执行某操作

查找

find()

在序列中找出某个值的第一次出现的位置

find_if()

在序列中找出符合某谓词的第一个元素

find_end()

在序列中找出一子序列的最后一次出现的位置

find_first_of()

在序列中找出第一次出现指定值集中之值的位置

adjacent_find()

在序列中找出相邻的一对值

计数

count()

在序列中统计某个值出现的次数

count_if()

在序列中统计与某谓词匹配的次数

比较

mismatch()

找出两个序列相异的第一个元素

equal()

两个序列中的对应元素都相同时为真

搜索

search()

在序列中找出一子序列的第一次出现的位置

search_n()

在序列中找出一值的连续n次出现的位置

修改性序列操作(27个)

复制

copy()

从序列的第一个元素起进行复制

copy_backward()

从序列的最后一个元素起进行复制

交换

swap()

交换两个元素

swap_ranges()

交换指定范围的元素

iter_swap()

交换由迭代器所指的两个元素

变换

transform()

将某操作应用于指定范围的每个元素

替换

replace()

用一个给定值替换一些值

replace_if()

替换满足谓词的一些元素

replace_copy()

复制序列时用一给定值替换元素

replace_copy_if()

复制序列时替换满足谓词的元素

填充

fill()

用一给定值取代所有元素

fill_n()

用一给定值取代前n个元素

生成

generate()

用一操作的结果取代所有元素

generate_n()

用一操作的结果取代前n个元素

删除

remove()

删除具有给定值的元素

remove_if()

删除满足谓词的元素

remove_copy()

复制序列时删除具有给定值的元素

remove_copy_if()

复制序列时删除满足谓词的元素

唯一

unique()

删除相邻的重复元素

unique_copy()

复制序列时删除相邻的重复元素

反转

reverse()

反转元素的次序

reverse_copy()

复制序列时反转元素的次序

环移

rotate()

循环移动元素

rotate_copy()

复制序列时循环移动元素

随机

random_shuffle()

采用均匀分布来随机移动元素

划分

partition()

将满足某谓词的元素都放到前面

stable_partition()

将满足某谓词的元素都放到前面并维持原顺序


序列排序及相关操作(27个)

排序

sort()

以很好的平均效率排序

stable_sort()

排序,并维持相同元素的原有顺序

partial_sort()

将序列的前一部分排好序

partial_sort_copy()

复制的同时将序列的前一部分排好序

第n个元素

nth_element()

将第n各元素放到它的正确位置

二分检索

lower_bound()

找到大于等于某值的第一次出现

upper_bound()

找到大于某值的第一次出现

equal_range()

找到(在不破坏顺序的前提下)可插入给定值的最大范围

binary_search()

在有序序列中确定给定元素是否存在

归并

merge()

归并两个有序序列

inplace_merge()

归并两个接续的有序序列

有序结构上的集合操作

includes()

一序列为另一序列的子序列时为真

set_union()

构造两个集合的有序并集

set_intersection()

构造两个集合的有序交集

set_difference()

构造两个集合的有序差集

set_symmetric_difference()

构造两个集合的有序对称差集(并-交)

堆操作

push_heap()

向堆中加入元素

pop_heap()

从堆中弹出元素

make_heap()

从序列构造堆

sort_heap()

给堆排序

最大和最小

min()

两个值中较小的

max()

两个值中较大的

min_element()

序列中的最小元素

max_element()

序列中的最大元素

词典比较

lexicographical_compare()

两个序列按字典序的第一个在前

排列生成器

next_permutation()

按字典序的下一个排列

prev_permutation()

按字典序的前一个排列

Display all entries for C++ Algorithms on one page, or view entries individually:

accumulate
sum up a range of elements
求和:用初值与指定范围内的元素相加。重载的版本不再做加法,而是传进来的二元操作符被应用到元素上。

adjacent_difference
compute the differences between adjacent elements in a range
创建一个新序列,该序列的每个新值都代表了当前元素与上一个元素的差。重载版本用指定的二元操作计算相邻元素的差。

adjacent_find
finds two items that are adjacent to eachother
在 指定的范围内,查找一对相邻的重复元素,如果找到返回一个 ForwardIterator ,指向这对元素的第一个元素。否则返回 last 。重载版本使用输入的二元操作符代替相等的判断。

binary_search
determine if an element exists in a certain range
折半/二分法查找:在有序序列中查找 value ,如果找到返回 true 。重载的版本使用指定的比较函数对象或者函数指针来判断相等。

copy
copy some range of elements to a new location
复制序列。

copy_backward
copy a range of elements in backwards order
除了元素以相反的顺序被拷贝外,别的和 copy 相同。

copy_n
copy N elements
只复制N个元素。

count
return the number of elements matching a given value
利用等于操作符,把标志范围类的元素与输入的值进行比较,并返回相等元素的个数。

count_if
return the number of elements for which a predicate is true
对于标志范围类的元素,应用输入的操作符,并返回结果为 true 的次数。

equal
determine if two sets of elements are the same
如果两个序列在范围内的元素都相等,则 equal 返回 true 。重载版本使用输入的操作符代替了默认的等于操作符。

equal_range
search for a range of elements that are all equal to a certain element
返回一对 iterator ,第一个 iterator 表示由 lower_bound 返回的 iterator ,第二个表示由 upper_bound 返回的 iterator 值。

fill
assign a range of elements a certain value
填充:将输入的值的拷贝赋给范围内的每个元素。

fill_n
assign a value to some number of elements
填充:将输入的值赋值给 first 到 frist+n 范围内的元素。

find
find a value in a given range
查找:利用底层元素的等于操作符,对范围内的元素与输入的值进行比较。当匹配时,结束搜索,返回该元素的一个 InputIterator 。

find_end
find the last sequence of elements in a certain range
查找:使用输入的函数替代了等于操作符执行了 find 。

find_first_of
search for any one of a set of elements
查找:在范围内查找“由输入的另外一个 iterator 对标志的第二个序列”的最后一次出现。重载版本中使用了用户输入的操作符替代等于操作。

find_if
find the first element for which a certain predicate is true
查找:在范围内查找“由输入的另外一个 iterator 对标志的第二个序列”中的任意一个元素的第一次出现。重载版本中使用了用户自定义的操作符。

for_each
apply a function to a range of elements
依次对范围内的所有元素执行输入的函数。

generate
saves the result of a function in a range
通过对输入的函数 gen 的连续调用来填充指定的范围。

generate_n
saves the result of N applications of a function
填充 n 个元素。

includes
returns true if one set is a subset of another
判断 [first1, last1) 的一个元素是否被包含在另外一个序列中。使用底层元素的 <= 操作符,重载版本使用用户输入的函数。

inner_product
compute the inner product of two ranges of elements
对两个序列做内积 ( 对应的元素相乘,再求和 ) ,并将内积加到一个输入的的初始值上。重载版本使用了用户定义的操作。

inplace_merge
merge two ordered ranges in-place
合并两个排过序的连续序列,结果序列覆盖了两端范围,重载版本使用输入的操作进行排序。

is_heap
returns true if a given range is a heap
判断指定的范围是否是构成一个堆。

is_sorted
returns true if a range is sorted in ascending order
判断指定的范围时候为升序排列。

iter_swap
swaps the elements pointed to by two iterators
交换两个 ForwardIterator 的值。

lexicographical_compare
returns true if one range is lexicographically less than another
比较两个序列(使用less)。重载版本使用了用户自定义的比较操作。

lexicographical_compare_3way
determines if one range is lexicographically less than or greater than another
确定第一个范围的元素,小于或者大于另一个范围的元素。
类似于memcmp的返回规则。

lower_bound
search for the first place that a value can be inserted while preserving order
返回一个 iterator ,它指向在范围内的有序序列中可以插入指定值而不破坏容器顺序的第一个位置。重载函数使用了自定义的比较操作。

make_heap
creates a heap out of a range of elements
把范围内的元素生成一个堆。重载版本使用自定义的比较操作。

max
returns the larger of two elements
返回两个元素中的较大的一个,重载版本使用了自定义的比较操作。

max_element
returns the largest element in a range
返回一个 iterator ,指出序列中最大的元素。重载版本使用自定义的比较操作。

merge
merge two sorted ranges
合并两个有序序列,并存放到另外一个序列中。重载版本使用自定义的比较。

min
returns the smaller of two elements
类似于 max ,不过返回最小的元素。

min_element
returns the smallest element in a range
类似于 max_element ,不过返回最小的元素。

mismatch
finds the first position where two ranges differ
并行的比较两个序列,指出第一个不匹配的位置,它返回一对 iterator ,标志第一个不匹配的元素位置。如果都匹配,返回每个容器的 last 。重载版本使用自定义的比较操作。

next_permutation
generates the next greater lexicographic permutation of a range of elements
取出当前范围内的排列,并将其重新排序为下一个排列。重载版本使用自定义的比较操作。

nth_element
put one element in its sorted location and make sure that no elements to its left are greater than any elements to its right
将范围内的序列重新排序,使所有小于第 n 个元素的元素都出现在它前面,而大于它的都出现在后面,重载版本使用了自定义的比较操作。

partial_sort
sort the first N elements of a range
对整个序列做部分排序,被排序元素的个数正好可以被放到范围内。重载版本使用自定义的比较操作。

partial_sort_copy
copy and partially sort a range of elements
与 partial_sort 相同,除了将经过排序的序列复制到另外一个容器。

partial_sum
compute the partial sum of a range of elements
创建一个新的元素序列,其中每个元素的值代表了范围内该位置之前所有元素之和。重载版本使用了自定义操作替代加法。

partition
divide a range of elements into two groups
对范围内元素重新排序,使用输入的函数,把计算结果为 true 的元素都放在结果为 false 的元素之前。

pop_heap
remove the largest element from a heap
并不是真正的把最大元素从堆中弹出,而是重新排序堆。它把 first 和 last-1 交换,然后重新做成一个堆。可以使用容器的 back 来访问被“弹出“的元素或者使用 pop_back 来真正的删除。重载版本使用自定义的比较操作。

prev_permutation
generates the next smaller lexicographic permutation of a range of elements
取出范围内的序列并将它重新排序为上一个序列。如果不存在上一个序列则返回 false 。重载版本使用自定义的比较操作。

push_heap
add an element to a heap
假设 first 到 last-1 是一个有效的堆,要被加入堆的元素在位置 last-1 ,重新生成堆。在指向该函数前,必须先把元素插入容器后。重载版本使用指定的比较。

random_sample
randomly copy elements from one range to another

random_sample_n
sample N random elements from a range

random_shuffle
randomly re-order elements in some range
对范围内的元素随机调整次序。重载版本输入一个随机数产生操作。

remove
remove elements equal to certain value
删除在范围内的所有等于指定的元素,注意,该函数并不真正删除元素。内置数组不适合使用 remove 和 remove_if 函数。

remove_copy
copy a range of elements omitting those that match a certian value
将所有不匹配的元素都复制到一个指定容器,返回的 OutputIterator 指向被拷贝的末元素的下一个位置。

remove_copy_if
create a copy of a range of elements, omitting any for which a predicate is true
将所有不匹配的元素拷贝到一个指定容器。

remove_if
remove all elements for which a predicate is true
删除所有范围内输入操作结果为 true 的元素。

replace
replace every occurrence of some value in a range with another value
将范围内的所有等于 old_value 的元素都用 new_value 替代。

replace_copy
copy a range, replacing certain elements with new ones
与 replace 类似,不过将结果写入另外一个容器。

replace_copy_if
copy a range of elements, replacing those for which a predicate is true
类似与 replace_if ,不过将结果写入另外一个容器

replace_if
change the values of elements for which a predicate is true
将范围内的所有操作结果为 true 的元素用新值替代。

reverse
reverse elements in some range
将范围内元素重新按反序排列。

reverse_copy
create a copy of a range that is reversed
类似与 reverse ,不过将结果写入另外一个容器。

rotate
move the elements in some range to the left by some amount
将范围内的元素移到容器末尾,由 middle 指向的元素成为容器第一个元素。

rotate_copy
copy and rotate a range of elements
类似与 rotate ,不过将结果写入另外一个容器

search
search for a range of elements
给出了两个范围,返回一个 iterator ,指向在范围内第一次出现子序列的位置。重载版本使用自定义的比较操作。

search_n
search for N consecutive copies of an element in some range
在范围内查找 value 出现 n 次的子序列。重载版本使用自定义的比较操作。

set_difference
computes the difference between two sets
构造一个排过序的序列,其中的元素出现在第一个序列中,但是不包含在第二个序列中。重载版本使用自定义的比较操作。

set_intersection
computes the intersection of two sets
构造一个排过序的序列,其中的元素在两个序列中都存在。重载版本使用自定义的比较操作。

set_symmetric_difference
computes the symmetric difference between two sets
构造一个排过序的序列,其中的元素在第一个序列中出现,但是不出现在第二个序列中。重载版本使用自定义的比较操作。

set_union
computes the union of two sets
构造一个排过序的序列,它包含两个序列中的所有的不重复元素。重载版本使用自定义的比较操作。

sort
sort a range into ascending order
以升序重新排列范围内的元素,重载版本使用了自定义的比较操作。

sort_heap
turns a heap into a sorted range of elements
对范围内的序列重新排序,它假设该序列是个有序的堆。重载版本使用自定义的比较操作。

stable_partition
divide elements into two groups while preserving their relative order
与 partition 类似,不过它保证保留容器中的相对顺序。

stable_sort
sort a range of elements while preserving order between equal elements
类似与 sort ,不过保留相等元素之间的顺序关系。

swap
swap the values of two objects
交换存储在两个对象中的值。

swap_ranges
swaps two ranges of elements
将在范围内的元素与另外一个序列的元素值进行交换。

transform
applies a function to a range of elements
将输入的操作作用在范围内的每个元素上,并产生一个新的序列。重载版本将操作作用在一对元素上,另外一个元素来自输入的另外一个序列。结果输出到指定的容器。

unique
remove consecutive duplicate elements in a range
清除序列中重复的元素,和 remove 类似,它也不能真正的删除元素。重载版本使用了自定义的操作。

unique_copy
create a copy of some range of elements that contains no consecutive duplicates
类似与 unique ,不过它把结果输出到另外一个容器。

upper_bound
searches for the last possible location to insert an element into an ordered range
返回一个 iterator ,它指向在范围内的有序序列中插入 value 而不破坏容器顺序的最后一个位置,该位置标志了一个大于 value 的值。重载版本使用了输入的比较操作。

0 0
原创粉丝点击