STL_algorithm专题

max;min;swap

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	//max()
	int mt1=1,mt2=2,mt3=3;
	cout<<max(mt1,mt2)<<endl;	
	cout<<max(mt1,max(mt2,mt3))<<endl;
	
	//min	
	mt1=1,mt2=2,mt3=3;
	cout<<min(mt1,mt2)<<endl;	
	cout<<min(mt1,min(mt2,mt3))<<endl;
	
	//swap
	int s1=1,s2=2;
	cout<<"s1 "<<s1<<" s2 "<<s2<<endl;
	swap(s1,s2);
	cout<<"s1 "<<s1<<" s2 "<<s2<<endl;
	
	 
}

OUT

2
3
1
1
s1 1 s2 2
s1 2 s2 1

reverse;fill

//reverse,反转
	int r[10]={0};
	for(int i=0;i<10;i++)
	{
		r[i]=i;
		printf("%d ",r[i]);
	} 
	printf("\n");
	
	reverse(r+1,r+7);				//左闭右开 
		for(int i=0;i<10;i++)
	{
		printf("%d ",r[i]);
	}
	printf("\n");
	
	//fill,填充一段区间
	int f[10]={0}; 
			for(int i=0;i<10;i++)
	{
		f[i]=i;
		printf("%d ",f[i]);
	}
	
	printf("\n");
	printf("\n");
	
	fill(f+2,f+5,0);				//左闭右开 
			for(int i=0;i<10;i++)
	{
		printf("%d ",f[i]);
	}
	printf("\n");
	
	

OUT


0 1 2 3 4 5 6 7 8 9
0 6 5 4 3 2 1 7 8 9

0 1 2 3 4 5 6 7 8 9
0 1 0 0 0 5 6 7 8 9

next_permutation;sort;lower_bound;upper_bound

//next_permutation ,给出全排列下一个排列
	int n[10]={1,2,3};
	do{
		cout<<n[0]<<n[1]<<n[2]<<" ";
	} while(next_permutation(n,n+3));//左闭右开 
	//到达全排列最后一个是时会自动给出False,退出循环
	 
	printf("\n");
//sort
	int s[5]={1,6,9,4,3};
	sort(s,s+5);
	for(int i=0;i<5;i++)
	{
		printf("%d ",s[i]);
	}
	printf("\n");
	//这里未写出的,sort还有第三个参数,cmp,比较规则,
	//通过编写cmp 函数,可以对结构体按其中的某一项排序
	
	//low_bound(start,end+1,val);返回第一>=val 
	//upper_bound(start,end+1,val);返回第一个>val 
	int l[10]={1,1,1,2,2,2,3,3,3};
	int *lowerpos=lower_bound(l,l+10,2);
	int *upperpos=upper_bound(l,l+10,2);
	cout<<lowerpos-l<<" "<<upperpos-l<<endl;
	//必须应用在有序数组中

OUT

123 132 213 231 312 321
1 3 4 6 9
3 6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值