C++11の新しい特性アプリケーション--いくつかの新しい便利なアルゴリズムを紹介します.

9013 ワード

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」区間は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_usortedを使用したversionの2つを示しています.
もう一つのポイントは、範囲内の要素の個数が二つより少ない場合、常にtrueに戻ります.
ISオルテドの原型:
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; }