常用小函数——不要重复造轮子

abs()

#include <cmath>

In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header<complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs).

在C++ 重载为各种类型,掉渣天的连复数也有。头文件 cmath

需要注意的是,cstdlib中也包含有abs,不过这个只支持int和longlong,并不支持浮点型,浮点型用另外一个fabs,同样包含在cstdlib

经实验,如果cmath和cstdlib同时包含的话,是可以用abs处理浮点数的。


max()  min() swap()

using namespace std;

支持各种类型包括自定义结构体,使用的是template机制。

根据www.cplusplus.com表示,在c++11之前版本,这些都包含在algorithm,不过在本机codeblocks v12.11上都是只需要std即可了。


 next_permutation()  prev_permutation();

#include <algorithm>

函数定义bool next_permutation (BidirectionalIterator first,BidirectionalIterator last);

           or  bool next_permutation (BidirectionalIterator first,BidirectionalIterator last, Compare comp);

First 是 头迭代器, last是尾迭代器,  作用是使得[first,last)内的数字按照比较算子comp(默认为小于号)来获得下一个序列.

prev_permutation同理

不定期更新

原文地址:https://www.cnblogs.com/someblue/p/3597931.html