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. )
{ 22, 33 }
is a braced-init-list, how it gets used depends on context.In
output.push_back({ 22, 33 });
,push_back
expectsvector<int>
, which could be constructed (list-initialization since C++11) from braced-init-list{ 22, 33 }
(viavector
's constructor takingstd::initializer_list
), then a temporaryvector<int>
is constructed and passed topush_back
.