std::unique_copy
| Defined in header <algorithm>
|
||
template< class InputIt, class OutputIt >
OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first );
|
(1) | (constexpr since C++20) |
template< class InputIt, class OutputIt, class BinaryPred >
OutputIt unique_copy( InputIt first, InputIt last,
OutputIt d_first, BinaryPred p );
|
(2) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 unique_copy( ExecutionPolicy&& policy, ForwardIt1 first,
ForwardIt1 last, ForwardIt2 d_first );
|
(3) | (since C++17) |
template< class ExecutionPolicy, class ForwardIt1,
class ForwardIt2, class BinaryPred >
ForwardIt2 unique_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, BinaryPred p );
|
(4) | (since C++17) |
Copies elements from the source range [first, last) to the destination range beginning at d_first. For each group of consecutive equivalent elements, only the first element is copied.
operator==.operator== does not establish an equivalence relation, the behavior is undefined.p.p does not establish an equivalence relation, the behavior is undefined.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
If *d_first = *first is invalid(until C++20)*first is not writable to d_first(since C++20), the program is ill-formed.
If any of the following conditions is satisfied, the behavior is undefined:
- The source and destination ranges overlap.
- For the non-parallel overloads ((1,2)), given
Tas the value type ofInputIt, all following conditions are satisfied:
|
(until C++20) |
|
(since C++20) |
Tis not CopyAssignable, or all following conditions are satisfied:
Tis not CopyConstructible.OutputItdoes not the requirements of LegacyForwardIterator, or the value type ofOutputItis notT.
Parameters
| first, last | - | the pair of iterators defining the source range |
| d_first | - | the beginning of the destination range |
| p | - | binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following:
While the signature does not need to have |
| policy | - | the execution policy to use |
| Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator.
| ||
-OutputIt must meet the requirements of LegacyOutputIterator.
| ||
-ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.
| ||
Return value
The past-the-end iterator of the destination range.
Complexity
Given \(\scriptsize N\)N as std::distance(first, last):
operator==.p.operator==.p.Exceptions
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Notes
If InputIt satisfies LegacyForwardIterator, this function rereads the input in order to detect duplicates.
Otherwise, if OutputIt satisfies LegacyForwardIterator, and the value type of InputIt is the same as that of OutputIt, this function compare *d_first to *first.
Otherwise, this function compares *first to a local element copy.
Possible implementation
See also the implementations in libstdc++ and libc++.
Example
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
std::string s1{"A string with mmmany letters!"};
std::cout << "Before: " << s1 << '\n';
std::string s2;
std::unique_copy(s1.begin(), s1.end(), std::back_inserter(s2),
[](char c1, char c2) { return c1 == 'm' && 'm' == c2; });
std::cout << "After: " << s2 << '\n';
}
Output:
Before: A string with mmmany letters!
After: A string with many letters!
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 239 | C++98 | the predicate was applied std::distance(first, last) times
|
applied one time fewer (for non-empty ranges) |
| LWG 241 | C++98 | the value type of InputIt was not required to be CopyConstructible
|
conditionally required |
| LWG 538 | C++98 | the value type of InputIt was not required to be CopyAssignable
|
conditionally required |
| LWG 2439 | C++98 | the value type of InputIt was not required to beCopyConstructible if OutputIt is a LegacyForwardIterator
|
conditionally required |
See also
(C++20) |
creates a copy of some range of elements that contains no consecutive duplicates (algorithm function object) |
| finds the first two adjacent items that are equal (or satisfy a given predicate) (function template & algorithm function object) | |
(C++20) |
|
| removes consecutive duplicate elements in a range (function template & algorithm function object) | |
(C++20) |
|
(C++11) |
copies a range of elements to a new location (function template & algorithm function object) |
(C++20)(C++20) |