C++标准模板库(STL)——algorithm内置函数

STL——algorithm内置函数
头文件:
#include<algorithm>
using namespace std;
  • 比较大小:max(x,y), min(x,y), abs(x)

  • 交换:swap(x,y)

  • 初始化数组:fill(首地址,尾地址,数值)
    一维数组:fill(a, a+5, 10)  //a[5]初始化为10
    二维数组:fill(a[0], a[0]+5*5, 10)  //二维数组a[5][5]初始化为10

  • 反转:reverse(t1, t2): 数组指针或者容器迭代器在[t1,t2)范围内元素反转

#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    reverse(a,a+10);                //从开头至结尾数组反转
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;

    string str="abcde";
    reverse(str.begin()+2,str.begin()+5);  //从第下标2至下标4,反转字符串
    cout<<str;

    return 0;
}
输出:
10 9 8 7 6 5 4 3 2 1
abedc
  • 全排列:next_permution() 给出一个序列在全排列中的下一个序列
    使用循环是因为next_peumutation()在到最后一个全排列后会返回false,这样方便退出循环;使用do…while而不是while是因为a b c本身也需要输出,否则直接进入下一个序列,不会输出a b c
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
    string str;           //将输入按字符串读入
    cin>>str;

    do{
        cout<<str<<endl;    
    }while(next_permutation(str.begin(),str.begin()+str.size()));  //求下一个全排列

    return 0;
}
输入:
abc
输出:abc的全排列
  • 排序:sort(首地址,尾地址下一个,比较函数)
    int、double、char型数组:
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int a[10]={0,9,1,8,2,7,3,6,4,5};

    sort(a,a+10);            //升序排列
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }

    cout<<endl;

    sort(a,a+10,greater<int>());  //降序排列
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }
    
    return 0;
}
输出:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

vector容器,string型数组:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector<int> a;
    for(int i=0;i<=9;i++){
        a.push_back(i);
    }

    sort(a.begin(),a.begin()+10);            //升序排列
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }

    cout<<endl;

    sort(a.begin(),a.begin()+10,greater<int>());  //降序排列
    for(int i=0;i<10;i++){
        cout<<a[i]<<" ";
    }

    return 0;
}

string 类型字符串:

#include<iostream>
#include<algorithm>

using namespace std;

int main(){
    string str="abcd";

    sort(str.begin(),str.begin()+4);            //升序排列
    cout<<str;

    cout<<endl;

    sort(str.begin(),str.begin()+4,greater<int>());  //降序排列
    cout<<str;

    return 0;
}
输出:abcd   dcba

struct结构体:需要使用第三个参数比较函数

#include<iostream>
#include<algorithm>
using namespace std;

struct node{          //定义结构体node
    int x,y;
}ssd[10];

bool cmp(node a, node b){
    if(a.x!=b.x) return a.x>b.x;   //x不相等按降序排列
    else         return a.y<b.y;   //x相等则按y升序排列
}

int main(){
    ssd[0].x=2; ssd[0].y=2;    //初始化赋值
    ssd[1].x=1; ssd[1].y=3;
    ssd[2].x=2; ssd[2].y=1;

    sort(ssd,ssd+3,cmp);       //排序

    for(int i=0;i<3;i++){
        cout<<ssd[i].x<<" "<<ssd[i].y<<endl;
    }
    return 0;
}
  • low_bound(初始地址,结束地址,数值):范围内第一个>=数值元素的位置
    upper_bound(初始地址,结束地址,数值): 范围内第一个>数值元素的位置

    如果是数组,返回数值位置的指针,如果是容器,返回数值位置的迭代器
    数组或者容器必须有序
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;

int main(){
    int a[6]={1,2,3,3,3,4};
    
    cout<<lower_bound(a,a+6,3)-a<<endl;      //指针-a,即第一个元素3的下标
    
    cout<<upper_bound(a,a+6,3)-a<<endl;      //第一个大于元素3的位置下标
    
    cout<<upper_bound(a,a+6,3)-lower_bound(a,a+6,3);   //两者相减即元素3的个数
    
    return 0;
}
输出:2 5 3
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值