继续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. windows下的虚拟内存分配分析

    让我们从原始的进程创建开始分析吧.当进程创建后,操作系统给该进程分配4GB的虚拟地址空间,这部分虚拟内存是你的应用程序看的到的区域(注意很大一部分是不能访问的,比如:内核区域,这部分加载了操作系统中的 ...

  2. eclipse使用egit插件

    本来想用myeclipse,奈何试过网上所列的常用方法,都无法成功安装egit插件.只得转到eclipse.话说eclipse不仅是免费的,启动也较myeclipse更为迅速,安装插件也非常顺利.使用 ...

  3. poj 1879 Truck History

    本题链接:点击打开链接 题目大意: 输入n表示卡车辆数,输入每辆卡车编号.即长度为7的字符串,每辆卡车编号均可由其他类型编号衍生过来,求由当中一辆衍生出其他全部的最小衍生次数(有一个字符不同就需衍生一 ...

  4. jsoup抓取豆瓣美女

    package com.huowolf; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOu ...

  5. 算法笔记_122:蓝桥杯第七届省赛(Java语言A组)试题解答

     目录 1 煤球数目 2 生日蜡烛 3 搭积木 4 分小组 5 抽签 6 寒假作业 7 剪邮票 8 取球博弈 9 交换瓶子 10 压缩变换   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...

  6. JUC-Callable

    实现线程的方式有四种: 1,实现runnable接口: 2,继承Thread. 3,也就是本节的Callable接口. 4,使用线程池. 区别: 实现Callable接口的方式,相较于实现Runnab ...

  7. 推荐10 款 SVG 动画的 JavaScript 库

    SVG 通常可以用作跨分辨率视频.这意味着在一块高分屏幕上不会降低图片的锐度.此外,你甚至可以让SVG动起来,通过使用一些javascript类库.下面,我们分享一些javascript类库,这些类库 ...

  8. properties转yml

    分享一个在线properties 转 yml工具,也支持yml转properteis: http://toyaml.com/ 域名非常好记:to yaml .com yml,即yaml文本格式文件的后 ...

  9. 500 OOPS: chroot

    FTP登录时报错: 1.500 OOPS: chroot 解决方法:关闭SElinux 2.500 OOPS: vsftpd: refusing to run with writable root i ...

  10. Start-Sleep 帮助信息

    如下说明是翻译: help Start-Sleep 产生的帮助信息.译者: Edengundam(马涛) Start-Sleep 大纲使shell, 脚本, 或运行空间的活动挂起指定的时间. 语法St ...