c++11之algorithm算法库新增is_sorted和sorted_until
0.时刻提醒自己
Note: vector的释放
1.is_sorted
1.1 功能
检查 [first, last) 中的元素是否以不降序排序
1.2 异常
若算法无法分配内存,则抛出 std::bad_alloc
1.3 返回值
若范围中的元素已按不降序排序则为 true,否则,返回false
1.4 代码用法
// 期末成绩
int score[] = {99, 77, 30, 80, 90, 20};
// 1.排序前结果
std::cout << "排序前结果:";
for_each(std::begin(score), std::end(score), [](const int& item) {std::cout << item << " "; });
std::cout << " : is_sorted: " << std::boolalpha << std::is_sorted(std::begin(score), std::end(score)) << '\n';
// 2.执行排序
std::sort(std::begin(score), std::end(score));
// 3.排序后结果
std::cout << "排序后结果:";
for_each(std::begin(score), std::end(score), [](const int& item) {std::cout << item << " "; });
std::cout << " : is_sorted: " << std::boolalpha << std::is_sorted(std::begin(score), std::end(score)) << '\n';
1.5 输出结果
2. is_sorted_until
2.1 功能
检验范围 [first, last) ,并寻找始于 first 且其中元素已以不降序排序的最大范围。
2.2 异常
若算法无法分配内存,则抛出 std::bad_alloc
2.3 返回值
始于 first 且其中元素已以升序排序的最大范围。即满足范围 [first, it) 已排序的最后迭代器 it 。
2.4 Note
std::is_sorted_until 对空范围及长为 1 的范围均返回 last 。
2.5 用法
// 期末成绩
int score[] = {99, 77, 30, 80};
std::cout << "排序前:";
// 1.找到第一个没有按升序排列的元素
auto it = std::is_sorted_until(std::begin(score), std::end(score));
if (std::end(score) != it)
std::cout << "第一个没有升序排列的元素是:" << *it << std::endl;
else
std::cout << "数组元素全部按照升序排序\n";
// 2.执行排序
std::sort(std::begin(score), std::end(score));
// 3.再次核查数组排序情况
std::cout << "排序后:";
auto it_find = std::is_sorted_until(std::begin(score), std::end(score));
if (std::end(score) != it_find)
std::cout << "第一个没有升序排列的元素是:" << *it_find << std::endl;
else
std::cout << "数组元素全部按照升序排序\n";
2.6 输出结果
c++11之algorithm算法库新增is_sorted和sorted_until的更多相关文章
- c++11之 algorithm 算法库新增 minmax_element同时计算最大值和最小值
0.时刻提醒自己 Note: vector的释放 1. minmax_element 功能 寻找范围 [first, last) 中最小和最大的元素. 2. 头文件 #include <algo ...
- C++ algorithm算法库
C++ algorithm算法库 Xun 标准模板库(STL)中定义了很多的常用算法,这些算法主要定义在<algorithm>中.编程时,只需要在文件中加入#include<algo ...
- C++神奇算法库——#include<algorithm>
算法(Algorithm)为一个计算的具体步骤,常用于计算.数据处理和自动推理.C++ 算法库(Algorithms library)为 C++ 程序提供了大量可以用来对容器及其它序列进行算法操作的函 ...
- scikit-learn 线性回归算法库小结
scikit-learn对于线性回归提供了比较多的类库,这些类库都可以用来做线性回归分析,本文就对这些类库的使用做一个总结,重点讲述这些线性回归算法库的不同和各自的使用场景. 线性回归的目的是要得到输 ...
- 算法库:Matlab与C++混合编程
算法库:Matlab与C++混合编程 最近做光流算法预演过程中,下载的源码中涉及到了Matlab和C++的混合编程.在同事Matlab2014的环境下,程序到是一下就运行通过了.但在我这Matlab2 ...
- 2019年11个javascript机器学习库
Credits: aijs.rocks 虽然python或r编程语言有一个相对容易的学习曲线,但是Web开发人员更喜欢在他们舒适的javascript区域内做事情.目前来看,node.js已经开始向每 ...
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
std::sort 对vector成员进行排序; std::sort(v.begin(),v.end(),compare); std::lower_bound 在排序的vector中进行 ...
- mahout算法库(四)
mahout算法库 分为三大块 1.聚类算法 2.协同过滤算法(一般用于推荐) 协同过滤算法也可以称为推荐算法!!! 3.分类算法 算法类 算法名 中文名 分类算法 Log ...
- 一个好用的多方隐私求交算法库JasonCeng/MultipartyPSI-Pro
Github链接传送:JasonCeng/MultipartyPSI-Pro 大家好,我是阿创,这是我的第29篇原创文章. 今天是一篇纯技术性文章,希望对工程狮们有所帮助. 向大家推荐一个我最近改造的 ...
随机推荐
- kubernetes部署 flannel网络组件
创建 flannel 证书和私钥flannel 从 etcd 集群存取网段分配信息,而 etcd 集群启用了双向 x509 证书认证,所以需要为 flanneld 生成证书和私钥. cat > ...
- 学习java 7.16
学习内容: 线程安全的类 Lock锁 生产者消费者模式 Object类的等待唤醒方法 明天内容: 网络编程 通信程序 遇到问题: 无
- acquire, acre, across
acquire An acquired taste is an appreciation [鉴赏] for something unlikely to be enjoyed by a person w ...
- 《Scala编程》课程作业
第一题.百元喝酒 作业要求:每瓶啤酒2元,3个空酒瓶或者5个瓶盖可换1瓶啤酒.100元最多可喝多少瓶啤酒?(不允许借啤酒) 思路:利用递归算法,一次性买完,然后递归算出瓶盖和空瓶能换的啤酒数 /** ...
- 【leetcode】43. Multiply Strings(大数相乘)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- IDEA 超实用使用技巧分享
前言 工欲善其事 必先利其器 最近受部门的邀请,给入职新人统一培训IDEA,发现有很多新人虽然日常开发使用的是IDEA,但是还是很多好用的技巧没有用到,只是用到一些基本的功能,蛮浪费IDEA这个优 ...
- Insert into select语句引发的生产事故
前言 Insert into select请慎用.这天xxx接到一个需求,需要将表A的数据迁移到表B中去做一个备份.本想通过程序先查询查出来然后批量插入.但xxx觉得这样有点慢,需要耗费大量的网络 ...
- iBatis查询时报"列名无效"或"找不到栏位名称"无列名的错误原因及解决方法
iBatis会自动缓存每条查询语句的列名映射,对于动态查询字段或分页查询等queryForPage, queryForList,就可能产生"列名无效".rs.getObject(o ...
- Output of C++ Program | Set 5
Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...
- OC简单介绍
一.OC与C的对比 关键字 OC新增的关键字在使用时,注意部分关键字以"@"开头 方法->函数 定义与实现 数据类型 新增:BOOL/NSObject/id/SEL/bloc ...