Skip to content
Search

C++:UniformInitialization

lastmod 2015-03-19created 2015-03-192 edits

About

In C++, to build existing STL containers or classes, for a Vector we added elements with push_back, and for a class we built it with a constructor. Now, however, there is no need for that: initialization is possible with {}.

This is called Uniform Initialization.

An operation that used push_back one element at a time in STL:

std::vector<CMan> table1;
table1.push_back(CMan(10, "1"));
table1.push_back(CMan(20, "2"));
table1.push_back(CMan(30, "3"));

can be changed in C++11 as follows.

std::vector<CMan> table2 = { {10, "1"}, {20, "2"}, {30, "3"} };