algorithm 头文件下的函数你真的都了解?
使用 algorithm 头文件,需要在头文件下加一行 “using namespace std;”,才能使用。
1.max() ,min(),和abs();
max(x,y)和min(x,y) 分别返回 x 和 y 中的最大者和最小值,且参数必须为两个(可以是浮点型)。如果想要返回三个数的最大值,可以使用 max(x,max(y,z))的写法。
abs(x)返回x的绝对值。注意:x 必须为整数,浮点型的绝对值请使用 math 头文件下的 fads()。
2.swap();
swap(x,y)用来交换 x和 y的值。
//swap(&x,&y);
//void swap(int *x,int *y)
//{
// *x^=*y;
// *y^=*x;
// *x^=*y;
//}
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int x=1,y=2;swap(x,y);cout<<x<<" "<<y;return 0;
}
// 输出 2 1
3.reverse()
reverse(it,it2); 可以将数组指针在 [it,it2) 之间的元素或容器的迭代器在 [it,it2)范围内的元素进行反转。示例如下:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int a[10]={10,11,12,13,14,15};reverse(a,a+4);for(int i=0;i<6;i++){cout<<a[i]<<" ";} return 0;
}
// 输出 13 12 11 10 14 15
如果是对容器中的元素(例如 string 字符串)进行反转,结果也是一样,让我们来看一下吧~
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{string str="abcdefghij"; reverse(str.begin()+2,str.begin()+6);for(int i=0;i<str.length() ;i++) //我们知道 vector 和 string 容器都支持下标访问 {cout<<str[i]<<" ";} return 0;
}
// 输出 a b f e d c g h i j
4.next_permutation()
next_permutation() 给出一个序列在全排列中的下一个序列。
例如:当 n=3 时的全排列为:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
则 2 3 1 的下一个序列就是 3 1 2;好了那么让我们一起来了解一下吧~
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int a[10]={1,2,3};do{cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;}while(next_permutation(a,a+3)); return 0;
}
5.fill()
fill() 可以把数组或容器中的某一段区间赋为某个相同的值,和 memset 不同,这里的赋值可以是数组类型对应范围中的任意值。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int a[5]={1,2,3,4,5};fill(a,a+5,999);for(int i=0;i<5;i++){cout<<a[i]<<endl;}return 0;
}
6.sort()
这里重点讲的是对容器的排序:
这里需要注意STL标准容器中,只有 vector , string ,deque 是可以使用 sort() 的,这是因为像 set ,map 容器是由红黑树实现的,元素本身有序,故不允许使用 sort() 排序。
下面以 vector 容器为例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{return a>b;
}
int main()
{vector<int> v;v.push_back(3);v.push_back(1);v.push_back(2);sort(v.begin(),v.end(),cmp);for(int i=0;i<3;i++){cout<<v[i];}return 0;
}
string 的排序:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(string a,string b)
{return a>b;
}
int main()
{string str[]={"bbbb","cc","aaa"}; //字典序大小进行排序 sort(str,str+3,cmp);for(int i=0;i<3;i++){cout<<str[i]<<endl;}return 0;
}
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(string a,string b)
{return a.length()>b.length();
}
int main()
{string str[]={"bbbb","cc","aaa"}; //string 的长度进行排序 sort(str,str+3,cmp);for(int i=0;i<3;i++){cout<<str[i]<<endl;}return 0;
}
7.最后要提到的是 lower_bound() 和 upper_bound()
它需要用在一个有序数组或容器中。
这一部分我们已经在之前讨论过,这里不再赘述。
algorithm 头文件下的函数你真的都了解?相关推荐
- algorithm头文件下常用函数
algorithm头文件下常用函数 1.max(),min(),abs() 应用:max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须是两个. abs(x) 返回x的绝对值. ...
- algorithm头文件下的常用函数-学习笔记
algorithm头文件下的常用函数-学习笔记 max(x,y),min(x,y),abs(x) swap(x,y) reverse(it,it2) next_permutation() fill( ...
- algorithm头文件下的sort()
1.简单的sort使用 必须加入头文件#include< algorithm >和using namespace std; 使用如下: sort(首元素地址(必填),尾元素地址的下一个地址 ...
- algorithm头文件下函数整合
使用algorithm头文件,在头文件下加一行"using namespace std:"才能正常使用 1.max(x,y).min(x,y).abs(x) 分别返回最大值.最小值 ...
- C语言—sort函数比较大小的快捷使用--algorithm头文件下
sort函数 一般情况下要将一组数从的大到小排序或从小到大排序,要定义一个新的函数排序. 而我们也可以直接使用在函数下的sort函数,只需加上头文件: #include<algorithm> ...
- algorithm头文件下的常用函数--习题
目录 问题 A: 求最大最小数 [简单] 问题 B: 全排列(字符串) [简单] 问题 C: 数组逆置 [简单] 问题 A: 求最大最小数 [简单] http://codeup.cn/problem. ...
- algorithm头文件下的常用函数
目录 max()和min() swap() reverse() next_permutation() fill() sort() lower_bound()和upper_bounnd() max()和 ...
- algorithm头文件下的next_permutation()
next_permutation给出一个序列在全排列中的下一个序列 举例 #include<iostream> #include<algorithm> using namesp ...
- algorithm头文件下的fill()
fill()可以把数组或容器中的某一段区间赋予相同的值. 和memset不同,这里可以是数组类型对应范围中的任意值. 示例如下: #include<iostream> #include&l ...
最新文章
- JDK1.8快速入门
- gis可达性分析步骤_这个 Python 项目厉害了!多个实战案例教你分析时空数据处理...
- MySQL 主键冲突,无法插入数据
- 2019第十届蓝桥杯C/C++ A组省赛 —— 第二题: 数列求值
- 向DataGridView中添加新的一行数据,可以添加到最后一行或作为第一行
- (十)nodejs循序渐进-高性能游戏服务器框架pomelo之介绍和安装篇
- Flink 学习(一)
- 视讯稳定对接出现的问题
- TCP/IP 基础简介
- HBase实战 | 从MySQL到HBase:数据存储方案转型的演进
- flex4与java_Flex4与java传递对象
- Excel与用友ERP-U8的数据集成方法(一)
- 计算机应用基础制作新春贺卡图片,WORD制作新年贺卡和个人简历大学计算机基础课程设计报告书...
- DIMM DDR 区别和联系
- FontAwesome for Axure字体图标合集 v4.7 — v5.8 含Free版和Pro版
- 在sublime中如何将html代码格式化!
- 团队项目代码分析(Android游戏:别踩白块儿)
- 怎样在PPT中抠图?这样操作30秒搞定!
- Android中级面筋:开发2年的程序员如何短期突击面试?跟着这几步去准备,大厂也不远了
- 第六章 Linux实际操作——实用指令
热门文章
- 使用Ubuntu Pastebin分享文本
- spring boot高校机房自动排课系统 毕业设计-附源码211004
- 【算法】机器人走迷宫破壁解法(适用于走迷宫、最短路径算法)-20200412
- 小红书显示找不到服务器,小红书崩了是怎么回事
- 漏刻有时物联网传感器API接口对接说明文档
- 电压电流双环控制PI参数计算01
- 笔记本配置VM虚拟机没有摄像头情况解决
- ES更新错误 Rejecting mapping update to [] as the final mapping would have more than 1 type
- 【C#】三层登录机房重构
- js根据输入的地址获得经纬度(百度地图)