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;
}
时间: 2024-08-10 16:58:00

C++11新特性应用--介绍几个新增的便利算法(用于排序的几个算法)的相关文章

C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)

有的时候为了维护数据,我们使用stl的堆去维护一序列. 首先您要弄清楚堆和栈的区别,即heap和stack stl中的堆默认是最大堆. 先介绍 push_heap,pop_heap,make_heap,sort_heap这四个算法,这四个不是C++11新增加的内容. 首先是如何产生一个最大推: make_heap 原型: template <class RandomAccessIterator> void make_heap (RandomAccessIterator first, Rando

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

昨天罗列了C++11中新增的几个算法,包括 find_if_not.all_of.any_of.none_of四个算法,这四个算法的共同点就是Non-modifying sequence operations. 所以,今天就来八一八C++11中新增的算法,而这些算法的特点是:Modifying sequence operations. copy算法我们很熟悉,这里介绍一下C++11新增的copy_n. copy_n 原型: template <class InputIterator, class

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

今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了,详细信息见之前的博客. 下面开始C++11新增的关于Partitions的算法: Partitions:即分区的意思. 很多人可能还不熟悉partition,所以先说一说partition算法,需要说明的是这不是C++11新增的内容.但为了更方便大家理解,还是先写一写std::partition. std::partitio

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

总所周知,C++ STL中有个头文件,名为algorithm,即算法的意思. The header<algorithm>defines a collection of functions especially designed to be used on ranges of elements. 所以,要八一八这个头文件中C++11新增的几个算法,今天主要描述的几个算法不改变容器中元素的顺序. 这里还要啰嗦一句,使用stl算法时,如果与lambda表达式组合使用,那么代码会更加简洁. find_

Java 11 新特性介绍

Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个大版本,每个季度发布一个中间特性版本,并且做出不会跳票的承诺.通过这样的方式,Java 开发团队能够将一些重要特性尽早的合并到 Java Release 版本中,以便快速得到开发者的反馈,避免出现类似 Java 9 发布时的两次延期的情况. 按照官方介绍,新的版本发布周期将会严格按照时间节点,于每年

C++11新特性:自动类型推断和类型获取

声明:本文是在Alex Allain的文章http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html的基础上写成的. 加入了很多个人的理解,不是翻译. 转载请注明出处 http://blog.csdn.net/srzhz/article/details/7934483 自动类型推断 当编译器能够在一个变量的声明时候就推断出它的类型,那么你就能够用auto关键字来作为他们的类型: [c

C++11新特性:Lambda函数(匿名函数)

声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html 加入了自己的理解,不是简单的翻译 C++11终于知道要在语言中加入匿名函数了.匿名函数在很多时候可以为编码提供便利,这在下文会提到.很多语言中的匿名函数,如C++,都是用Lambda表达式实现的.Lambda表达式又称为lambda函数.我在下文中称之为Lambda函数. 为了明白Lambda函数的用处,请务必先搞明白C++中的自动

C++培训 C++11新特性:杂项

C++培训之前小编给大家总结了一些C++的新特性,这一篇文章是介绍的C++11新特性之杂项,在后面的文章中,小编还会给大家总结一些C++11新特性的知识出来! 类型别名声明 类似typedef,新标准中可以使用using为类型声明一个别名(alias). std::cout<<"test using alias:\n"; using HT = double; using NAME = std::string; HT h = 1.78; NAME name = "R

C++:C++11新特性超详细版(1)

前言: 虽然目前没有编译器能够完全实现C++11,但这并不意味着我们不需要了解,学习它.深入学习C++11,你会发现这根本就是一门新的语言,它解决了c++98中许多遗留下来的问题.早晚会有一天,C++11便会普及大部分编译器.因此,提早做些准备也是应该的. 在此我想做一个关于C++11的专题,将C++11的新特性进行一一讲解,以通俗易懂的语言及例子帮助读者入门C++11.本文便是C++11新特性超详细版系列文章的第一篇, 即C++:[C++11]新特性超详细版(1). 不过我要强调的是,这些文章