I have a container std::vector and I would like to efficiently split it into sub-ranges with x items in each. The original container is not needed so the items should be moved and not copied into the sub-ranges.
I've managed to do the splitting using copying, however I'm unsure how to do it with move assignments?
range.insert(range.end(), new_items.begin(), new_items.end());
while(range.size() >= x)
{
sub_ranges.push_back(std::vector<int>(range.begin(), range.begin() + x));
range = std::vector<int>(range.begin() + x, range.end());
}
EDIT:
Some progress... still not quite there, and a bit ugly
while(range.size() >= x)
{
std::vector<short> sub_range(x); // Unnecessary allocation?
std::move(range.begin(), range.begin() + x, sub_range.begin());
sub_ranges_.push_back(std::move(sub_range));
std::move(range.begin() + x, range.end(), range.begin());
range.resize(range.size() - x);
}