C++模板库STL——algorithm下的常用函数

跟着书做一个整理:
0、max(),min(),abs()
abs()中必须是整数;如果求浮点数的绝对值,使用fabs()

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){
	int x = 1,y = -2;
	printf("%d %d\n",max(x,y),min(x,y));
	printf("%d %d\n",abs(x),abs(y));
	double z = -1.222;
	printf("%.3f\n",fabs(z));//小数的绝对值 
	return 0;
}

1、swap(很方便~)

#include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int x = 1,y = 2;
	swap(x,y);
	printf("%d %d\n",x,y);
	return 0;
}

2、reverse(将区间的数据反转)

#include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int a[10] = {10,11,12,13,14,15};
	reverse(a,a+4);//将a[0]-a[3]之间数据反转(左闭右开区间) 
	for(int i = 0;i<6;i++){
		printf("%d ",a[i]);
	}
}

对容器中的元素反转

#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;

int main(){
	string str = "abcdefghijklmn";
	reverse(str.begin()+2,str.begin()+6);
	for(int i = 0;i<str.length();i++){
		printf("%c",str[i]);
	}
	return 0;
}

3、next_permutation
给出一个序列在全排列中的下一个序列

#include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int a[10] = {1,2,3};
	do{
		printf("%d%d%d\n",a[0],a[1],a[2]);
	}while(next_permutation(a,a+3));
	return 0;
}

4、fill()
可以把数组或容器中某一段区间赋为某个相同的值(与memset不同,memset是整个的修改)

#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
	int a[5] = {1,2,3,4,5};
	fill(a,a+5,233);
	for(int i = 0;i <5;i++){
		printf("%d ",a[i]);
	}
	return 0;
}

5、sort
sort默认是升序,这里只记录对容器的排序

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a,int b){
	return a>b;
}
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; 
}

按字符串长度从小到大排序:

#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(string str1,string str2){
	return str1.length()<str2.length();//按string的长度从小到大排序 
}

int main(){
	string str3[3] = {"bbbb","cc","aaa"};
	sort(str3,str3+3,cmp);
	for(int i = 0;i <3;i++){
		cout<<str3[i]<<endl;
	}
	return 0;
}

6、lower_bound()及upper_bound()
lower_bound(first,last,val)寻找在数组或容器的[first,last)范围内第一个值大于等于 val的元素的位置
upper_bound(first,last,val)寻找在数组或容器的[first,last)范围内第一个值大于val的元素的位置

#include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int a[10] = {1,2,2,3,3,3,5,5,5,5};
	//寻找-1 
	int *lowerPos = lower_bound(a,a+10,-1);
	int *upperPos = upper_bound(a,a+10,-1);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找1
	lowerPos = lower_bound(a,a+10,1);
	upperPos = upper_bound(a,a+10,1);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找3
	lowerPos = lower_bound(a,a+10,3);
	upperPos = upper_bound(a,a+10,3);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找4
	lowerPos = lower_bound(a,a+10,4);
	upperPos = upper_bound(a,a+10,4);
	printf("%d, %d\n",lowerPos - a,upperPos - a);
	//寻找6
	lowerPos = lower_bound(a,a+10,6);
	upperPos = upper_bound(a,a+10,6);
	printf("%d, %d\n",lowerPos - a,upperPos - a); 
}
发布了123 篇原创文章 · 获赞 3 · 访问量 3220

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104094379