Is there a std::copy-like algorithm that accepts four iterators, denoting two ranges?
Basically, it should stop copying as soon as either range is exhausted:
template<typename Iter>
void copy_range(Iter begin1, Iter end1, Iter begin2, Iter end2)
{
for (; (begin1 != end1) && (begin2 != end2); ++begin1, ++begin2)
{
*begin2 = *begin1;
}
}
No there is no such thing sadly. The closest thing is
std::copy_n.And of course the algorithm you just wrote.
Depending on the type of iterator used (random or not), using this is more efficient (because only one check needs to be made for each iteration) than your algorithm:
Another alternative is a checked output iterator, something along the lines of this (rough sketch, not checked code):
Usage:
This has roughly the same performance as your solution, but it is more widely usable.