How to properly resize a vector of unique_ptr vectors in one line without gcc giving a compilation error regarding a deleted function?
vector<vector<unique_ptr<A> >> a;
a.resize(..... )
UPDATE: This is the code that I used that works OK.
int width, height;
vector<vector<unique_ptr<A> >> a;
a.resize(width);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
a.at(i).emplace_back(new A());
If possible, I would like to resize the sizes in one go just like resizing of vector of vectors;
vector<vector<int>> intObj;
intObj.resize(width, vector<int>(height, int()));
but I got this error whenever I tried to resize the above vectors using the following method;
a.resize(x, vector<unique_ptr<A>>(y, unique_ptr<A>()));
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete<A>]’
Thanks.
vector<vector<...>>
is a code smell to me in this scenario. You should use a single vector that iswidth*height
in size, not a vector of vectors. NOTE: If you make this change, a call toa.resize(width*height)
will work. – Mauretaniavector<vector<>>
is more scalable as the memory used to held data doesn't need to be continuous which might be necessary for a large amount of data. Also I have seen way to many wrong calculated offsets... – Inexplicitemplace_back(new A())
= leak. Don't do it; usepush_back(make_unique<A>())
– Fit