Namespaces
Variants

std:: unique_copy

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class InputIt, class OutputIt >
OutputIt unique_copy ( InputIt first, InputIt last, OutputIt d_first ) ;
(1) (自 C++20 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt2 unique_copy ( ExecutionPolicy && policy, ForwardIt1 first,

ForwardIt1 last, ForwardIt2 d_first ) ;
(2) (自 C++17 起)
template < class InputIt, class OutputIt, class BinaryPred >

OutputIt unique_copy ( InputIt first, InputIt last,

OutputIt d_first, BinaryPred p ) ;
(3) (自 C++20 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt1,

class ForwardIt2, class BinaryPred >
ForwardIt2 unique_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, BinaryPred p ) ;
(4) (自 C++17 起)

将来自范围 [ first , last ) 的元素复制到始于 d_first 的另一范围,确保不存在连续相等的元素。仅复制每组相等元素中的首个元素。

1) 元素使用 operator == 进行比较。
如果 operator == 未建立 等价关系 ,则行为未定义。
3) 元素通过给定的二元谓词 p 进行比较。
如果 p 未建立等价关系,则行为未定义。
2,4) (1,3) 相同,但根据 policy 执行。
这些重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)

如果 * d_first = * first 无效 (C++20 前) * first 不可写入 d_first (C++20 起) ,则程序非良构。

如果源范围和目标范围重叠,则行为未定义。

给定 T 作为 InputIt 的值类型,若重载 (1) (3) 满足下列所有条件,则行为未定义:

(C++20 前)
(C++20 起)

目录

参数

first, last - 定义待处理元素源 范围 的迭代器对
d_first - 目标范围的起始位置
policy - 要使用的 执行策略
p - 返回​ true 若元素应被视为相等的二元谓词

谓词函数的签名应等价于如下形式:

bool pred ( const Type1 & a, const Type2 & b ) ;

虽然签名不需要包含 const & ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许使用 Type1 & ,也不允许使用 Type1 ,除非对于 Type1 类型移动操作等价于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 InputIt 类型的对象在解引用后能隐式转换到这两个类型。 ​

类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
OutputIt 必须满足 LegacyOutputIterator 的要求。
-
ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。

返回值

指向最后一个被写入元素之后位置的输出迭代器。

复杂度

给定 N std:: distance ( first, last )

1,2) 使用 operator == 进行恰好 max(0,N-1) 次比较。
3,4) 恰好需要 max(0,N-1) 次谓词 p 的应用。

对于重载 (2,4) ,如果 ForwardIt1 的值类型既不满足 可复制构造 也不满足 可复制赋值 要求,则可能存在性能损耗。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 是某个 标准策略 ,则调用 std::terminate 。对于其他任何 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc

可能的实现

另请参阅 libstdc++ libc++ 中的实现。

注释

如果 InputIt 满足 LegacyForwardIterator 要求,此函数会重新读取输入以检测重复项。

否则,若 OutputIt 满足 LegacyForwardIterator ,且 InputIt 的值类型与 OutputIt 相同,则此函数会比较 * d_first * first

否则,此函数将 * first 与局部元素副本进行比较。

示例

#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';
}

输出:

Before: A string with mmmany letters!
After:  A string with many letters!

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 239 C++98 谓词被应用 std:: distance ( first, last ) 减少一次应用
(针对非空范围)
LWG 241 C++98 未要求 InputIt 的值类型满足 CopyConstructible 有条件要求
LWG 538 C++98 未要求 InputIt 的值类型满足 CopyAssignable 有条件要求
LWG 2439 C++98 OutputIt LegacyForwardIterator
未要求 InputIt 的值类型满足 CopyConstructible
有条件要求

参见

查找首对相邻的相同(或满足给定谓词的)元素
(函数模板)
移除范围内的连续重复元素
(函数模板)
复制元素范围到新位置
(函数模板)
创建不含连续重复元素的范围副本
(算法函数对象)