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);
}
Aucun commentaire:
Enregistrer un commentaire