algorithm头文件下常用函数
max()、min()
max(x,y),min(x,y)分别返回x和y中的最大值和最小值,必须是两个参数,这两个参数的类型可以是整数也可以是浮点数。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
int x=5,y=6,z=2;
double dx=3.2,dy=5.1;
//用于整数
cout<<"max(x,y)="<<max(x,y)<<endl;
cout<<"min(x,y)="<<min(x,y)<<endl;
cout<<"max(x,y,x)="<<max(z,max(x,y))<<endl; //比较三个数
//用于浮点数
cout<<"max(dx,dy)="<<max(dx,dy)<<endl;
cout<<"min(dx,dy)="<<min(dx,dy)<<endl;
getchar();getchar();
return 0;
}
结果
abs()
函数abs(x)返回x的绝对值,x必须是整数类型。求浮点数的绝对值可以用math.h头文件下的fabs()
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
int a[]={1,-5,6,23,-89,41,-77,-56,-12,-20,0,12};
for(int i=0;i<12;i++){
cout<<abs(a[i])<<' ';
}
getchar();getchar();
return 0;
}
结果
swap()
swap(x,y)用来交换x,y的值,参数类型可以是整型、浮点型、结构体类型。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
typedef struct node{
int s;
}Node;
int main()
{
int x=10,y=20;
double dx=10.2,dy=20.4;
Node Nx,Ny;
Nx.s=100,Ny.s=200;
swap(x,y);
swap(dx,dy);
swap(Nx,Ny);
cout<<x<<' '<<y<<endl;
cout<<dx<<' '<<dy<<endl;
cout<<Nx.s<<' '<<Ny.s<<endl;
getchar();getchar();
return 0;
}
结果
reverse()
reverse(it,it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)之间的元素进行反转。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
vector<int> b(a,a+10);
reverse(a,a+5);
reverse(b.begin()+5,b.end());
for(int i=0;i<10;i++)
cout<<a[i]<<' ';
cout<<endl;
for(int i=0;i<10;i++)
cout<<b[i]<<' ';
cout<<endl;
getchar();getchar();
return 0;
}
结果:
next_permutation()、pre_permutation()
next_permutation()给出一个序列在全排列中的下一个序列;pre_permutation()给出一个序列在全排列中的上一个序列。常用do-while循环,而不用while循环。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
int a[3]={1,2,3};
int b[3]={3,2,1};
do{
for(int i=0;i<3;i++)
cout<<a[i]<<' ';
cout<<"\t\t";
for(int i=0;i<3;i++)
cout<<b[i]<<' ';
cout<<endl;
}while(next_permutation(a,a+3)&&prev_permutation(b,b+3));
getchar();getchar();
return 0;
}
结果
fill()
fill(begin,end,x),begin是首元素地址,end是尾元素地址的下一个地址,将数组或容器在区间[begin,end)内的所有元素值设置为x
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
fill(a,a+10,100);
for(int i=0;i<10;i++)
cout<<a[i]<<' ';
cout<<endl;
getchar();getchar();
return 0;
}
结果
sort()
sort(首元素地址,尾元素地址的下一个地址,比较函数(非必填))
如果不写比较函数,默认进行递增排序,可以用于int、double、char等基本类型进行排序。
如果要进行降序排序或对于结构体元素,需要人为指定比较规则。编写比较函数一般写作cmp()
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
using namespace std;
bool cmp(int a,int b){
return a>b; //按照降序排序
}
void Print(int a[]){//输出
for(int i=0;i<15;i++)
cout<<a[i]<<' ';
cout<<endl;
}
int main()
{
int a[15];
for(int i=0;i<15;i++){
a[i]=rand()%50;
}
cout<<"原数组a:";
Print(a);
sort(a,a+15); //升序
cout<<"升序排序:";
Print(a);
sort(a,a+15,cmp);//降序
cout<<"降序排序:";
Print(a);
getchar();getchar();
return 0;
}
结果
结构体元素的排序
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
typedef struct node{
int x;
int y;
}Node;
bool cmp(Node a,Node b){ //按照元素中x的大小升序排序(一级排序)
return a.x<b.x;
}
bool cmp2(Node a,Node b){
//先按x降序排序,x相同时按y升序排序(二级排序)
if(a.x!=b.x)
return a.x>b.x;
else
return a.y<b.y;
}
void Print(Node a[]){ //输出
for(int i=0;i<15;i++){
cout<<"("<<a[i].x<<","<<a[i].y<<")"<<' ';
}
cout<<endl;
}
int main()
{
Node a[15];
for(int i=0;i<15;i++){
a[i].x=rand()%10;
a[i].y=rand()%10;
} //赋初值
cout<<"原结构体数组a:\t";
Print(a);
sort(a,a+10,cmp);
cout<<"按x升序排序:\t";
Print(a);
sort(a,a+10,cmp2);
cout<<"二级排序:\t";
Print(a);
getchar();getchar();
return 0;
}
结果
lower_bound()、upper_bound()
lower_bound()和upper_bound()用在有序的数组或容器中。
lower_bound(first,last,val)用来寻找[first,last)范围内第一个值大于等于val的元素的位置
upper_bound(first,last,val)用来寻找[first,last)范围内第一个值大于val的元素的位置
如果是数组返回该位置指针,如果是容器返回该位置迭代器
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int a[15];
for(int i=0;i<15;i++){
a[i]=rand()%10;
} //数组元素赋初值
sort(a,a+15); //升序
cout<<"数组a为:";
for(int i=0;i<15;i++)
cout<<a[i]<<' ';
cout<<endl;
int *t1=lower_bound(a,a+15,1); //查找1
int *t2=upper_bound(a,a+15,1);
cout<<t1-a<<' '<<t2-a<<endl;
cout<<lower_bound(a,a+15,5)-a<<' '; //查找5
cout<<upper_bound(a,a+15,5)-a<<endl;
cout<<lower_bound(a,a+15,10)-a<<' '; //查找10
cout<<upper_bound(a,a+15,10)-a<<endl; //得到的结果为15,超出了数组下标
getchar();getchar();
return 0;
}
结果
nth_element()
nth_element(begin,nth,end,cmp)可以用于求区间[begin,end)中第n小的元素,但并不对所有元素进行排序,函数将所有小于第n个元素的值放到第n小元素的左边,将所有大于第n小的元素放到第n小元素的右边,所以最终第n小的元素在正确的位置上。也可以自己定义比较函数用于求第n大的函数。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<ctime>
using namespace std;
void Print(int a[]){
cout<<"数组a为:";
for(int i=0;i<20;i++)
cout<<a[i]<<' ';
cout<<endl;
}
bool cmp(int x,int y){
return x>y;
}
int main()
{
int a[20];
srand(unsigned(time(NULL)));
for(int i=0;i<20;i++){
a[i]=rand()%10;
} //数组元素赋初值
Print(a);
nth_element(a,a+4,a+20); //求第5小的数
cout<<"第5小元素为:"<<a[4]<<endl;
Print(a);
nth_element(a,a+4,a+20,cmp); //求第5大的数
cout<<"第5大元素为:"<<a[4]<<endl;
Print(a);
getchar();getchar();
return 0;
}
结果