Article Directory
Part.0 Introduction
Official website: https://cplusplus.com/reference/
This blog post records some functions commonly used by the author when using the C++ algorithm library. It acts like a dictionary for future reference.
Part.I algorithm
In the header file #include <algorithm>
, it provides a set of functions implemented by common algorithms, including permutation, sorting, merging and searching. The list of commonly used functions in the official website is as follows:
find operation
function | meaning |
---|---|
*min_element(v.begin(), v.end()) |
find the minimum value of the data in the range v of[begin,end) |
*max_element(v.begin(), v.end()) |
find the maximum value of the data in the v range[begin,end) |
min()/max() |
Return the minimum/maximum value. The incoming parameter can be two simple types of numbers or an array, but it cannot be a container. The container should use the above. |
minmax() |
Returns the maximum and minimum values |
find(v.begin(),v.end(),key) |
Search in the data in the range of , v if found, return an iterator of value, otherwise return[begin,end) key key end |
count(v.begin(),v.end(),key) |
The number of statistical values in the range v of[begin,end) key |
mismatch(v1.begin(), v1.end(), v2); |
Whether the data in the returned range is the v1 same as that, return the data that is different for the first time, and the return value is a[begin,end) v2 pair |
equal(v1.begin(), v1.end(), v2); |
Whether the data in the range v1 of judgment is the same as . return the same else return[begin,end) v2 true false |
is_permutation(v1.begin(), v1.end(), v2.begin()) |
Whether the data in the range to be judged is the v1 same as the contained elements, or not in the same order. return the same else return[begin,end) v2 true false |
search(v1.begin(), v1.end(), v2.begin(), v2.end(),) |
Search for the data segment equal to in the range of v1 , return the iterator of the first element of the matching data segment, if not found return .[begin,end) v2 [begin,end) v1 v2 v1.end() |
search_n(v.begin(),v.end(),n,tar) |
Find a target data in the range of , v if found, return the first iterator[begin,end) n tar tar |
shuffle(v.begin(), v.end() |
Shuffling the order of data in a v range[begin,end) |
all_of(v.begin(), v.end(),func) |
Judging whether the data in the range satisfies the condition v , if yes, return otherwise[begin,end) func true false |
any_of(v.begin(), v.end(),func) |
Judging whether there is data in the range that satisfies the conditions v , if there is, return otherwise return[begin,end) func true false |
none_of(v.begin(), v.end(),func) |
Judging whether the data in the range v of [begin,end) is not satisfied func , if not, return true otherwise returnfalse |
modify operation
function | meaning |
---|---|
transform(v1.begin(), v1.end(), v2.begin(), op_increase); |
Assign the data in the range v1 of to[begin,end) op_increase v2 |
replace(v.begin(), v.end(), a, b); |
Replace in the data in the range v of with[begin,end) a b |
fill(v.begin(),v.begin()+4,5); |
Set all data values in the range v of to[begin,begin+4) 5 |
remove(v.begin(), v.end(),rm) |
Delete all the data whose value is within the v range of[begin,end) rm |
reverse(v.begin(),v.end()) |
flip the data in the range v of[begin,end) |
unique (v.begin(), v.end()) |
uniqueize the data in the v range of[begin,end) |
rotate(v.begin(),v.begin()+3,v.end()) |
Put v the first 3 elements of the at the end, their relative order remains unchanged |
Sort operation
function | meaning |
---|---|
sort(v.begin(),v.end(),cmp) |
Arrange the data within the range in ascending order according to the rules v , and reverse iterator can be used for reverse order[begin,end) cmp rbegin |
is_sorted(v.begin(),v.end()) |
Determine v whether [begin,end) the data in the range is already sorted |
binary search
function | meaning |
---|---|
lower_bound(v.begin(), v.end(), tar); |
Find elements whose value is less than in the sorted range, return the iterator of the last v element less than +1[begin,end) tar tar |
upper_bound(v.begin(), v.end(), tar) |
Find elements with a value less than or equal to within the sorted range v , returning an iterator to the first greater than[begin,end) tar tar |
equal_range(v.begin(), v.end(), tar); |
Returns the range of data that is equal to the data in the range , returns one , and the data in the range is equal v to[begin,end) tar pair [pair.first,pair.second) tar |
binary_search(v.begin(), v.end(), tar) |
In the range v of [begin,end) , search in the way of binary search tar , return if found true , otherwise returnfalse |
heap
function | meaning |
---|---|
make_heap(v.begin(),v.end()) |
The elements will v be sorted, the default big top heap |
pop_heap(v.begin(),v.end()); |
popup top element |
push_heap(v.begin(),v.end()); |
add elements to the heap |
sort_heap(v.begin(),v.end()); |
Sort the elements in the heap |
is_heap(v.begin(),v.end()); |
Determine whether it is a heap |
other
function | meaning |
---|---|
merge(v1,v1+5,v2,v2+5,v.begin()); |
Merge v1 and v2 ascending together |
includes(v1,v1+10,v2,v2+4) |
Determine v1 whether to includev2 |
set_union(v1, v1+5, v2, v2+5, v.begin()); |
v1 The summed v2 collection is stored inv |
set_intersection(v1, v1+5, v2, v2+5, v.begin()) |
取v1 和v2 的交集存于v ,返回交集最后一个元素的迭代器+1 |
set_difference(v1, v1+5, v2, v2+5, v.begin()) |
取v1 减v2 的差集存于v |
set_symmetric_difference(v1, v1+5, v2, v2+5, v.begin()) |
取v1 与v2 的互差集存于v ,set_difference 只包含v1 不包含v2 的元素,它都包含,就是不包含公共的元素。 |
lexicographical_compare(v1,v1+5,v2,v2+9) |
比较v1 与v2 的大小,默认小true |
next_permutation(v,v+3) |
将v 变为其下一次置换,尽可能小数在前 |
pre_permutation(v,v+3) |
将v 变为其上一次置换,尽可能小数在前,否极泰来 |
Part.II numeric
处于头文件#include <numeric>
中,定义了常用的数学实现函数。官网中一共5个函数,列表如下:
函数 | 含义 |
---|---|
accumulate(v.begin(),v.end(),0); |
返回v 的[begin,end) 范围内的数据累加和 |
accumulate(v.begin(),v.end(), init, std::minus<int>()) |
返回初始值init 减去v 的[begin,end) 范围内的数据所得到的结果。可以看到第四个参数默认是加法std::plus<int>() ,这里是减法,还可以是乘法std::multiplies<int>() ,除法std::divides<int>() ,或者自己定义的运算myfunc |
adjacent_difference(v.begin(),v.end(), result, minus<int>()) |
v 的[begin,end) 范围内的数据相邻元素进行std::minus<int>() 操作(后一个减前一个,首项不变,默认是减法,当然也可是其他运算),第三个参数是返回结果。 |
inner_product(v1.begin(),v1.end(), v2, init,minus<int>(),divides<int>()); |
v1 的[begin,end) 范围内的元素和v2 做内积(默认相乘相加),加到初始值为init 上,并返回结果。当然这里是相减相除,也可以是自己定义的运算。 |
partial_sum(v.begin(),v.end(), result, std::multiplies<int>()); |
v 的[begin,end) 范围内的数据进行前缀和(部分和)multiplies 运算(默认是加法),并将结果赋值给result |
std::iota(v.begin(),v.end(),init); |
将init 顺次增加1赋值给向量v ,包括init |
Part.III functional
处于头文件#include <functional>
中,定义了许多函数对象类型和支持函数对象的功能。因为里面的函数实现大多是为别的类中的函数提供便利和支持的,因此就不详细列举了(像加-减-乘-除-模-非[反号] plus
-minus
-multiplies
-divides
-modulus
-negate
都是在里面定义的),详细介绍可看官网。
Part.IV memory
处于头文件#include <memory>
中,给容器、管理内存的函数和auto_ptr
模板类定义标准内存分配器。官网中常用的函数列表如下:
函数 | 含义 |
---|---|
T* allocate(size_t n); |
分配足够的存储空间来存储T的n个实例,并返回指向它的指针 |
auto sp = std::make_shared<int>(); |
分配堆空间,创建智能指针make_shared ,用指针鼓励用这个,不要用new |
- 之前我一直以为
memcpy
和sizeof
是在这个头文件中包含的,但是后来发现并没有,sizeof
是C++的一个关键字,memcpy
是在string.h
中定义的。
Part.V iterator
处于头文件#include <iterator>
中,给迭代器(可以将其简单理解为指针,*itr
可以获取迭代器itr
所指向的值)提供定义和支持。官网中常用的函数列表如下:
函数 | 含义 |
---|---|
advance(it,n); |
itr Advances the iterator n by units. For example, itr it points to the index at the beginning 1 , then after executing this instruction, it points to the index1+n |
distance(v.begin(),itr) |
Returns the iterator- itr to-iterator v.begin() distance |
begin(container) |
Returns container the starting index of the container |
begin(container) |
Returns container the last index of the container, note that it generally end() indicates the end of the container and does not point to any value |
prev(itr) |
returns itr an iterator to the previous iterator |
next(itr) |
returns itr an iterator after the iterator |
Part.VI utility
In the header file #include <utility>
, define overloaded relational operators to simplify the writing of relational operators. The list of commonly used functions in the official website is as follows:
function | meaning |
---|---|
swap(a,b) |
Exchange the values of a and b , as long as they are both of the same type, they can be exchanged |
make_pair(x,y) |
return (x,y) composed ofpair |