Reputation: 974
I'm creating a vector that itself will contain a vector, using this code:
vector<vector<short>> pixelData;
However, I start with an empty vector, and I would like to append a new vector as the first element, but I don't know how to construct a vector without assigning it to a variable. Right now, I'm doing:
pixelData.push_back(vector<short> (x, y));
Where pixelData is my containing vector, and I'm constructing a new vector inside with two short values.
Upvotes: 1
Views: 98
Reputation: 172884
If you want to append a new vector
, which contains two elements with value x
and y
, you should use initialization list (since C++11),
pixelData.push_back(vector<short> {x, y});
or just
pixelData.push_back({x, y});
Check the constructors of std::vector
here.
Upvotes: 3
Reputation: 133567
Actually you are not constructing a vector with 2 short values, you are invoking this constructor:
explicit vector::vector(size_type count, const T& value = T())
which initializes the vector with a capacity of count
by coping value
to each cell.
To create an empty vector just call the empty constructor, eg vector<short>()
, but if you know the capacity just use it, eg vector<short>(size)
to avoid internal reallocation.
Upvotes: 1