C + + Initialize_list

Source: Internet
Author: User

Seeing such a thing, you can implement the curly braces ("{" "}") to initialize the container class.

Need to include header files when using

#include <initialize_list>

We've all seen this code.

vector<int1,2,3,4,5  }; or vector<int  1,2,3,4,5 };

The curly brace on the right returns the type of Initialize_list

We can use this in our own class.

class Foo {public:    std::vector<int> data;     // put the initialize_list in the constructor.     foo () {}    foo (std::initializer_list<int> list):d ata (list) {}     void  print () {        for  (auto item:data) {            << Item ;        }         << Endl;    }};

Test it.

int Main () {    1,2,3,4,5  };     1,2,3,4,5  };    Test1.print ();    Test2.print ();     return 0 ;}

Can output normally

The test code for Cppreference is as follows: There's a lot of tricks to see this thing.

1#include <iostream>2#include <vector>3#include <initializer_list>4 5Template <classT>6 structS {7Std::vector<t>v;8S (std::initializer_list<t>l): V (l) {9Std::cout <<"constructed with a"<< l.size () <<"-element list\n";Ten     } One     voidAppend (std::initializer_list<t>l) { A V.insert (V.end (), L.begin (), L.end ()); -     } -std::p air<Constt*, std::size_t> C_arr ()Const { the         return{&v[0], v.size ()};//copy list-initialization in return statement -                                     //This isn't a use of std::initializer_list -     } - }; +  -Template <typename t> + voidTemplated_fn (T) {} A  at intMain () - { -s<int> s = {1,2,3,4,5};//Copy list-initialization -S.append ({6,7,8});//list-initialization in function call -  -Std::cout <<"The vector size is now"<< S.c_arr (). Second <<"ints:\n"; in  -      for(auto n:s.v) toStd::cout << N <<' '; +Std::cout <<'\ n'; -  theStd::cout <<"range-for over brace-init-list: \ n"; *  $      for(intX: {-1, -2, -3})//The rule for auto makes this ranged-for workPanax NotoginsengStd::cout << x <<' '; -Std::cout <<'\ n'; the  +Auto Al = {Ten, One, A};//Special rule for auto A  theStd::cout <<"The list bound to auto have size () ="<< al.size () <<'\ n'; +  -     //Templated_fn ({1, 2, 3});//compiler error! "{1, 2, 3}" is not a expression, $     //It has no type, and so T cannot is deduced $templated_fn<std::initializer_list<int>> ({1,2,3});//OK -templated_fn<std::vector<int>> ({1,2,3});//also OK -}

C + + Initialize_list

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.