algorithm头文件下的常用函数(摘)


摘自胡凡《算法笔记》

1. max() min()和abs()

#include <cstdio> 
#include <algorithm>
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));
	return 0;
}

2. swap

#include <cstdio> 
#include <algorithm>
using namespace std;
int main(){
	int x=1,y=-2;
	swap(x,y);
	printf("%d %d",x,y);
	return 0;
}

3. reverse

reverse(it,it2)可以将指针数组在(it,it2] 范围内进行反转

#include <cstdio> 
#include <algorithm>
using namespace std;
int main(){
	int a[10]={1,2,3,4,5,6,7,8,9,0};
	reverse(a,a+4);
	for(int i=0;i<10;i++){
		printf("%d ",a[i]);
	}
	return 0;
}

4.next_permutation()给出一个全排列中的下一个序列
n==3时 全排列为 123 132 213 231 312 321
231的下一个序列为 312

#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;
}

5. fill()

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

6.1 sort()

sort的使用的方式如下,
sort(首元素地址(必须填),尾元素地址的下一个地址(必须填),比较函数(非必填));

  • 可以对int型,double型数组排序,也可以对char型数组排序:
#include <iostream>
#include <algorithm> 
using namespace std;
int main(){
char c[20]={'a','A','b','B','c','C','d','D'};
sort(c,c+8);
for(int i=0;i<8;i++){
	cout<<c[i]<<" ";
}
	return 0;
} 

运行结果:A B C D a b c d

6.2比较函数 cmp

6.2.1 基本数据类型的数组排序

若比较函数不填,则默认按照从小到大的顺序排序。如果想要从大到小排序,则使用cmp比较函数。
bool cmp(int a,int b){
return a>b;//可以理解为a>b时,a放在b前面
}

#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
	return a>b;//可以理解为a>b时,a放在b前面 
}
int main(){
	int a[10]={5,8,2,4,6,1,3,7,9,0};
	sort(a,a+10,cmp);
	for(int i=0;i<10;i++)
	{cout<<a[i]<<" ";
	 } 
	 return 0;
} 

6.2.2 基本结构体数组的排序

比如定义了如下的结构体

struct node{
	int x,y;
}ssd[10];

如果想将ssd结构体数组按从大到小进行排序,则这样写cmp函数

bool cmp(node a,node b){
	return a.x>b.x;
}

如果想将ssd结构体数组按从小到大进行排序,但当x相等时候,比较y的大小来进行从小到大排序,则这样写cmp函数

bool cmp(node a,node b){
	if(a.x!=b.x)  return a.x<b.x;
	else return a.y<b.y;
}

测试代码:

#include <iostream>
#include <algorithm>
using namespace std;
struct node{
	int x,y;
}ssd[4];
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=3;
	ssd[0].y=66;//{3,66}
	ssd[1].x=2;
	ssd[1].y=77;//{2,77}
	ssd[2].x=2;
	ssd[2].y=88;//{2,88}
	ssd[3].x=1;
	ssd[3].y=99;//{1,99}
	sort(ssd,ssd+4,cmp);
	for(int i=0;i<4;i++){
		cout<<ssd[i].x<<"    "<<ssd[i].y<<endl; 
	}
	return 0;
}

输出结果:
1 99
2 77
2 88
3 66

6.2.3 容器的排序

在stl中只有vector,String,deque是可以使用sort。因为set map容器是用红黑树实现的,元素本身有序,所以不允许使用sort排序

  1. vector排序
#include <iostream>
#include <algorithm>
#include <vector>
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++){
		cout<<vi[i]<<endl; 
	}
	return 0;
}
  1. string排序
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
	string str[]={"bbbb","cc","aaa"};
	sort(str,str+3);
	for(int i=0;i<3;i++){
		cout<<str[i]<<endl; 
	}
	return 0;
}

输出结果:
aaa
bbbb
ccc

如果我们想按字符串大小的顺序进行排序,则要添加cmp函数

bool cmp(string str1,string str2){
	return str1.length()<str2.length();
}

7. lower_bound()和up_bound()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值