algorithm文件中的常用函数

1. max() min() abs()

max(x,y):返回两个数中的最大值
min(x,y):返回两个数中的最小值
abs(x):返回整数x的绝对值
拓展:math头文件中的fabs(x),才可以返回浮点数的绝对值。

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    cout<<max(a,b)<<endl;//求a,b中的最大值
    cout<<max(c,max(a,b))<<endl;//求a,b,c中的最大值
    cout<<min(a,min(b,c))<<endl;//求a,b,c中的最小值
    cout<<abs(a)<<" "<<abs(b)<<" "<<abs(c)<<endl;//分别求三个数的绝对值
    return 0;
}
2.swap() reverse()

swap(x,y):实现交换x,y的值
reverse(it1,it2):实现将数组指针在[it1,it2)间的元素/容器的迭代器在此范围内的元素进行反转

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
    int a,b,c;
    int num[10]={9,8,7,6,5,4,3,2,1,0};//数组
    string str="abcdefg";//字符串
    cin>>a>>b>>c;
    swap(a,b);//交换a,b的值
    cout<<a<<" "<<b<<" "<<c<<endl;
    reverse(num,num+4);//0-3范围内的4个迭代器/指针进行反转
    for(int i=0;i<10;i++)
    if(i!=9)
    cout<<num[i]<<" ";
    else cout<<num[i]<<endl;
    reverse(str.begin(),str.begin()+4);//对str[0]-str[3]元素进行反转
    cout<<str<<endl;
    return 0;
}
4.next_permutation()

next_permutation(a,b):返回由a-b数字组成的全排列的下一个排列
eg:123的next_permutation是132,然后依次是213、231、312、321

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int num[10]={9,8,7};//数组
    do{
        cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
    }while(next_permutation(num,num+3));//达到排列的最后一个返回false
    return 0;
}
5.fill()

fill(a,b,x):可将数组/容器中的区间(a,b)均赋为值x.

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int num[10]={9,8,7};//数组
    fill(num,num+3,1);
    cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
    return 0;
}
6.sort()

格式sort(首元素地址,尾元素地址的下一个,比较函数);
其中,比较函数可省略,默认按由小到大的递增序列排序

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int num[10]={9,8,7};//数组
    char str[]={'c','b','a'};
    sort(num,num+3);//递增排序
    sort(str,str+3);
    cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
    cout<<str<<endl;
    return 0;
}

自定义比较函数排序

#include <iostream>
#include <algorithm>
using namespace std;
struct fruit{
    string name;
    double price;
}f[10];
bool cmp2(int a,int b){//返回较大值
    return a>b;
}
bool cmp1(fruit a,fruit b){//比较结构体中的元素
    return a.price>b.price;
}
int main(){
    int num[10]={3,4,5};//数组
    cin>>f[0].name>>f[0].price>>f[1].name>>f[1].price;
    sort(f,f+2,cmp1);//递增排序
    sort(num,num+3,cmp2);
    cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
    cout<<f[0].name<<" "<<f[0].price<<" "<<f[1].name<<" "<<f[1].price<<endl;
    return 0;
}
7.lower_bound() upper_bound()

lower_bound(a,b,x):返回在a,b范围内第一个大于等于x的元素指针。
upper_bound(a,b,x):返回在a,b范围内第一个大于x的元素指针.

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int num[10]={1,2,3,4,5,6,7,8,9};//数组
    int* lower=lower_bound(num,num+10,0);//返回在0-9中第一个>=0的元素指针
    int* upper=upper_bound(num,num+10,5);//返回在0-9中第一个>5的元素指针
    cout<<lower-num<<endl;//返回元素指针的下标
    cout<<upper-num<<endl;//返回元素指针的下标
    return 0;
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值