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 << '
';

    } while (!std::is_sorted(foo.begin(), foo.end()));

    std::cout << "the range is sorted!
";


    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 << '
';

    } while (!std::is_sorted(foo2.begin(), foo2.end(), compare));

    std::cout << "the range is Descending sorted!
";

    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)
";

  } while (it!=foo.end());

  std::cout << "the range is sorted!
";

  return 0;
}
原文地址:https://www.cnblogs.com/slgkaifa/p/7327457.html