data that push back to 2D vector

63 Views Asked by At

vector<vector<int>> output; // 2D int vector


//normally, we can do
vector<int> v = { 44, 55 };
output.push_back(v);

//I found some example that we can do
output.push_back({ 22, 33 });

I know {} can be used to init an array or vector. How the compiler know { 22, 33 } is an vector<int> rather than int array if i want to skip the line like vector<int> v = { 44, 55 }; ?

and {22,33} is it a temporary object? (I know the return-by-value functions always give rise to temporary objects. )

1

There are 1 best solutions below

0
On

{ 22, 33 } is a braced-init-list, how it gets used depends on context.

In output.push_back({ 22, 33 });, push_back expects vector<int>, which could be constructed (list-initialization since C++11) from braced-init-list { 22, 33 } (via vector's constructor taking std::initializer_list), then a temporary vector<int> is constructed and passed to push_back.