继续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新特性应用--介绍几个新增的便利算法(用于排序的几个算法)的更多相关文章

  1. C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)

    总所周知.C++ STL中有个头文件,名为algorithm.即算法的意思. The header<algorithm>defines a collection of functions ...

  2. C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)

    今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了.具体信息见之前的博客. 以 ...

  3. Java 11 新特性介绍

    Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个 ...

  4. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  5. [转载] C++11新特性

    C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...

  6. C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)

    这是C++11新特性介绍的第五部分,涉及到智能指针的相关内容(shared_ptr, unique_ptr, weak_ptr). shared_ptr shared_ptr 基本用法 shared_ ...

  7. 【C++11新特性】 C++11智能指针之weak_ptr

    如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...

  8. Redis 6.0 新特性 ACL 介绍

    Redis 6.0 新特性 ACL 介绍 Intro 在 Redis 6.0 中引入了 ACL(Access Control List) 的支持,在此前的版本中 Redis 中是没有用户的概念的,其实 ...

  9. c++ 11 线程池---完全使用c++ 11新特性

    前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...

随机推荐

  1. Linux对文件归档和压缩(学习笔记八)

    一.归档和压缩 压缩命令工具:gzip,bzip2 归档命令工具:tar 二.压缩 2.1.gzip gzip是一种标准的.广泛应用的文件压缩和解压缩实用工具.gzip允许文件并置.用gzip压缩文件 ...

  2. 一些面试基本知识(Android篇一)

    Android Activity设计模式之MVC模式(介绍MVP之前的引子) 參考博客.文章 http://www.cnblogs.com/liqw/p/4175325.html MVC即Model- ...

  3. javaWeb 批量下载图片

      批量下载网页图片 CreateTime--2017年9月26日15:40:43 Author:Marydon 所用技术:javascript.java 测试浏览器:chrome 开发工具:Ecli ...

  4. idea 配置多个jdk

      1.File -->Project Structure 2.SDKs-->点击+号-->JDK-->目录选择到jdk文件夹所在的位置 3.选择jdk所在路径 4.可以配置多 ...

  5. 新浪微博api出现认证失败问题 (获取code字段值的问题)

    出现该提示的原因:`` - 说: (2015-10-30 18:06:14)回调地址不一致,`` - 说: (2015-10-30 18:07:38)请在编辑开发者信息中将网站地址和应用信息--高级信 ...

  6. linux 和windows系统下同时可用的UML建模工具(umbrello),超强

    原文地址:linux 和windows系统下同时可用的UML建模工具(umbrello),超强 作者:zhangjiakouzf OPEN SOURCE 的 UML建模工具 -- umbrello   ...

  7. 掀开图片显示介绍的css效果

    概述 主要运用到CSS3的3D transform等变换 详细 代码下载:http://www.demodashi.com/demo/10575.html 一.概述 1.主要运用到CSS3的3D tr ...

  8. 【mysql】Innodb三大特性之insert buffer

    一.什么是insert buffer insert buffer是一种特殊的数据结构(B+ tree)并不是缓存的一部分,而是物理页,当受影响的索引页不在buffer pool时缓存 secondar ...

  9. 在sys用户下执行的sql脚本创建了摁多个表和序列, 怎么回退?

    一个个删除, 暂时不会别的方法...

  10. HDUOJ---1133(卡特兰数扩展)Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...