algorithm头文件下的常用函数-学习笔记
algorithm头文件下的常用函数-学习笔记
- max(x,y),min(x,y),abs(x)
- swap(x,y)
- reverse(it,it2)
- next_permutation()
- fill( )
- memset:
- sort()
- 如何使用
- 如何实现比较函数cmp
- lower_bound()和upper_bound()
#include < algorithm >
using namespace std;
max(x,y),min(x,y),abs(x)
max和min返回x和y中的最大值和最小值,参数可以是浮点数。如果要返回三个数的最值,可以使用max(x,max(y,z))
写法。
abs(x)返回x的绝对值,x必须是整数,浮点数使用math头文件下的fabs。
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){int x=1,y=-5;printf("%d %d\n",max(x,y),min(x,y));printf("%d %d\n",abs(x),abs(y));return 0;
}
swap(x,y)
交换x和y的值。
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){int x=1,y=-5;swap(x,y);printf("%d %d\n",x,y);return 0;
}
reverse(it,it2)
可以将数组指针在[it,it2)之间的元素或容器的迭代器在此范围内的元素进行反转。
#include <stdio.h>
#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++){printf("%d ",a[i]);} return 0;
}
对容器中的元素进行反转:
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;
int main(){string str="abcdefg";reverse(str.begin()+2,str.begin()+6); //c d e ffor(int i=0;i<str.length();i++){printf("%c ",str[i]);} return 0;
}
next_permutation()
给出一个序列在全排列中的下一个序列。
如,当n==3时的全排列:
123
132
213
231
312
321
这样213的下一个序列就是231.
#include <stdio.h>
#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)); //next_permutation在已经到达全排列的最后一个时会返回false,方便退出循环 return 0;
}
fill( )
可以把数组或容器中的某一段区间赋为某个相同的值。与memset不同,这里的赋值可以是数组类型对应范围中的任意值。
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){int a[5]={1,2,3,4,5 };fill(a,a+5,233);for(int i=0;i<5;i++){printf("%d ",a[i]);}return 0;
}
memset:
#include <string.h>
使用按字节赋值,建议使用memset赋值只赋0或-1,如对数组赋其他数字如1,使用fill函数,但memset的执行速度更快。
char型数组可以赋1,因为一个字符1比特。int型数组一个数据4比特,赋1会为数组a的前4个字节进行赋值
00000001 00000001 00000001 00000001B=16843009D
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main(){int a[5]={1,2,3,4,5 };memset(a,-1,sizeof(a)); //使用sizeof,不同的机器上int的大小可能不同 //fill(a,a+5,233);for(int i=0;i<5;i++){printf("%d ",a[i]);}return 0;
}
对二维数组或多维数组的赋值方法也是一样的,只需写数组名,不需要改变任何东西。
sort()
如何使用
sort(首元素地址,尾元素地址的下一个地址,比较函数)
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main(){double a[]={1.4,-2.1,9} ;sort(a,a+3);for(int i=0;i<3;i++){printf("%.1f ",a[i]);}return 0;
}
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main(){char a[]={'T','W','A','K'} ;sort(a,a+4);for(int i=0;i<4;i++){printf("%c",a[i]);}return 0;
}
如何实现比较函数cmp
- 基本数据类型数组的排序
比较函数不填,默认按照从小到大的顺序排序。如果想从大到小排序,则使用比较函数cmp
bool cmp(int a,int b){return a>b; //从大到小排序
}
- 结构体数组的排序
struct node{int x,y;
}ssd[10];
将ssd数组按x从大到小排序:
bool cmp(node a,node b){return a.x>b.x;
}
如果想先按x从大到小,当x相等情况下,按y从小到大排序:
bool cmp(node a,node b){if(a.x!=b.x) return a.x>b.x;else return a.y<b,y;
}
- 容器的排序
在STL标准容器中,只有**vector(变长数组)、string(字符串)、deque(双端队列)**是可以使用sort的。而set、map使用红黑树实现的,元素本身有序。
以vector为例:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#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(5);vi.push_back(8);sort(vi.begin(),vi.end(),cmp);for(int i=0;i<4;i++){printf("%d ",vi[i]);}return 0;
}
对于string的排序:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){string str[3]={"ddd","aa","u"};sort(str,str+3);for(int i=0;i<3;i++){cout<<str[i]<<" ";}return 0;
}
如果想按字符串长度从小到大排序:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool cmp(string str1,string str2){return str1.length() < str2.length();
}
int main(){string str[3]={"ddd","aa","u"};sort(str,str+3,cmp);for(int i=0;i<3;i++){cout<<str[i]<<" ";}return 0;
}
lower_bound()和upper_bound()
用在一个有序数组或容器中。
lower_bound(first,last,val)用来寻找在数组或容器的[first,last)范围内第一个值大于等于val的元素的位置。upper_bound()返回第一个值大于val的元素的位置。
如果是数组,返回该位置的指针,是容器,则返回该位置的迭代器。
如果数组或容器中没有需要寻找的元素,则两种均返回可以插入该元素的位置的指针或迭代器。
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;
int main(){int a[10]={1,2,3,3,3,4,5,5,5,8};int* lowerPos=lower_bound(a,a+10,-1);int* upperPos=upper_bound(a,a+10,-1);printf("%d, %d\n",lowerPos-a,upperPos-a); // 0,0 lowerPos=lower_bound(a,a+10,3);upperPos=upper_bound(a,a+10,3);printf("%d, %d\n",lowerPos-a,upperPos-a); // 2,5lowerPos=lower_bound(a,a+10,6);upperPos=upper_bound(a,a+10,6);printf("%d, %d\n",lowerPos-a,upperPos-a); // 9,9lowerPos=lower_bound(a,a+10,10);upperPos=upper_bound(a,a+10,10);printf("%d, %d\n",lowerPos-a,upperPos-a); // 10,10return 0;
}
algorithm头文件下的常用函数-学习笔记相关推荐
- algorithm头文件下的常用函数--习题
目录 问题 A: 求最大最小数 [简单] 问题 B: 全排列(字符串) [简单] 问题 C: 数组逆置 [简单] 问题 A: 求最大最小数 [简单] http://codeup.cn/problem. ...
- algorithm头文件下的常用函数
目录 max()和min() swap() reverse() next_permutation() fill() sort() lower_bound()和upper_bounnd() max()和 ...
- stdlib.h头文件下的常用函数
atof()将字符串转换成浮点数的函数 原形:double atof(const char *s) 功能:把s所指向的字符串转换成double类型. s格式为:符号 数字.数字 E符号 数字 返回值: ...
- algorithm头文件下常用函数
algorithm头文件下常用函数 1.max(),min(),abs() 应用:max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须是两个. abs(x) 返回x的绝对值. ...
- algorithm头文件下的sort()
1.简单的sort使用 必须加入头文件#include< algorithm >和using namespace std; 使用如下: sort(首元素地址(必填),尾元素地址的下一个地址 ...
- C++STL中string的头文件中的常用函数
文章目录 STL简介 头文件 初始化操作 实现效果 基本函数 1.size,length,capacity 2.resize,reserve 1,2的实现代码 实现效果 3.at,append,ins ...
- algorithm头文件下函数整合
使用algorithm头文件,在头文件下加一行"using namespace std:"才能正常使用 1.max(x,y).min(x,y).abs(x) 分别返回最大值.最小值 ...
- algorithm 头文件下的函数你真的都了解?
使用 algorithm 头文件,需要在头文件下加一行 "using namespace std;",才能使用. 1.max() ,min(),和abs(); max(x,y)和m ...
- linux下mkdir头文件_Linux部分常用命令学习记录
Linux部分常用命令 ls 显示目标列表 ls -a 显示所有档案及目录(ls内定将档案名或目录名称为"."的视为影藏,不会列出): ls -l 以长格式显示目录下的内容列表.输 ...
最新文章
- 让开源解读“甲骨文”--RHEL5.3部署安装Oracle Database10g Release2
- Centos普通用户提权至ROOT
- c++人脸特征保存到本地_尚邦小规模人脸识别布控系统
- 五个值得尝试的前端开发工具
- SQL Server 中的ROWID
- linux内核杂记(17)-内核链表结构(1)
- Avalonia跨平台入门第七篇之RadioButton的模板
- 《看聊天记录都学不会C#?太菜了吧》(3)变量:我大哥呢?$:小弟我罩着你!
- 学习《Building Applications with FME Objects》 之四 从数据集读取要素
- IDA使用方法-----1
- 匿名内部类 java 1614965228
- Matlab2018a安装成功后,打开出现licensing error:-8523
- cortex a7 a53_西昊人体工学椅A7开箱测评
- 电偶溶解氧传感器行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
- BP神经网络从理论到应用(一):C++实现
- vdbench的作用_Vdbench----文件系统IO,一致性校验,常见问题
- kindle阅读mobi电子书资料共享
- 云杰恒指:9.3恒指期货早盘资讯
- Java-底层建筑-JVM-第3篇-StringTable
- surface pen未接触屏幕就有反应 解决办法
热门文章
- JNI 使用,原来我一直错
- PhotoZoom Classic 7中的新功能
- python离线下载第三方库
- 皮球从某给定高度自由落下,触地后反弹到原高度的一半,再落下,再反弹,……,如此反复。问皮球在第n次落地时,在空中一共经过多少距离?第n次反弹的高度是多少?
- python星星万花筒_少儿编程分享:码趣君教你用Python编写推星星游戏(完)
- nginx解决使用域名访问项目
- 单片机包络检波c语言,模拟电子系统设计指南:从半导体、分立元件到TI集成电路的分析与实现(基础篇)...
- Ubuntu运行roscore时候报错 Unable to contact my own server at xxx的解决方法
- N - Cthulhu
- 硬件知识andl linux发展历史