继续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. rmdir 命令(转)

    原文:http://www.cnblogs.com/peida/archive/2012/10/27/2742076.html rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目 ...

  2. VScode-Go can't load package: package .: no buildable Go source files in

    在VScode中调试Go程序时提示: can't load package: package .: no buildable Go source files in d:\my_workspace\go ...

  3. 3DMax脚本插件--改动材质&amp;贴图名称

    从网上淘到了一套人物的模型,当时的心情是激动无比,掏出用的不熟练的3DMax折腾了半天.突然发现了一个蛋疼的事儿,所有的模型文件,材质名称,子材质,以及贴图所实用的是中文命名!! ! 尽管说是能跑,只 ...

  4. 寻找[nginx] 由Lua 粘合的Nginx生态环境-- agentzh

    来自:linuxtone org     Chnangelog:         120312 fixed as s/hhttp/http/g ,thanx muxueqz         12030 ...

  5. ActiveMQ基本介绍

    1.ActiveMQ服务器工作模型       通过ActiveMQ消息服务交换消息.消息生产者将消息发送至消息服务,消息消费者则从消息服务接收这些消息.这些消息传送操作是使用一组实现 ActiveM ...

  6. 17、JAVA流程控制

    一.IF 1.第一种形式:if if(逻辑表达式){ 语句1; 语句2; ... } 当if中只有一条语句时,可以省略{} 2.第二种形式:if-else if(逻辑表达式){ 语句块1: } els ...

  7. Eclipse c++ 编译调试

    直接添加源文件方法: 右键选择工程->import->General->File System,在弹出的对话框中选择源文件目录,筛选文件后: 1.如果直接加到工程中,点Finish就 ...

  8. STL容器 erase的使用陷井

    http://www.cppblog.com/beautykingdom/archive/2008/07/09/55760.aspx?opt=admin 在STL(标准模板库)中经常会碰到要删除容器中 ...

  9. Python 实现的、带GUI界面的词云生成器

    代码地址如下:http://www.demodashi.com/demo/14233.html 详细说明: "词云"就是数据可视化的一种形式,给出一段文本,根据文本中词语的出现频率 ...

  10. iOS-启动动态页跳过设计思路

    概述 根据UIBezierPath和CAShapeLayer自定义倒计时进度条,适用于app启动的时候设置一个倒计时关闭启动页面.可以设置进度条颜色,填充颜色,进度条宽度以及点击事件等. 详细 代码下 ...