C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)
继续C++11在头文件algorithm中添加的算法。
至少我认为,在stl的算法中,用到最多的就是sort了,我们不去探索sort的源代码。就是介绍C++11新增的几个关于排序的函数。
对于一个序列,我们怎么知道他是不是有序的呢?这就用到了:
is_sorted
原型:
template <class ForwardIterator>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Compare>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
Compare comp);
作用:
排队[first, last)是否有序。
这里须要注意的是。有两种原型。
第一种是默认的,即仅仅有两个參数,这样是推断[first, last)是否按升序排列。即<。
另外一种是三个參数的,这样是推断[first, last)区间是否按comp进行排序的。
应用:
#include <iostream> // std::cout
#include <iostream> // std::cout
#include <algorithm> // std::is_sorted, std::prev_permutation
#include <array> // std::array
bool compare(int a, int b)
{
return a>b; //升序排列,假设改为return a>b。则为降序
}
int main() {
std::array<int, 4> foo{ 2,4,1,3 };
std::array<int, 4> foo2{ 2,4,1,3 };
do {
// try a new permutation:
std::prev_permutation(foo.begin(), foo.end());
// print range:
std::cout << "foo:";
for (int& x : foo) std::cout << ' ' << x;
std::cout << '\n';
} while (!std::is_sorted(foo.begin(), foo.end()));
std::cout << "the range is sorted!\n";
do {
// try a new permutation:
std::prev_permutation(foo2.begin(), foo2.end());
// print range:
std::cout << "foo2:";
for (int& x : foo2) std::cout << ' ' << x;
std::cout << '\n';
} while (!std::is_sorted(foo2.begin(), foo2.end(), compare));
std::cout << "the range is Descending sorted!\n";
return 0;
}
//输出:
// foo: 2 3 4 1
// foo : 2 3 1 4
// foo : 2 1 4 3
// foo : 2 1 3 4
// foo : 1 4 3 2
// foo : 1 4 2 3
// foo : 1 3 4 2
// foo : 1 3 2 4
// foo : 1 2 4 3
// foo : 1 2 3 4
// the range is sorted!
// foo2 : 2 3 4 1
// foo2 : 2 3 1 4
// foo2 : 2 1 4 3
// foo2 : 2 1 3 4
// foo2 : 1 4 3 2
// foo2 : 1 4 2 3
// foo2 : 1 3 4 2
// foo2 : 1 3 2 4
// foo2 : 1 2 4 3
// foo2 : 1 2 3 4
// foo2 : 4 3 2 1
// the range is Descending sorted!
这里用到了一个全排列算法,不是C++11新增的内容,就不再赘述。
上面的代码展示了两种使用is_sorted的version。
还有一一点须要注意的是:
假设范围内的元素个数少于两个,总是返回true.
is_sorted_until
原型:
template <class ForwardIterator>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Compare>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
Compare comp);
作用:
Find first unsorted element in range
Returns an iterator to the first element in the range [first,last) which does not follow an ascending order.
If the entire range is sorted, the function returns last.
应用:
#include <iostream> // std::cout
#include <algorithm> // std::is_sorted_until, std::prev_permutation
#include <array> // std::array
int main () {
std::array<int,4> foo {2,4,1,3};
std::array<int,4>::iterator it;
do {
// try a new permutation:
std::prev_permutation(foo.begin(),foo.end());
// print range:
std::cout << "foo:";
for (int& x:foo) std::cout << ' ' << x;
it=std::is_sorted_until(foo.begin(),foo.end());
std::cout << " (" << (it-foo.begin()) << " elements sorted)\n";
} while (it!=foo.end());
std::cout << "the range is sorted!\n";
return 0;
}
C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)的更多相关文章
- C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)
总所周知.C++ STL中有个头文件,名为algorithm.即算法的意思. The header<algorithm>defines a collection of functions ...
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了.具体信息见之前的博客. 以 ...
- Java 11 新特性介绍
Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个 ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)
这是C++11新特性介绍的第五部分,涉及到智能指针的相关内容(shared_ptr, unique_ptr, weak_ptr). shared_ptr shared_ptr 基本用法 shared_ ...
- 【C++11新特性】 C++11智能指针之weak_ptr
如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...
- Redis 6.0 新特性 ACL 介绍
Redis 6.0 新特性 ACL 介绍 Intro 在 Redis 6.0 中引入了 ACL(Access Control List) 的支持,在此前的版本中 Redis 中是没有用户的概念的,其实 ...
- c++ 11 线程池---完全使用c++ 11新特性
前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...
随机推荐
- Eclipse 之使用技巧积累(一)
1.代码缩进 选中缩进代码,然后点击“Tab”键增加缩进. 2.代码退格 选中退格代码,然后按住“Shift”键,再点击“Tab”键退格. 3.无格式代码格式化 (1)默认快捷键:Ctrl + Shi ...
- 优秀web资源
http://www.filewatcher.com 一步一步asp.net_页面静态化管理 http://www.cnblogs.com/ylwn817/articles/2006923.html ...
- HDU 4620 Fruit Ninja Extreme 暴搜
题目大意:题目就是描述的水果忍者. N表示以下共有 N种切水果的方式. M表示有M个水果需要你切. W表示两次连续连击之间最大的间隔时间. 然后下N行描述的是 N种切发 第一个数字C表示这种切法可以切 ...
- linux上安装BeatifulSoup(第三方python库)
1. 什么是beatifulsoup? beatifulsoup官网http://www.crummy.com/software/BeautifulSoup/ BeatifulSoup是用Python ...
- geoip+php演示样例:通过ip,获取国家名称和代码
GeoIP + PHP的使用 方法一: 下载 GeoIP 的 PHP 文件geoip.inc,保存为 geoip.inc.php http://sjolzy.cn/php/GeoIP/bak/geoi ...
- reset.css 和 flexible.js
重置css默认样式(淘宝): body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, f ...
- MHDD修复硬盘坏道
2种修复的方法,本人已经尝试过,非常管用! 1.先按SHIFT+F3扫描硬盘连接并选择,按F4键,先用一般模式扫一遍,再用高级模式扫一变,具体方法是选择LBA模式,remap项OFF,Loop the ...
- systemctl的常用命令
#systemctl #systemctl --all #systemctl list-units --type=sokect #systemctl list-units --type=service ...
- poj 2226 二分图 最小点覆盖 , 最大流
题目就是问怎样用最小的板覆盖全部的草地.能够横着放.也能够竖着放,同意一个草地放多个点. 建图方法就是 每一个横向的草地作为X,纵向连续的草地作为Y. X连接Y的边表示, 这里有他们的公共点 ...
- 【重要】新浪微博api研究
# -*- coding: utf-8 -*- #python 27 #xiaodeng #新浪微博api研究 ''' 3.SDK的使用规则: 1)使用微博API,需要通过用户的授权,获取用户的授权码 ...