I'm trying to find a way to improve my answer here. Let's simplify the question to say: I want to partition the input container, lets call it foo
, into a vector
of vector
s of size STEP
, the last of these vector
s shall have a smaller size if there were less than STEP
elements remaining in the input container, let's call this partitioned container bar
.
I don't want to iterate over my input or output range multiple times. The element of the problem I'm trying to solve are simply:
- Append
bar
with a constructedvector
of sizemin(STEP, distance(it, foo.end())
- Have the
it
point toadvance(it, size(bar.back()))
after constructing the container - Do this
vector
construction in-place
The translation of my answer to the confines of this problem is:
auto it = cbegin(foo);
for (auto i = size(foo); i > STEP; i -= STEP) {
bar.push_back(decltype(bar)::value_type(STEP));
for (auto internalIt = bar.back().begin(); internalIt != bar.back().end(); ++internalIt, ++it) {
*internalIt = *it;
}
}
bar.push_back(decltype(bar)::value_type(it, cend(foo)));
The problem is this line: bar.push_back(decltype(bar)::value_type(STEP))
I'm allocating the vector
and 0-initializing it's components. Is there a better way to do this, under which I still would only iterate over the input and output ranges once?