template< class InputIt, class OutputIt >
OutputIt unique_copy( InputIt first, InputIt last,
OutputIt d_first );
Is it valid to use std::unique_copy
if input range and output range overlap?
Consider the following two example cases
auto d_last = std::unique_copy(first, last, d_first);
d_first <= first <= d_last <= last
first <= d_first <= last <= d_last
CodePudding user response:
The preconditions for std::unique_copy
are described in
[algorithms#alg.unique-8]:
template<class InputIterator, class OutputIterator> constexpr OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result);
Preconditions:
- The ranges
[first, last)
and[result, result (last-first))
do not overlap.
So this is undefined behavior.