C++标准模板库(STL)(三)

头文件algorithm

max() min() abs() swap()

#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
	int x=1,y=-2;
	printf("max(x,y)=%d min(x,y)=%d\n",max(x,y),min(x,y));
//abs(x)中x必须为整数 浮点数需用math下的fabs()
	printf("abs(x)=%d abs(y)=%d\n",abs(x),abs(y));
	swap(x,y);
	printf("x=%d y=%d\n",x,y);
	return 0;
}

max(x,y)=2 min(x,y)=-1
abs(x)=1 abs(y)=2
x=-2 x=1


reverse()

reverse(it1,it2)将数组指针或容器迭代器在[it1,it2)之间元素反转

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	string str="abcdefgh";
	reverse(str.begin()+1,str.end()-3);
	cout<<str<<endl;	
	int arr[10]={1,2,3,4,5};
	reverse(arr+1,arr+4);
	for(int i=0;i<5;i++){
		printf("%d",arr[i]);
	}
	return 0;
}

aedcbfgh
14325


next_permutation()

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

#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
	int a[10]={1,2,3};
	do{								//使用do...while语句以输出序列本身		
		printf("%d %d %d\n",a[0],a[1],a[2]);
	}while(next_permutation(a,a+3));//next_permutation()在到达全排列最后一个时返回false
	return 0;
}

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

#include<iostream>
#include<algorithm>
#include<string> 
using namespace std;
int main(){
	string str="abc";
	do{
		cout<<str<<endl;
	}while(next_permutation(str.begin(),str.end()));
	return 0;
}

abc
acb
bac
bca
cab
cba


fill()

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

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int a[10]={0};
	fill(a,a+5,233);
	for(int i=0;i<5;i++){
		printf("%d ",a[i]);
	}
	return 0;
}

233 233 233 233 233

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
	string str="abcd";
	fill(str.begin(),str.end(),'c');
	cout<<str<<endl;
	return 0;
}

cccc


sort()

sort(it1,it2,cmp)将[it1,it2)之间的元素进行排序,cmp为比较函数,可不填,此时默认为递增排序

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
	string str="cbda";
	sort(str.begin(),str.end());
	cout<<str<<endl;	
	return 0;
}

abcd

cmp() 基本数据类型,容器
在STL标准容器中,只有vector,string,dequeue可以使用sort,使用红黑树实现的set,map元素本身有序,不可使用sort排序。
这也是只有vector,string,dequeue容器的迭代器可以使用加减法的原因。

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(char a,char b){
	return a>b;
}
int main(){
	string str="dcba";
	sort(str.begin(),str.end(),cmp);
	cout<<str<<endl;	
	return 0;
}

dcba

#include<iostream>
#include<algorithm>
#include<string> 
using namespace std;
bool cmp(string str1,string str2){//按字符串长度从小到大排序 
	return str1.length()<str2.length();
}
int main(){
	string str[3]={"aaa","cc","bbbb"};
	sort(str,str+3,cmp);
	for(int i=0;i<3;i++){
		cout<<str[i]<<endl;
	}
	return 0;
}

cc
aaa
bbbb

cmp() 结构体数组

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	int x,y;
}ssd[10];
//二级排序:先按x从大到小进行排序,x相等时按照y大小从小到大进行排序 
bool cmp(node a,node b){
	if(a.x!=b.x)
		return a.x>b.x;
	else
		return a.y<b.y;
}
int main(){
	ssd[0].x=2;//{2,2} 
	ssd[0].y=2;	
	ssd[1].x=1;//{1,3} 
	ssd[1].y=3;	
	ssd[2].x=2;//{2,1} 
	ssd[2].y=1;	
	sort(ssd,ssd+3,cmp);
	for(int i=0;i<3;i++){
		printf("%d %d\n",ssd[i].x,ssd[i].y);
	}
	return 0;
}

2 1
2 2
1 3


lower_bound() upper_bound()

用于有序数组或容器中,lower_bound(first,last,val)及upper_bound(first,last,val)分别用来寻找[first,last)范围内第一个大于等于val或第一个大于val的元素位置的指针或迭代器。若不存在要找的元素,返回插入位置。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	vector<int> vi;
	for(int i=1;i<=5;i++){
		vi.push_back(i);
	}
	printf("%d\n",lower_bound(vi.begin(),vi.end(),3)-vi.begin());
	return 0;
}

2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值