I have a vector of vectors:
std::vector<std::vector<T>> v;
I want to initialize this vector with 5 elements (5 T vectors). Each of these vectors will contain from 0 to 10 elements. Obviously, I need internal vectors reserved with 10, and not the size of 10. I do not need excessive redistribution or copies. In other words, I need an emplace construct.
Since it std::vectordoes not provide a constructor with the necessary number of elements for redundancy, I came up with this idea:
std::vector<std::vector<T>> v(5,
[](){
std::vector<T> temp;
temp.reserve(10);
return temp;
}());
Questions:
- It's really? Does this behavior support undefined?
- Did I really minimize the resources needed to complete the steps above? I feel like there are unnecessary copies in my approach.