C++ STL algorithm library algorithm, numeric, functional, memory, iterator, utility contains a list of commonly used functions

Part.0 Introduction

insert image description here

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 vof[begin,end)
*max_element(v.begin(), v.end()) find the maximum value of the data in the vrange[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 , vif found, return an iterator of value, otherwise return[begin,end)keykeyend
count(v.begin(),v.end(),key) The number of statistical values ​​in the range vof[begin,end)key
mismatch(v1.begin(), v1.end(), v2); Whether the data in the returned range is the v1same as that, return the data that is different for the first time, and the return value is a[begin,end)v2pair
equal(v1.begin(), v1.end(), v2); Whether the data in the range v1of judgment is the same as . return the same else return[begin,end)v2truefalse
is_permutation(v1.begin(), v1.end(), v2.begin()) Whether the data in the range to be judged is the v1same as the contained elements, or not in the same order. return the same else return[begin,end)v2truefalse
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)v1v2v1.end()
search_n(v.begin(),v.end(),n,tar) Find a target data in the range of , vif found, return the first iterator[begin,end)ntartar
shuffle(v.begin(), v.end() Shuffling the order of data in a vrange[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)functruefalse
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)functruefalse
none_of(v.begin(), v.end(),func) Judging whether the data in the range vof [begin,end)is not satisfied func, if not, return trueotherwise returnfalse

modify operation

function meaning
transform(v1.begin(), v1.end(), v2.begin(), op_increase); Assign the data in the range v1of to[begin,end)op_increasev2
replace(v.begin(), v.end(), a, b); Replace in the data in the range vof with[begin,end)ab
fill(v.begin(),v.begin()+4,5); Set all data values ​​in the range vof to[begin,begin+4)5
remove(v.begin(), v.end(),rm) Delete all the data whose value is within the vrange of[begin,end)rm
reverse(v.begin(),v.end()) flip the data in the range vof[begin,end)
unique (v.begin(), v.end()) uniqueize the data in the vrange of[begin,end)
rotate(v.begin(),v.begin()+3,v.end()) Put vthe 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)cmprbegin
is_sorted(v.begin(),v.end()) Determine vwhether [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 velement less than +1[begin,end)tartar
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)tartar
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 vto[begin,end)tarpair[pair.first,pair.second)tar
binary_search(v.begin(), v.end(), tar) In the range vof [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 vbe 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 v1and v2ascending together
includes(v1,v1+10,v2,v2+4) Determine v1whether to includev2
set_union(v1, v1+5, v2, v2+5, v.begin()); v1The summed v2collection is stored inv
set_intersection(v1, v1+5, v2, v2+5, v.begin()) v1v2的交集存于v,返回交集最后一个元素的迭代器+1
set_difference(v1, v1+5, v2, v2+5, v.begin()) v1v2的差集存于v
set_symmetric_difference(v1, v1+5, v2, v2+5, v.begin()) v1v2的互差集存于vset_difference只包含v1不包含v2的元素,它都包含,就是不包含公共的元素。
lexicographical_compare(v1,v1+5,v2,v2+9) 比较v1v2的大小,默认小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

  • 之前我一直以为memcpysizeof是在这个头文件中包含的,但是后来发现并没有,sizeof是C++的一个关键字,memcpy是在string.h中定义的。

Part.V iterator

处于头文件#include <iterator> 中,给迭代器(可以将其简单理解为指针,*itr可以获取迭代器itr所指向的值)提供定义和支持。官网中常用的函数列表如下:

函数 含义
advance(it,n); itrAdvances the iterator nby units. For example, itrit 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- itrto-iterator v.begin()distance
begin(container) Returns containerthe starting index of the container
begin(container) Returns containerthe 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 itran iterator to the previous iterator
next(itr) returns itran 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 aand b, as long as they are both of the same type, they can be exchanged
make_pair(x,y) return (x,y)composed ofpair

Guess you like

Origin blog.csdn.net/Gou_Hailong/article/details/128476298