
algorithm 头文件常用函数(转载,内有原文链接)
发布日期:2021-05-07 18:29:18
浏览次数:22
分类:精选文章
本文共 4213 字,大约阅读时间需要 14 分钟。
@[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 原文: 版权声明:本文为博主原创文章,转载请附上博文链接!发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月19日 02时35分43秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
设计模式(18)——中介者模式
2021-05-12
用JavaScript实现希尔排序
2021-05-12
推荐几篇近期必看的视觉综述,含GAN、Transformer、人脸超分辨、遥感等
2021-05-12
BUU-MISC-认真你就输了
2021-05-12
BUU-MISC-caesar
2021-05-12
【专题2:电子工程师 之 上位机】 之 【36.事件重载】
2021-05-12
【专题3:电子工程师 之 上位机】 之 【46.QT音频接口】
2021-05-12
一文理解设计模式--命令模式(Command)
2021-05-12
VTK:可视化之RandomProbe
2021-05-12
block多队列分析 - 2. block多队列的初始化
2021-05-12
Java时间
2021-05-12
不编译只打包system或者vendor image命令
2021-05-12
The wxWindows Library Licence (WXwindows)
2021-05-12
leetcode——第203题——虚拟头结点
2021-05-12
【编程】C语言入门:1到 100 的所有整数中出现多少个数字9
2021-05-12
MySQL----基础及常用命令
2021-05-12
flink启动(二)
2021-05-12
前端开发进阶手册.pdf
2021-05-12