I like to reserve memory for the inner vector in a std::vector<std::vector<TYPE>>, in order to avoid a lot of single memory allocations during a subsequent push_back. I don't know the innerSize of the vector exactly, but I can give a good estimate.
std::resize can be used as
vecs.resize(outerSize, std::vector<TYPE>(innerSize));
where outerSize and innerSize are given ints. This does not work for me, because the default constructor is not applicable. However std::reserve does not provide an interface like this.
Is this a good way to reserve memory for all inner vectors?
vecs.resize(outerSize);
for (auto &elem : vecs) {
elem.reserve(innerSize);
}
vecs.resize(outerSize, vector_with_reserved_size);is not guaranty to build inner vectors with correct capacity. - Jarod42vecs.resize(outerSize, std::vector<TYPE>(innerSize));is very different than thereserveloop you have. The former will initialize every inner vector with the default constructor. IfTYPEis large, it will end up taking much longer time than thereserveloop. EDIT: And later on you'd have to replace existingTYPEitems, instead of callingpush_back. - Edy