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:
- When initializing, the curly brace is initialized and is automatically constructed. Include function call and assignment
- 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