algorithm头文件下常用函数
algorithm头文件下常用函数
1.max(),min(),abs()
应用:max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须是两个。
abs(x) 返回x的绝对值。x必须为整数,浮点型的绝对值要用math头文件下的fabs
2.count()
应用:计算某区间内某元素出现的次数
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int a[6] = {1, 2, 2, 3, 5, 5};int cnt = count(a, a+6, 2);cout << cnt << endl; // 2return 0;
}
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{string str = "Hello world!";int cnt = count(str.begin(), str.end(), 'l');cout << cnt << endl; // 3return 0;
}
3.find()
应用:返回第一次出现的指针或迭代器,通过做差可获得在数组或者向量中的下标
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int a[] = {1, 2, 2, 5, 4};int pos = find(a, a+5, 5) - a;cout << pos << endl; //输出3,为该元素的下标
}
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{string str = "Hello world!";int pos = find(str.begin(), str.end(), 'l') - str.begin();cout << pos << endl; //2return 0;
}
2.swap()
应用:swap(x,y)用来交换x和y的值
3.reverse()
应用:reverse(it,it2) 可以将数组指针在[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); //反转前四个元素for(int i=0;i<6;i++){ printf("%d ",a[i]); //13 12 11 10 14 15}return 0;
}#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{string str="abcdefghi";reverse(str.begin()+2,str.begin()+6); //反转第三个到第六个元素for(int i=0;i<str.length();i++){ printf("%c",str[i]); //abfedcghi}return 0;
}
4.next_permutation()
应用:next_permutation() 给出一个序列在全排列中的下一个序列
#include<stdio.h>
#include<string>
#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));return 0;
}
5. fill()
应用:fill() 可以把数组或容器中的某一段区间赋为某个相同的值。和memset不同,这里的赋值可以使数组类型对应范围中的任意值。
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{int a[10]={1,2,3,4,5};fill(a,a+5,666);for(int i=0;i<5;i++){printf("%d ",a[i]);}return 0;
}
6. sort()
应用:排序,默认为递增排序
- 若要递减排序,需要增加比较函数
bool cmp(int a,int b){return a>b;
}
sort(a,a+n,cmp);
- 结构体数组排序
struct node{int x,y;
};
bool cmp(node a,node b){return a.x>b.x;
}
- 容器排序,在STL标砖容器中,只有vector/string/deque可以sort
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
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(2);sort(vi.begin(),vi.end(),cmp);for(int i=0;i<3;i++){printf("%d ",vi[i]);}return 0;
}
7. lower_bound()和upper_bound()
应用:lower_bound 和 upper_bound()需要用在一个有序数组或容器中。
lower_bound(first,last,val) 用来寻找在数组或容器的[first,last)范围内第一个值大于等于
val元素的位置,如果是数组,返回该位置的指针;若果是容器,返回该位置的迭代器
upper_bound(first,last,val) 用来寻找在数组或容器的[first,last)范围内第一个值大于
val元素的位置,如果是数组,返回该位置的指针;若果是容器,返回该位置的迭代器
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{int a[10]={1,2,2,3,3,3,5,5,5,5};printf("%d,%d\n",lower_bound(a,a+10,3)-a,upper_bound(a,a+10,3)-a);return 0;
}
8.set_intersection()、set_union()和set_difference()
应用:求交集、并集、余集
#include<vector>
using namespace std;
int main(){vector<int> a;
vector<int> b;
vector<int> c;
set_intersection(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
set_union(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
set_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
}
algorithm头文件下常用函数相关推荐
- algorithm 头文件下的函数你真的都了解?
使用 algorithm 头文件,需要在头文件下加一行 "using namespace std;",才能使用. 1.max() ,min(),和abs(); max(x,y)和m ...
- 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) 分别返回最大值.最小值 ...
- algorithm头文件下的常用函数--习题
目录 问题 A: 求最大最小数 [简单] 问题 B: 全排列(字符串) [简单] 问题 C: 数组逆置 [简单] 问题 A: 求最大最小数 [简单] http://codeup.cn/problem. ...
- algorithm头文件下的常用函数
目录 max()和min() swap() reverse() next_permutation() fill() sort() lower_bound()和upper_bounnd() max()和 ...
- C语言—sort函数比较大小的快捷使用--algorithm头文件下
sort函数 一般情况下要将一组数从的大到小排序或从小到大排序,要定义一个新的函数排序. 而我们也可以直接使用在函数下的sort函数,只需加上头文件: #include<algorithm> ...
- algorithm头文件下的next_permutation()
next_permutation给出一个序列在全排列中的下一个序列 举例 #include<iostream> #include<algorithm> using namesp ...
- algorithm头文件下的fill()
fill()可以把数组或容器中的某一段区间赋予相同的值. 和memset不同,这里可以是数组类型对应范围中的任意值. 示例如下: #include<iostream> #include&l ...
最新文章
- 什么是mysql的游标_MySQL游标概念是什么 MySQL游标概念与用法介绍
- oracle动态采样超时,解决 ORACLE 11.2 动态采样导致的性能问题
- P2179-[NOI2012]骑行川藏【导数,二分】
- 儿童python编程能给孩子带来哪些好处_python编程入门学习对孩子成长有哪些优势?...
- 怎样恢复oracle函数,Oracle闪回恢复误删除的表、存储过程、包、函数...
- python path模块_python pathlib模块详解
- 四步破解大亚DP607超级密码,别的光猫可能也适用!
- Bat脚本处理ftp超强案例解说
- 关于秩的等式与不等式总结
- 36. In Depth Magento System Configuration
- l7sa008b故障代码_Robertshaw 365-A8振动开关故障探测器
- python爬取网页数据出现中文乱码解决办法
- 解决win10家庭版本系统无法远程连接问题
- 网络工程师的python之路-FTP备份配置文件
- 在线客服系统代码安装 (附移动版APP下载)
- C++下实现全连接神经网络
- 【目标检测竞赛总结】IEEE UV 2022 “Vision Meets Algae” Object Detection Challenge
- CPU占用率100%解决方法
- 如何修改oa服务器地址,oa服务器地址设置
- 使用elementUI中的date-picker组件年月日显示英文
热门文章
- Windows怎么查看苹果heic文件
- HTTTP 缓存机制
- 几行JS代码实现b站直播间弹幕抽奖
- linux top buff cache,linux – 顶部的“buff / cache”和“avail mem”字段是什么意思?...
- AE-新知识-关于美女头发,衣服飘动的效果
- MySQL 添加、更新和删除数据
- python的发展前景与展望_Python未来的发展前景到底如何?
- InsecureRequestWarning: Unverified HTTPS request is being made to host ‘api.ai.qq.com‘. Adding
- 译算法交易策略的成功测一
- 基于springboot layui新闻发布网站前后端源码