C++11 New Convenience Algorithm example analysis _c language

Source: Internet
Author: User

C + + is a very broad application of the programming language, and C++11 has added a number of convenient algorithms, these new algorithms to make our code more concise and convenient, this article enumerates some commonly used new algorithms, is to do a summary analysis, more new algorithm 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, defined as follows:

template< class Inputit, class unarypredicate >
bool all_of (Inputit-I, Inputit last, Unarypredicate p); 
   template< class Inputit, class unarypredicate >
bool any_of (Inputit-I, Inputit, unarypredicate p); c4/>template< class Inputit, class unarypredicate >
bool none_of (Inputit-I, Inputit last, Unarypredicate p );

①all_of: Check that all elements in the interval [first and last] satisfy a unary-judgment p, all elements satisfy the condition return true, or false returns.
②any_of: Check whether at least one element in the interval [first, last] satisfies the unary-judgment p, returns true if one element satisfies the condition, or returns TRUE.
③none_of: Check that all elements in the interval [first, last] do not satisfy the unary judgment p, all elements do not satisfy the condition return true, otherwise return false.

The following are 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 is 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 odd
None is odd
At 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 elements that do not meet a certain condition, find_if can also realize the function of Find_if_not, only to be judged to the negative judgment, now add find After _if_not, there is no need to write negative judgments, and readability has become better. The following are its basic uses:

#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" the "the" is "<<* firsteven << Endl;

    Using find_if to find odd numbers, you need to write a negative meaning of the judgment type
auto Isnoteven = [] (int i) {return I% 2!= 0;};
Auto firstodd = std::find_if (V.begin (), V.end (), isnoteven);

    if (Firstodd!=v.end ())
       cout << "The" the "the" is "<<* firstodd << Endl;

    Using Find_if_not to find odd numbers requires no new definition of

    auto odd = Std::find_if_not (V.begin (), V.end (), isEven);
    if (Odd!=v.end ())
       cout << "The" the "the" "<<* odd << Endl;


Will output:

The even is 4
The odd is 1
The odd is 1

It can be seen that using find_if_not does not need to define a new negative meaning of the judgment, more convenient.

Algorithm library also added a copy_if algorithm, which compared to the original copy algorithm more than a judgment, more convenient to use, 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 = std::copy_if (V.begin (), V.end (), V1.begin (), [] (int i) {return i%2!=0;})
according to the condition Shrink vector to 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, such as we need a fixed-length array, the elements of this array are based on a number of values on the basis of the increment, then the iota can be very convenient to generate this array. The following are its basic uses:

#include <numeric>
#include <array>
#include <vector>
#include <iostream>
using namespace std;
 
int main ()
{
vector<int> V (4);
Iterate through the assignment to initialize the array
//for (int i=1; i<=4; i++)
//{
//  v.push_back (i);

///Direct initialization of arrays via iota, simpler
  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 4
1 2 3 4

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

The algorithm library also added an algorithm to obtain both the maximum and the minimum value, so that if we want to get the maximum and the minimum value, we do not have to call the Max_element and max_element algorithm respectively, it will be more convenient to use, Minmax_ Element puts the iterator of the minimum and maximum values back in a pair, and the following is 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 algorithm, which is used to determine whether a sequence is sorted or not, and the is_sort is used to return a sequence of previously ordered parts of the sequence. 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 9
0

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

It is hoped that the algorithm described in this paper can help us to better master c++11.

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.