I'm trying to mimic std::copy() and I came up with the following
1 2 3 4 5 6 7 8 9 10 11
|
template<typename iter1, typename iter2>
iter2 mycopy(iter1 f1, iter1 e1, iter2 f2)
{
while(f1 != e1) {
*f2 = *f1;
f1++;
f2++;
}
f2--; //hack-ish
return f2;
}
|
That f2-- is quite hack-ish and ugly. Does anyone know a better implementation of this that only uses primitive facilities?
Last edited on
does the built - in one not move blocks of memory or have performance enhancements that you won't get in a brute force loop?
I suspect that the built-in one relies on the compiler's optimization to handle performance enhancements.
From cppreference:
http://en.cppreference.com/w/cpp/algorithm/copy
Possible implementation, First version:
1 2 3 4 5 6 7 8 9
|
template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last,
OutputIt d_first)
{
while (first != last) {
*d_first++ = *first++;
}
return d_first;
}
|
To me that looks like the classic K&R copy idiom from way back then :+)
As
jonnin says, the notes mention std::memmove, and the requirements about the types.
Last edited on