New features in c++11: Initializer_list detailed

Source: Internet
Author: User

The new type provided by C++11 is defined in the <initializer_list> header file.

class T >class initializer_list;

Let's talk about its usefulness first, and then introduce it in detail.

First with Initializer_list, the initialization of STL container is much more convenient, such as initializing a vector before:

int a[] = {0123};std::vector<int> Vec (A, A + sizeof (a));

Or

std::vector<int> vec;vec.push_back (1); Vec.push_back (3); Vec.push_back (3); Vec.push_back (2);

With Initializer_list, you can just like initialize an array:

classTest {Private:    Staticstd::map<string,string>ConstNametobirthday = {        {"Lisi","18841011"},        {"Zhangsan","18850123"},        {"Wangwu","18870908"},        {"Zhaoliu","18810316"},    };}

Of course, the std::map inside must provide a constructor with parameters of initializer_list such as:

Map (std::initializer_list<value_type> init,     const compare& comp = Compare (),      Const allocator& alloc = Allocator ());

In fact for (initializer:list) If the list is a shape such as: {A, B, c ...}, then the list is automatically constructed as a Initializer_list object.

Here's a little bit about initializer_list

A initializer_list is automatically constructed when it appears in the following two cases:

    1. When initializing, the curly brace is initialized and is automatically constructed. Include function call and assignment
    2. When it comes to for (initializer:list), the list is automatically constructed as a Initializer_list object

This means that the Initializer_list object can only be initialized with curly braces {}.

Copying a Initializer_list object does not copy the elements inside. Actually, it's just a quote. And the elements inside are all const.

The following example can help us better understand how to use initializer_list:

#include <iostream>#include<vector>#include<initializer_list>using namespacestd;template<classT>structS {vector<T>v; S (Initializer_list<T>l): V (l) {cout<<"constructed with a"<< l.size () <<"-elements lists"<<Endl; }    voidAppend (std::initializer_list<t>l) {V.insert (V.end (), L.begin (), L.end ()); } pair<Constt*, size_t> C_arr ()Const{        return{&v[0], v.size ()}; }};template<typename t>voidtemplated_fn (T Arg) { for(auto a:arg) cout<< a <<" "; cout<<Endl;}intMain () {S<int> s = {1,2,3,4,5};//automatically construct a initializer_list//object and copy itS.append ({6,7,8});//list-initialization in function callcout<<"The vector size is now"<< S.c_arr (). Second <<"ints:"<<Endl;  for(auto n:s.v) cout<<' '<<N; cout<<Endl; cout<<"range-for over brace-init-list:"<<Endl;  for(Auto x: {-1, -2,Geneva})////The rule for auto makes this rangedcout << x <<" "; cout<<Endl; Auto Al= {Ten, One, A};//Special rule for autocout<<"The list bound to auto have size () ="<< al.size () <<Endl; //Templated_fn ({1, 2, 3}); //compiler error! "{1, 2, 3}" is a expressionit have no type, and so T cannot be duduced.TEMPLATED_FN<initializer_list<int> > ({7,8,9});//OKtemplated_fn<vector<int> > ({3,5,7});//also OK    return 0;}

Reference:

Http://en.cppreference.com/w/cpp/utility/initializer_list

Http://www.cppblog.com/liyiwen/archive/2009/07/26/91248.html

New features in c++11: Initializer_list detailed

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.