algorithm 头文件常用函数

(1) max()函数:返回两数的最大值。
1.用法:max(x,y) ; 注意:里面的参数只能是两个(可以是浮点型)
如果想返回x,y,z三个的最大值,可以写成 max(x,max(y,z);

#include <iostream>
#include<algorithm> 
using namespace std;
int main() {
	int x=10,y=15,z=20;
	int a=max(x,y);  //求两个数的最大值
	int b=max(x,max(x,z)); //求三个数的最大值
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(2) min() 函数:返回两数的最小值
1.用法:min(x,y) ; 注意:里面的参数只能是两个(可以是浮点型)
如果想返回x,y,z三个的最大值,可以写成 min(x,min(y,z));

#include <iostream>
#include<algorithm>
using namespace std;
int main() {
	int x=10,y=15,z=20;
	int a=min(x,y);
	int b=min(x,min(x,z));
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(3) abs() 函数:求某数的绝对值
1.用法:abs(x) 注意:x必须是一个整数
如果求一个浮点型的绝对值则用math头文件下的fabs().

#include <iostream>
#include<math.h>
#include <algorithm> 
using namespace std;
int main() {
	int x,y;
	x=-10;
	y=-1.2;
	int a=abs(x);
	int b=fabs(y);
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

(4) swap(x,y) :交换x,y的值

#include <iostream>
#include<algorithm>
using namespace std; 
int main() {
	int a=1,b=2;
	swap(a,b);
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(5) reverse()
reverse(it,it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转。

1.将数组进行反转

#include <iostream>
#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++)
	{
		cout<<a[i]<<" ";  
	}
	return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.对容器(或者string字符串)进行反转

#include <iostream>
#include<string> 
#include<algorithm>
using namespace std; 
int main() {
	string a="abcdefghij";
	reverse(a.begin()+2,a.begin()+6);  //将a[2]-a[5]进行反转
	cout<<a;
	return 0; 

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

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

#include <iostream>
#include<algorithm>
using namespace std;

int main() {
int a[3]={1,2,3};
do
{
cout<<a[0]<<" “<<a[1]<<” "<<a[2]<<endl;
}while(next_permutation(a,a+3));
return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

(7) fill() 可以把数组或容器的某个区间赋相同的值

#include <iostream>
#include<algorithm>
using namespace std; 
int main() {
	int a[5]{1,2,3,4,5};
	fill(a,a+5,233); //[a,a+5)是区间,233是值
	for(int i=0;i<5;i++)
	{
		cout<<a[i]<<" ";
	} 
	cout<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

(8) sort() 用来排序

  1. 对整型,浮点型,字符型进行排序(用的c++中的模版写的)
    注意:默认排序是从小到大
#include <iostream>
#include<algorithm> 
using namespace std;
template<typename T>
void print(T arr[])  //输出函数
{
	for(int i=0;i<5;i++)
	{
		cout<<arr[i]<<" ";
	}
}
int main() {
	int arr[5]={9,8,5,3,6};
	char ch[5]={'h','a','m','n','i'};
	double d[5]={1.1,1.6,1.5,1.8,2.1};
	sort(arr,arr+5);
	print(arr);
	cout<<endl;
	sort(ch,ch+5);
	print(ch);
	cout<<endl;
	sort(d,d+5);
	print(d);
	cout<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2.对整型的从大到小

#include <iostream>
#include<algorithm> 
using namespace std;

bool cmp(int a,int b) //从大大小需要加这个
{
return a>b;
}
int main() {
int arr[5]={8,9,5,4,3};
sort(arr,arr+5,cmp);
for(int i=0;i<5;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.对字符型的从大到小

#include <iostream>
#include<algorithm> 
using namespace std;

bool cmp(char a,char b)
{
return a>b;
}
int main() {
char ch[5]={‘h’,‘a’,‘m’,‘n’,‘i’};
sort(ch,ch+5,cmp);
for(int i=0;i<5;i++)
{
cout<<ch[i]<<" ";
}
cout<<endl;
return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4.对浮点型从大到小

#include <iostream>
#include<algorithm> 
using namespace std;

bool cmp(double a,double b)
{
return a>b;
}
int main() {
double d[5]={1.1,1.6,1.5,1.8,2.1};
sort(d,d+5,cmp);
for(int i=0;i<5;i++)
{
cout<<d[i]<<" ";
}
cout<<endl;
return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

5.对结构体进行排序

#include <iostream>
#include<algorithm> 
using namespace std;
struct Node
{
	int x;
	char y;
}arr[3];
bool cmp(Node a,Node b)
{
	if(a.x!=b.x)
	return a.x>b.x;
	else
	return a.y>b.y;
}
int main() {
	arr[0].x=5;
	arr[0].y='H';
	arr[1].x=5;
	arr[1].y='A';
	arr[2].x=3;
	arr[2].y='B'; 
	sort(arr,arr+3,cmp);
		for(int i=0;i<3;i++)
	{
		cout<<arr[i].x<<" "<<arr[i].y<<endl;
	}
	cout<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6.对容器的排序

#include <iostream>
#include<vector> 
#include<algorithm> 
using namespace std;
bool cmp(int a,int b)
{
	return a>b;
}
int main() {
	vector<int> s;
	s.push_back(3);
	s.push_back(9);
	s.push_back(11); 
	sort(s.begin(),s.end(),cmp);
	for(int i=0;i<3;i++)
	{
		cout<<s[i]<<" "; 
	}
	cout<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

7.对string字符串排序

#include <iostream>
#include<vector> 
#include<algorithm> 
using namespace std;
int main() {
	string str[3]={"bcd","abc","efg"}; 
	sort(str,str+3);  //字符串从小到大排序
	for(int i=0;i<3;i++)
	{
		cout<<str[i]<<" "; 
	}
	cout<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

(8) lower_bound() upper_bound需要在一个有序的容器或者数组;
1.lower_bound(first,last,val):用来寻找在数组或者容器的[first,last)范围内第一个大于等于val的元素的位置,如果是数组,则返回该位置的指针,如果是容器,则返回该位置的迭代器

2.upper_bound(first,last,val):用来寻找在数组或者容器的[first,last)范围内第一个大于val的元素的位置,如果是数组,则返回该位置的指针,如果是容器,则返回该位置的迭代器

#include <iostream>
#include<algorithm>
using namespace std;
int main() {
	int a[10]={1,3,3,3,5,7,9,10,15,15};
	//寻找3的位置 ,下标从0开始 
	cout<<lower_bound(a,a+10,3)-a<<endl;
	cout<<upper_bound(a,a+10,3)-a<<endl; 
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
                </div>
发布了27 篇原创文章 · 获赞 16 · 访问量 1979

(1) max()函数:返回两数的最大值。
1.用法:max(x,y) ; 注意:里面的参数只能是两个(可以是浮点型)
如果想返回x,y,z三个的最大值,可以写成 max(x,max(y,z);

#include <iostream>
#include<algorithm> 
using namespace std;
int main() {
	int x=10,y=15,z=20;
	int a=max(x,y);  //求两个数的最大值
	int b=max(x,max(x,z)); //求三个数的最大值
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(2) min() 函数:返回两数的最小值
1.用法:min(x,y) ; 注意:里面的参数只能是两个(可以是浮点型)
如果想返回x,y,z三个的最大值,可以写成 min(x,min(y,z));

#include <iostream>
#include<algorithm>
using namespace std;
int main() {
	int x=10,y=15,z=20;
	int a=min(x,y);
	int b=min(x,min(x,z));
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(3) abs() 函数:求某数的绝对值
1.用法:abs(x) 注意:x必须是一个整数
如果求一个浮点型的绝对值则用math头文件下的fabs().

#include <iostream>
#include<math.h>
#include <algorithm> 
using namespace std;
int main() {
	int x,y;
	x=-10;
	y=-1.2;
	int a=abs(x);
	int b=fabs(y);
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

(4) swap(x,y) :交换x,y的值

#include <iostream>
#include<algorithm>
using namespace std; 
int main() {
	int a=1,b=2;
	swap(a,b);
	cout<<a<<endl;
	cout<<b<<endl;
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(5) reverse()
reverse(it,it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转。

1.将数组进行反转

#include <iostream>
#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++)
	{
		cout<<a[i]<<" ";  
	}
	return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.对容器(或者string字符串)进行反转

#include <iostream>
#include<string> 
#include<algorithm>
using namespace std; 
int main() {
	string a="abcdefghij";
	reverse(a.begin()+2,a.begin()+6);  //将a[2]-a[5]进行反转
	cout<<a;
	return 0; 

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

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

#include <iostream>
#include<algorithm>
using namespace std;

猜你喜欢

转载自blog.csdn.net/weixin_43899069/article/details/98317428