I have a vector of vectors:
std::vector<std::vector<T>> v;
I want to initialize this vector with 5 items(5 vectors of T). Each one of those vectors will contain between 0 and 10 items. Obviously, I need the inner vectors to be reserved with 10 rather than sized with 10. I do not need unnecessary reallocation or copies to happen. In other words, I need emplace-construction.
Since std::vector
does not provide a constructor with the needed number of items to reserve, I came up with this idea:
std::vector<std::vector<T>> v(5,
[](){
std::vector<T> temp;
temp.reserve(10);
return temp;
}());
Questions:
That being said, you're not doing what you set out to do at all. If you check the capacity()
of v
's elements, you'll see that they're not set to 10
. That's because vector
's copy constructor is not defined to copy the container verbatim - it copies its elements.
To do what you wanted you need to call reserve()
after those vectors have been constructed:
std::vector<std::vector<T>> v(5);
for(auto& vec : v) {
vec.reserve(10);
}
It has the added benefit of being less code and more readable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.