天天看點

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_if_not

該算法在之前介紹過,請參閱部落格《實戰c++中的vector系列–vector應用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)》。

all_of

原型:

template <class InputIterator, class UnaryPredicate>
  bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);           

作用:

Test condition on all elements in range

Returns true if pred returns true for all the elements in the range [first,last) or if the range is empty, and false otherwise.

檢測區間[first, last)中是否全部的元素都滿足一進制推斷表達式pred。全部的元素都滿足條件傳回true,否則傳回false.

應用:

#include <iostream>     // std::cout
#include <algorithm>    // std::all_of
#include <array>        // std::array

int main () {
  std::array<int,8> foo = {3,5,7,11,13,17,19,23};

  if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
    std::cout << "All the elements are odd numbers.\n";

  return 0;
}
//輸出:
All the elements are odd numbers.           

any_of

template <class InputIterator, class UnaryPredicate>
  bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);           

Test if any element in range fulfills condition

Returns true if pred returns true for any of the elements in the range [first,last), and false otherwise.

#include <iostream>     // std::cout
#include <algorithm>    // std::any_of
#include <array>        // std::array

int main () {
  std::array<int,7> foo = {0,1,-1,3,-3,5,-5};

  if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
    std::cout << "There are negative elements in the range.\n";

  return 0;
}
//輸出:
There are negative elements in the range.           

none_of

template <class InputIterator, class UnaryPredicate>
  bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);           

Returns true if pred returns false for all the elements in the range [first,last) or if the range is empty, and false otherwise.

#include <iostream>     // std::cout
#include <algorithm>    // std::none_of
#include <array>        // std::array

int main () {
  std::array<int,8> foo = {1,2,4,8,16,32,64,128};

  if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
    std::cout << "There are no negative elements in the range.\n";

  return 0;
}
//輸出:
There are no negative elements in the range.           

is_permutation

permutation 名詞 排列 、交換等意思。

該函數是用來推斷兩個序列是否為同一進制素集的不同排列。

該函數使用operator==或者是pred來推斷兩個元素是否是相等的。

template <class ForwardIterator1, class ForwardIterator2>
   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
                        ForwardIterator2 first2);   
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
                        ForwardIterator2 first2, BinaryPredicate pred);           
#include <iostream>     // std::cout
#include <algorithm>    // std::is_permutation
#include <array>        // std::array

int main () {
  std::array<int,5> foo = {1,2,3,4,5};
  std::array<int,5> bar = {3,1,4,5,2};

  if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) )
    std::cout << "foo and bar contain the same elements.\n";

  return 0;
}
//輸出:
foo and bar contain the same elements.