Here is a demonstrative example of the approach
#include <iostream>
#include <iterator>
#include <algorithm>
#include <list>
template <class InputIterator, class OutputIterator>
OutputIterator copy( InputIterator first,
InputIterator last,
OutputIterator result )
{
for ( ; first != last; ++first, ++result ) *result = *first;
return result;
}
template<class InputIterator, class Size, class OutputIterator>
OutputIterator copy_n( InputIterator first,
Size n,
OutputIterator result )
{
for ( Size i = Size(); i != n; ++i, ++first, ++result ) *result = *first;
return result;
}
int main()
{
{
std::list<int> l = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto it = l.begin();
::copy_n<decltype( it ) &>( it, l.size() / 2,
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
l.insert( it, 0 );
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
}
{
const size_t N = 3;
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int b[2 * N + 1];
int *last = b;
size_t i = 0;
while ( i < N &&
::copy<int *, int * &>( std::begin( a[i] ),
std::end( a[i] ),
last ) <= std::prev( std::end( b ), N ) ) ++i;
for ( auto it = b; it != last; ++it ) std::cout << *it << ' ';
std::cout << std::endl;
}
}
The program output is
0 1 2 3 4
0 1 2 3 4 0 5 6 7 8 9
1 2 3 4 5 6
Thus simply for some algorithms there should be written moreclearly in the C++ Standard what is the value of the iterator passed by reference after exiting the algorithm