This is somewhat related to a previous question I asked regarding using emplace_back
on a vector of pairs. emplace_back() vs push_back when inserting a pair into std::vector
Now my question pertains to using emplace_back
on a vector of vectors.
Here is the code that I am questioning about with comments
std::vector<std::vector<int>> matrix;
matrix.emplace_back({1,2,3}); //doesn't compile
matrix.emplace_back(1,2,3); //doesn't compile
matrix.push_back({1,2,3}); //works and does what is expected (insert a vector made of {1,2,3} into matrix);
matrix.emplace_back(std::vector<int>{1,2,3}); //works but
//defeats the purpose of using emplace_back since this makes a copy
//and is thus equivalent to push_back in this case?
matrix.emplace_back(3,2) //this compiles,
//but it seems to insert a vector of size 3 made of 2s into the matrix.
//not actually sure why it does this
So, from this, matrix.emplace_back(std::vector<int>{1,2,3});
seems to be the only correct way to use std::vector<T>::emplace_back
on a vector of vectors, but this seems to offer no advantages over push_back
. Is my understanding on this matter correct?
Also, could someone explain why matrix.emplace_back(3,2)
is inserting a vector of size 3 made of 2s into the matrix?