Unfortunately, there is currently no way to limit the number of items processed using STL algorithms. My personal opinion is that std::copy()
should take two ranges, both limited to start and end. If none of the ends can be reached, the corresponding range will be unlimited. That is, if anything, I would throw my own copy()
algorithm as follows:
template <typename InIt, typename OutIt> std::pair<InIt, OutIt> copy(InIt init, InIt inend, OutIt outit, OutIt outend) { for (; init != inend && outit != outend; ++init, ++outit) { *outit = *init; } return std::make_pair(init, outit); }
To cope with the current iterator system, comparison between output iterators is actually not possible. Thus, comparison for the output iterator actually requires a bit of programming of the templates to ensure that the actual output iterators are never compared, and true
returned instead. For all other classes of iterators, the above algorithm should work (provided that I did not introduce any typos).
source share