C++常用函数
1、sort()函数
①、处理数组
bool cmp(int a, int b)
{
return a>b;//实现降序排序
}
sort(a, a+100, cmp);
②、处理vector
sort(nums.begin(), nums.end(), cmp);
2、accumulate()函数
#include<numeric>
int sum=accumulate(nums.begin(), nums.end(), 0);
3、pow()函数
include<cmath
它的函数原型有两种常见形式:
1、 double pow(double base, double exponent); :计算 base 的 exponent 次幂, base 是底数, exponent 是指数,返回值是计算结果,类型为 double 。
2、 float pow(float base, float exponent); :与上面类似,只不过参数和返回值的类型是 float ,用于处理单精度浮点数的幂运算。
一般需要强转成int
4、swap()函数
在C++中, swap 函数是标准库 中的一个函数,用于交换两个相同类型对象的值。
1. 基本用法
- 它可以交换内置类型(如 int 、 float 等)的变量值,也可以交换自定义类型(如类对象)的值,只要自定义类型定义了合适的赋值运算符( operator= )。