Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 ExecutionPolicy,class ForwardIt1,class ForwardIt2> ForwardIt2 unique_copy( ExecutionPolicy&& policy, ForwardIt1 first, | (2) | (since C++17) |
template<class InputIt,class OutputIt,class BinaryPred> OutputIt unique_copy( InputIt first, InputIt last, | (3) | (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt1, class ForwardIt2,class BinaryPred> | (4) | (since C++17) |
Copies the elements from the range[
first,
last)
, to another range beginning atd_first in such a way that there are no consecutive equal elements. Only the first element of each group of equal elements is copied.
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
If*d_first=*first is invalid(until C++20)*first is notwritable tod_first(since C++20), the program is ill-formed.
If source and destination ranges overlap, the behavior is undefined.
GivenT
as the value type ofInputIt
, if overload(1) or(3) doesnot satisfy all of the following conditions, the behavior is undefined:
| (until C++20) |
| (since C++20) |
T
is bothCopyConstructible andCopyAssignable.OutputIt
meets the requirements ofLegacyForwardIterator.OutputIt
is alsoT
.T
isCopyAssignable.Contents |
first, last | - | the pair of iterators defining the sourcerange of elements to process |
d_first | - | the beginning of the destination range |
policy | - | theexecution policy to use |
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: bool pred(const Type1&a,const Type2&b); While the signature does not need to haveconst&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) |
Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-OutputIt must meet the requirements ofLegacyOutputIterator. | ||
-ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator. |
Output iterator to the element past the last written element.
Given\(\scriptsize N\)N asstd::distance(first, last):
For overloads(2,4), there may be a performance cost if the value type ofForwardIt1
is not bothCopyConstructible andCopyAssignable.
The overloads with a template parameter namedExecutionPolicy
report errors as follows:
ExecutionPolicy
is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.See also the implementations inlibstdc++ andlibc++.
IfInputIt
satisfiesLegacyForwardIterator, this function rereads the input in order to detect duplicates.
Otherwise, ifOutputIt
satisfiesLegacyForwardIterator, and the value type ofInputIt
is the same as that ofOutputIt
, this function compare*d_first to*first.
Otherwise, this function compares*first to a local element copy.
#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!
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 appliedstd::distance(first, last) times | applied one time fewer (for non-empty ranges) |
LWG 241 | C++98 | the value type ofInputIt was not required to beCopyConstructible | conditionally required |
LWG 538 | C++98 | the value type ofInputIt was not required to beCopyAssignable | conditionally required |
LWG 2439 | C++98 | the value type ofInputIt was not required to beCopyConstructible if OutputIt is aLegacyForwardIterator | conditionally required |
finds the first two adjacent items that are equal (or satisfy a given predicate) (function template)[edit] | |
removes consecutive duplicate elements in a range (function template)[edit] | |
(C++11) | copies a range of elements to a new location (function template)[edit] |
(C++20) | creates a copy of some range of elements that contains no consecutive duplicates (algorithm function object)[edit] |