The initializer_list Template (C++11)

The initializer_list template is another C++11 addition to the C++ library. You can use the initializer-list syntax to initialize an STL container to a list of values:

std::vector<double> payments {45.99, 39.23, 19.95, 89.01};

This would create a container for four elements and initialize the elements to the four values in the list. What makes this possible is that the container classes now have constructors taking an initializer_list<T> argument. A vector<double> object, for example, has a constructor that accepts an initializer_list<double> argument, and the previous declaration is the same as this:

std::vector<double> payments({45.99, 39.23, 19.95, 89.01});

Here, the list is written explicitly as a constructor ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.