C++11 Some of the new handy algorithms

Source: Internet
Author: User

C+11 added a number of convenient algorithms, these new algorithms make our code more concise and convenient to write, here just to enumerate some of the commonly used new algorithms, as a summary, more new algorithms readers can refer to http://en.cppreference.com/w/cpp/algorithm.

The algorithm library has three new algorithms for judging all_of, any_of, and none_of:

template< class InputIt, class Unarypredicate >bool all_of (InputIt First, InputIt last, unarypredicate p); template < class InputIt, class Unarypredicate >bool any_of (InputIt First, InputIt last, Unarypredicate p);template< CLA SS InputIt, Class Unarypredicate >bool none_of (InputIt First, InputIt last, Unarypredicate p);
    • All_of: Checks if all elements in the interval [first, last] satisfy the unary-judged p, all elements satisfy the condition to return true, otherwise false is returned.
    • Any_of: Checks if at least one element in the interval [first, last] satisfies the unary-judged p, and returns True if one element satisfies the condition.
    • None_of: Checks if all elements in the interval [first, last] do not satisfy the unary-judged p, all elements do not satisfy the condition to return true, otherwise false is returned.

Here are some examples of these algorithms:

#include <iostream> #include <algorithm> #include <vector>using namespace Std;int main () {       vector  <int> v = {1, 3, 5, 7, 9};  Auto IsEven = [] (int i) {return I% 2! = 0;       BOOL isallodd = std::all_of (V.begin (), V.end (), isEven);       if (isallodd)              cout << "All are odd" << Endl;       BOOL Isnoneeven = std::none_of (V.begin (), V.end (), isEven);       if (isnoneeven)              cout << "None is even" << Endl;       Vector<int> V1 = {1, 3, 5, 7, 8, 9};       BOOL anyof = std::any_of (V1.begin (), V1.end (), isEven);       if (anyof)              cout << "at least one is even" << Endl;}

Output:

All are oddnone are oddat least one is even

Algorithm Library search algorithm added a find_if_not, its meaning and find_if is the opposite, that is, to find the element that does not meet a certain condition, find_if can also realize the function of find_if_not, only need to change the judgment to negative judgment, and now new find After _if_not, there is no need to write negative judgments, and readability becomes better. Here is the basic use of it:

#include <iostream> #include <algorithm> #include <vector>using namespace Std;int main () {    vector <int> v = {1, 3, 5, 7, 9,4};    Auto IsEven = [] (int i) {return I% 2 = = 0;};    Auto Firsteven = std::find_if (V.begin (), V.end (), isEven);    if (Firsteven!=v.end ())              cout << "The first even is" <<* firsteven << Endl;       Using find_if to find odd numbers you need to re-write a negative meaning of the judgment auto Isnoteven = [] (int i) {return I% 2! = 0;}; Auto firstodd = std::find_if (V.begin (), V.end (), isnoteven);       if (Firstodd!=v.end ())              cout << "The first odd is" <<* firstodd << Endl;       Use Find_if_not to find an odd number without a new definition of the       auto odd = Std::find_if_not (V.begin (), V.end (), isEven);       if (Odd!=v.end ())              cout << "The first odd is" <<* odd << Endl;}

Will output:

The first even is 4the first odd are 1the first odd is 1

You can see that using find_if_not does not need to define the new negative meaning of the judgement, more convenient.

The algorithm library also added a copy_if algorithm, which compared to the original copy algorithm more than a judgement, used more convenient, the following is its basic usage:

#include <iostream> #include <algorithm> #include <vector>using namespace Std;int main () {       vector <int> v = {1, 3, 5, 7, 9, 4};       Std::vector<int> v1 (v.size ());    Copy Auto it by condition = std::copy_if (V.begin (), V.end (), V1.begin (), [] (int i) {return i%2!=0;}); Reduce vectors to the appropriate size    v1.resize (std::d istance (V1.begin (), it));       for (int i:v1)       {              cout<<i<< "";       }       Cout<<endl;}    

The algorithm library has added iota to facilitate the generation of ordered sequences, for example, we need a fixed-length array, the elements of this array are on the basis of a certain number of increments, then the iota can be very convenient to generate this array. Here is the basic use of it:

#include <numeric> #include <array> #include <vector> #include <iostream>using namespace std; int main () {vector<int> V (4);//Loop through assignment to initialize array//for (int i=1; i<=4; i++)//{//    v.push_back (i);//}// Initialize arrays directly via iota, more concise    Std::iota (V.begin (), V.end (), 1);    for (auto n:v) {        cout << n << ';    }    cout << Endl;        Std::array<int, 4> array;    Std::iota (Array.begin (), Array.end (), 1);    for (auto N:array) {        cout << n << ';    }    Std::cout << Endl;}

Will output:

1 2 3 41 2 3 4

You can see that using iota to initialize an array is more concise than traversing an assignment, and it is important to note that the sequence of iota initialization needs to specify the size, if:vector<int> V (4) in the code above, or if the initialization size is 4, then the output is empty.

The algorithm library has also added a new algorithm to get the maximum and minimum value minmax_element, so that we do not need to call the Max_element and max_element algorithm when we want to get the maximum value and the minimum value, it will be more convenient to use, Minmax_ Element returns an iterator of the minimum and maximum values to a pair, following its basic usage:

#include <iostream> #include <algorithm> #include <vector>using namespace Std;int main () {    //your Code goes here    vector<int> v = {1, 2, 5, 7, 9, 4};    Auto result = Minmax_element (V.begin (), V.end ());       cout<<*result.first<< "" <<*result.second<<endl;    return 0;}

Will output:

1 9

The algorithm library adds the is_ sorted and Is_ sorted_until algorithms, Is_sort is used to determine if a sequence is ordered, and Is_sort_until is used to return a sequence of parts that have been ordered earlier in the series. The following are their basic uses:

#include <iostream> #include <algorithm> #include <vector>using namespace Std;int main () {    vector <int> v = {1, 2, 5, 7, 9, 4};    Auto pos = Is_sorted_until (V.begin (), V.end ());       for (auto It=v.begin (); it!=pos; ++it)    {        cout<<*it<< "";    }    cout<<endl;        BOOL Is_sort = is_sorted (V.begin (), V.end ());    cout<< is_sort<<endl;    return 0;}

Will output:

1 2 5) 7 90

Summary: These new algorithms make it easier to use and enhance the readability of your code.

C++11 Some of the new handy Algorithms (RPM)

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.