algorithm 头文件常用函数(转载,内有原文链接)

@[TOC]由于直接转载图片的标号全部错位,无法改正,因此复制重新排版。内有原文链接,望谅解。

(1) max()函数:返回两数的最大值。
1.用法:max(x,y) ; 注意:里面的参数只能是两个(可以是浮点型)
如果想返回x,y,z三个的最大值,可以写成 max(x,max(y,z);
#include
#include
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;
}
(2) min() 函数:返回两数的最小值
1.用法:min(x,y) ; 注意:里面的参数只能是两个(可以是浮点型)
如果想返回x,y,z三个的最大值,可以写成 min(x,min(y,z));
#include
#include
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;
}
(3) abs() 函数:求某数的绝对值
1.用法:abs(x) 注意:x必须是一个整数
如果求一个浮点型的绝对值则用math头文件下的fabs().

#include
#include<math.h>
#include
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;
}
(4) swap(x,y) :交换x,y的值

#include
#include
using namespace std;
int main() {
int a=1,b=2;
swap(a,b);
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
(5) reverse()
reverse(it,it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转。

1.将数组进行反转

#include
#include
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;
}
2.对容器(或者string字符串)进行反转

#include
#include
#include
using namespace std;
int main() {
string a=“abcdefghij”;
reverse(a.begin()+2,a.begin()+6); //将a[2]-a[5]进行反转
cout<<a;
return 0;
}
(6) next_permutation()
给出一个序列在全排列中的下一个序列

#include
#include
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;
}
(7) fill() 可以把数组或容器的某个区间赋相同的值

#include
#include
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;
}
(8) sort() 用来排序

对整型,浮点型,字符型进行排序(用的c++中的模版写的)
注意:默认排序是从小到大
#include
#include
using namespace std;
template
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;
}
2.对整型的从大到小

#include
#include
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;
}
3.对字符型的从大到小

#include
#include
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;
}
4.对浮点型从大到小

#include
#include
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;
}
5.对结构体进行排序

#include
#include
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;
}
6.对容器的排序

#include
#include
#include
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main() {
vector 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;
}
7.对string字符串排序

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

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

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


作者:LEEWLD
来源:CSDN
原文:https://blog.csdn.net/wait_13/article/details/86254025
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/m0_45128748/article/details/90761356