使用algorithm头文件,需要在头文件下加一行using namespace std;
1)max()
max(x,y)返回x和y中的最大值,并且参数必须是两个(可以是浮点数);如果想要返回三个数x,y,z的最大值,可以使用max(x,max(y,z))的写法。
2)min()
min(x,y)返回x和y中的最小值,并且参数必须是两个(可以是浮点数);如果想要返回三个数x,y,z的最小值,可以使用min(x,min(y,z))的写法。
3)abs()
abs(x)返回x的绝对值,x必须是整数,浮点数的绝对值用math头文件下的fabs.
4)swap()
swap(x,y)用来交换x和y的值
5)reverse()
reverse(it,it2)可以将数组指针在[it,it2之间的元素或容器的迭代器在[it,it2)范围内的元素记行反转
6)next_permutation
nect_permutation()给出一个序列在全排列中的下一个序列
#include<cstdio>
#icnlude<algorithm>
using namespace std;
int main()
{
int a[10]={1,2,3};
do{
printf("%d%d%d\n",a[0],a[1],a[2]);
}while(next_permutation(a,a+3));
return 0;
}
//结果
123
132
213
231
312
321
7)fill()
fill()可以把数组或者容器中的某一段区间赋为某个相同的值。
#include<cstdio>
#icnlude<algorithm>
using namespace std;
int main()
{
int a[10]={1,2,3};
fill(a,a+3,233);//将a[0]~a[2]就赋值为233
for(int i=0;i<3;i++)
{
printf("%d ",a[i]);
}
return 0;
}
//结果
233 233 233
8)sort
9)lower_bound()
10)upper_bound()
这几之前讲过所以在此不再重复