C++标准模板库(STL)之 algorithm头文件下的常用函数

一、max() , min() , abs()

  max(x,y)和min(x,y)分别返回x和y的最大值和最小值,且参数必须是两个(可以是浮点数)

二、swap()

  swap(x,y)用来交换x和y的值

三、reverse()

  reverse(it,it2)可以将数组指针在(t,t2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>

#include<string>
using namespace std;
int main(){
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    reverse(a,a+4);//将a[0]~a[3]反转
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}
//输出:
//4 3 2 1 5 6 7 8 9 10

  对容器中元素(例如string字符串)进行反转,也一样

四、next_permutation()

  next_permutation()给出一个序列在全排列中的下一个序列

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>

#include<string>
using namespace std;
int main(){
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    do{
        printf("%d %d %d
",a[0],a[1],a[2]);
    }while(next_permutation(a,a+3));
    cout<<endl;
    return 0;
}
//输出:
//1 2 3
//1 3 2
//2 1 3
//2 3 1
//3 1 2
//3 2 1

五、fill()

  fill()可以把数组或容器中的某一段区间赋值为某个相同的值,和memset不同,这里的赋值可以是数组类型对应范围中的任意值

  而memset只能赋值-1或0

  

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>

#include<string>
using namespace std;
int main(){
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    fill(a,a+10,5);
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}
//输出:
//5 5 5 5 5 5 5 5 5 5

  对二维数组的赋值:

  fill(a[0],a[0]+maxn*maxn,b);

六、sort()

  sort()是用来排序的函数

  (1)如何使用:

  sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填))

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>

#include<string>
using namespace std;
int main(){
    int a[6]={9,4,2,5,6,-1};
    //将a[0]~a[3]从小到大排序
    sort(a,a+4);//默认为从小到大排序
    for(int i=0;i<6;i++){
        printf("%d ",a[i]);
    }
    printf("
");
    //a[0]~a[5]从小到大排序
    sort(a,a+6);
    for(int i=0;i<6;i++){
        printf("%d ",a[i]);
    }
    return 0;
}
//输出:
//2 4 5 9 6 -1
//-1 2 4 5 6 9

  对double型数组排序:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>

#include<string>
using namespace std;
int main(){
    double a[]={1.4,-2.1,9};
    sort(a,a+3);
    for(int i=0;i<3;i++){
        printf("%.1f ",a[i]);
    }
    printf("
");
    return 0;
}
//输出:
//-2.1 1.4 9.0

  (2)如何实现比较函数cmp

    使用比较函数cmp,来告诉sort何时要交换元素(让元素的大小比较关系反过来)

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
//bool cmp(char a,char b){
//    return a>b;
//}
bool cmp(int a,int b){
    return a>b;  //可以理解为当a>b 时把a放在b前面
}

int main(){
    int a[]={3,1,4,2};
    sort(a,a+4,cmp);
    for(int i=0;i<4;i++){
        printf("%d ",a[i]); //输出4 3 2 1
    }
    printf("
");
    return 0;
}
//输出:
//4 3 2 1

  (3)对结构体数组的排序

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
struct node{
    int x,y;
}ssd[10];
bool cmp(node a,node b){
    return a.x>b.x;  //按照x值从大到小对结构体进行排序
}

int main(){
    ssd[0].x=2;
    ssd[0].y=2;
    ssd[1].x=1;
    ssd[1].y=3;
    ssd[2].x=3;
    ssd[2].y=1;
    sort(ssd,ssd+3,cmp);
    for(int i=0;i<3;i++){
        printf("%d %d
",ssd[i].x,ssd[i].y);
    }
    return 0;
}
//输出:
//3 1
//2 2
//1 3

  (4)容器的排序

    在STL容器中,只有vector,string,deque是可以使用sort排序的

    vector排序:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
using namespace std;
//struct node{
//    int x,y;
//}ssd[10];
bool cmp(int a,int b){
    return a>b;  //按照x值从大到小对结构体进行排序
}

int main(){
    vector<int> vi;
    vi.push_back(3);
    vi.push_back(1);
    vi.push_back(2);
    sort(vi.begin(),vi.end(),cmp);
    for(int i=0;i<3;i++){
        printf("%d ",vi[i]);
    }
    return 0;
}
//输出:
//3 2 1

  string的排序:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
using namespace std;

bool cmp(int a,int b){
    return a>b;  //按照x值从大到小对结构体进行排序
}

int main(){
    string str[3]={"bbbb","cc","aaa"};
    sort(str,str+3);
    for(int i=0;i<3;i++){
        cout<<str[i]<<endl;
    }
    return 0;
}
//输出:
//aaa
//bbbb
//cc

    

    

原文地址:https://www.cnblogs.com/dreamzj/p/14324984.html