C++11 new Feature application--introduction of several new convenience algorithms (algorithms that do not change the order of elements in the container)

Source: Internet
Author: User
Tags ranges

As is known to all, C + + STL has a header file named algorithm, which is the meaning of the algorithm.
The header <algorithm> defines a collection of functions especially designed to being used on ranges of elements.

Therefore, to 818 this header file c++11 new algorithms, the main description of the several algorithms today does not change the order of elements in the container.

In addition, if you use the STL algorithm in combination with a lambda expression, the code will be more concise.

Find_if_not
This algorithm has been introduced before, please refer to the blog "real combat C + + vector series –vector application of STL's" Find, Find_if, Find_end, find_first_of, Find_if_not (c++11).

all_of
Prototype:

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

Role:
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 other Wise.
Detection interval [First, last] whether all the elements satisfy the unary-judge expression pred. All elements satisfy the condition to return true, otherwise false is returned.

Application:

#include <iostream> //std::cout #include <algorithm> //std::all_of #include <array> //Std::array intMain () {STD:: Array<int,8>Foo = {3,5,7, One, -, -, +, at};if(STD:: All_of (Foo.begin (), Foo.end (), [] (inti) {returni%2;}) )STD::cout<<"All the elements is odd numbers.\n";return 0;}//output:All the elements is odd numbers.

any_of
Prototype:

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

Role:
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.

Application:

#include <iostream> //std::cout #include <algorithm> //std::any_of #include <array> //Std::array intMain () {STD:: Array<int,7>Foo = {0,1,-1,3,-3,5,-5};if(STD:: Any_of (Foo.begin (), Foo.end (), [] (inti) {returni<0;}) )STD::cout<<"There is negative elements in the range.\n";return 0;}//output:There is negative elements in the range.

none_of
Prototype:

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

Role:
Returns true if pred Returns false for all the elements in the range [First,last] or if the range is empty, and false othe Rwise.

Application:

#include <iostream> //std::cout #include <algorithm> //std::none_of #include <array> //Std::array intMain () {STD:: Array<int,8>Foo = {1,2,4,8, -, +, -, -};if(STD:: None_of (Foo.begin (), Foo.end (), [] (inti) {returni<0;}) )STD::cout<<"There is no negative elements in the range.\n";return 0;}//output:There is no negative elements in the range.

is_permutation
Permutation noun arrangement, exchange and other meanings.
This function is used to determine whether two sequences are different permutations of the same element set!
The function uses operator== or pred to determine whether two elements are equal.

Prototype:

template  < class  ForwardIterator1, class  forwarditerator2> bool    is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); template  <class  ForwardIterator1, class  ForwardIterator2, class  binarypredicate>                        Span class= "hljs-built_in" >bool  is_permutation (ForwardIterator1 first1, ForwardIterator1 Last1, ForwardIterator2 First2, Binarypredicate pred);  

Role:
Test whether range is permutation of another
Compares the elements in the range [First1,last1] with those in the range beginning on First2, and returns True if all of The elements in both ranges match, even in a different order.

Application:

#include <iostream> //std::cout #include <algorithm> //std::is_permutation #include <array> //Std::array intMain () {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;}//output:Foo and bar contain the same elements.

It is important to note that you cannot have a whim, such as comparing a greater than or less than, you can only do this:
The elements is compared using operator== (or pred)

C++11 new Feature application--introduction of several new convenience algorithms (algorithms that do not change the order of elements in the container)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.