Constructor That Uses an Initialization List (C++11)

This constructor takes an initializer_list<charT> parameter:

basic_string(initializer_list<charT> il, const Allocator& a = Allocator());

You can use it with a braced list of characters:

string slow({'s', 'n', 'a', 'i', 'l'});

This isn’t the most convenient way to initialize a string, but it keeps the string interface similar to that of the STL container classes.

The initializer_list class has begin() and end() members, and the effect of using this constructor is the same using the range constructor:

basic_string(il.begin(), il.end(), a);

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.