栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

algorithm头文件下的常用函数
  • 1. max() min()和abs()
  • 2. swap
  • 3. reverse
  • 5. fill()
  • 6.1 sort()
  • 6.2比较函数 cmp
    • 6.2.1 基本数据类型的数组排序
    • 6.2.2 基本结构体数组的排序
    • 6.2.3 容器的排序
  • 7. lower_bound()和up_bound()

摘自胡凡《算法笔记》

1. max() min()和abs()
#include  
#include 
using namespace std;
int main(){
	int x=1,y=-2;
	printf("%d %dn",max(x,y),min(x,y));
	printf("%d %dn",abs(x),abs(y));
	return 0;
}
2. swap
#include  
#include 
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  
#include 
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 
using namespace std;
int main(){
	int a[10]={1,2,3};
	do{
		printf("%d %d %dn",a[0],a[1],a[2]);
	}while(next_permutation(a,a+3));
	return 0;
}
5. fill()
#include  
#include 
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 
#include  
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< 

运行结果: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 
#include 
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< 
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 

测试代码:

#include 
#include 
using namespace std;
struct node{
	int x,y;
}ssd[4];
bool cmp(node a,node b){
	if(a.x!=b.x) return a.x 

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

6.2.3 容器的排序

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

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

输出结果:
aaa
bbbb
ccc

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

bool cmp(string str1,string str2){
	return str1.length() 
7. lower_bound()和up_bound()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号