Xi. C + + features begin () and end ()

Source: Internet
Author: User

Non-member Begin () and end ()

Perhaps you have noticed that I have used the non-member begin () and end () functions in the previous example. They are new to the standard library, and in addition to improved code consistency, it also helps to use generic programming more. They are compatible with all STL containers. More importantly, they can be overloaded. So they can be extended to support any type. Overloads of the C-type array are already included in the standard library.

We also use the code in the previous example to illustrate that in this example I printed an array and then looked for its first even element. If Std::vector is replaced with an array of type C. The code might look like this:

1 intArr[] = {1,2,3}; 2Std::for_each (&arr[0], &arr[0]+sizeof(arr)/sizeof(arr[0]), [](intN) {std::cout << n <<Std::endl;});3 4Auto is_odd = [] (intN) {returnn%2==1;}; 5Auto begin = &arr[0]; 6Auto End = &arr[0]+sizeof(arr)/sizeof(arr[0]); 7Auto pos =std::find_if (begin, end, is_odd);8 if(POS! =end)9Std::cout << *pos << Std::endl;

If you do this using the Begin () and end () of non-members, it will be the following:

1 intArr[] = {1,2,3}; 2Std::for_each (Std::begin (arr), Std::end (arr), [] (intN) {std::cout << n <<Std::endl;}); 3 4Auto is_odd = [] (intN) {returnn%2==1;}; 5Auto pos =std::find_if (Std::begin (arr), Std::end (arr), is_odd);6 if(POS! =std::end (arr))7Std::cout << *pos << Std::endl;

This is basically the same as the code that uses Std::vecto. This means that we can write a generic function that handles all types that support begin () and end ().

1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5 6Template <typename iterator>7 voidBar (Iterator begin, Iterator end)8 { 9Std::for_each (Begin, End, [] (intN) {std::cout << n <<Std::endl;}); Ten  OneAuto is_odd = [] (intN) {returnn%2==1;};//returns the n that satisfies the condition AAuto pos = std::find_if (begin, end, is_odd);//  -     if(POS! =end) -Std::cout <<"The first odd number that satisfies the condition is:"<< *pos <<Std::endl; the }  -  -Template <typename c> - voidfoo (c c) + {  - Bar (Std::begin (c), Std::end (c)); + }  A  atTemplate <typename T, size_t n> - voidFoo (T (&arr) [N]) - {  - Bar (Std::begin (arr), Std::end (arr)); - }  -  in  - intMain () to { +     intArr[] = {Ten, A, -};  - foo (arr); the  *std::vector<int>v; $V.push_back (4); Panax NotoginsengV.push_back (5);  -V.push_back (6);  the foo (v); +     return 0; A}

Output:

Xi. C + + features begin () and end ()

Related Article

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.