Namespaces
Variants

std::unique

来自 zh.cppreference.net
 
 
算法库
约束算法和范围算法 (C++20)
约束算法,例如 ranges::copy, ranges::sort, ...
排序和相关操作
划分操作
(C++11)    

排序操作
二分查找操作
(在划分后的范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <algorithm>
template< class ForwardIt >
ForwardIt unique( ForwardIt first, ForwardIt last );
(1) (自 C++20 起为 constexpr)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt unique( ExecutionPolicy&& policy,
                  ForwardIt first, ForwardIt last );
(2) (自 C++17 起)
template< class ForwardIt, class BinaryPred >
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPred p );
(3) (自 C++20 起为 constexpr)
template< class ExecutionPolicy,
          class ForwardIt, class BinaryPred >
ForwardIt unique( ExecutionPolicy&& policy,
                  ForwardIt first, ForwardIt last, BinaryPred p );
(4) (自 C++17 起)

从范围 [first, last) 中每个连续的等价元素组中移除除第一个元素外的所有元素,并返回指向范围新末尾的尾后迭代器。

1) 元素使用 operator==.
如果 operator== 没有建立 等价关系,则行为未定义。
3) 元素使用给定的二元谓词 p.
如果 p 没有建立等价关系,则行为未定义。
2,4) 与 (1,3) 相同,但根据 policy.
这些重载仅当以下表达式的值为 true:

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

(直到 C++20)

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

(自 C++20 起)

说明

移除操作通过移动范围内的元素来实现,使得不需要被移除的元素出现在范围的起始位置。

  • 移位操作通过 复制赋值 (C++11 前) 移动赋值 (C++11 起) 完成。
  • 移除操作是稳定的:未被移除元素的相对顺序保持不变。
  • 底层序列 [ first , last ) 的长度不会因移除操作而缩短。设返回的迭代器为 result :
  • 所有位于 [ result , last ) 区间内的迭代器仍然保持 可解引用 特性。
  • [ result , last ) 范围内的每个元素都处于有效但未指定的状态,因为移动赋值操作可能通过从原属于该范围的元素中移出内容来消除元素。
(since C++11)

参数

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

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

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

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

类型要求
-
ForwardIt 必须满足 向前迭代器 的要求。
-
解引用 ForwardIt 所得的类型必须满足 可移动赋值 的要求。

返回值

指向范围新末尾的 ForwardIt 。

复杂度

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

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

异常

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

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

可能的实现

另请参阅以下实现: libstdc++ 、 libc++ 和 MSVC STL 。

unique (1)
template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
    ForwardIt result = first;
    while (++first != last)
        if (!(*result == *first) && ++result != first)
            *result = std::move(*first);
    return ++result;
}
unique (3)
template<class ForwardIt, class BinaryPredicate>
ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPredicate p)
{
    if (first == last)
        return last;
    ForwardIt result = first;
    while (++first != last)
        if (!p(*result, *first) && ++result != first)
            *result = std::move(*first);
    return ++result;
}
说明: - 保留了所有HTML标签和属性 - 未翻译 ` ` 和 `
` 标签内的C++代码
- 仅翻译了函数签名外的文本描述(版本号)
- 保持了原始格式和结构

注解

调用 unique 后通常需要接着调用容器的 erase 成员函数,以实际从容器中移除元素。

示例

#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
    // 包含若干重复元素的向量
    std::vector<int> v{1, 2, 1, 1, 3, 3, 3, 4, 5, 4};
    auto print = [&](int id)
    {
        std::cout << "@" << id << ": ";
        for (int i : v)
            std::cout << i << ' ';
        std::cout << '\n';
    };
    print(1);
    // 移除连续(相邻)的重复元素
    auto last = std::unique(v.begin(), v.end());
    // v 现在包含 {1 2 1 3 4 5 4 x x x},其中 'x' 为不确定值
    v.erase(last, v.end());
    print(2);
    // 先排序再使用 unique,以移除所有重复项
    std::sort(v.begin(), v.end()); // {1 1 2 3 4 4 5}
    print(3);
    last = std::unique(v.begin(), v.end());
    // v 现在包含 {1 2 3 4 5 x x},其中 'x' 为不确定值
    v.erase(last, v.end());
    print(4);
}

输出:

@1: 1 2 1 1 3 3 3 4 5 4
@2: 1 2 1 3 4 5 4
@3: 1 1 2 3 4 4 5
@4: 1 2 3 4 5

缺陷报告

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

DR 适用范围 发布时的行为 正确行为
LWG 202 C++98 若使用非等价关系比较元素
其行为不明确
此情况下的行为
是未定义的

另见

查找第一对相等的相邻元素(或满足给定谓词的一对元素)
(函数模板 & 算法函数对象)
创建某个范围元素的副本,该副本不包含连续重复项
(函数模板 & 算法函数对象)
移除满足特定条件的元素
(函数模板 & 算法函数对象)
移除连续重复元素
(公开成员函数,属于 std::list<T,Allocator>)
移除连续重复元素
(公开成员函数,属于 std::forward_list<T,Allocator>)
移除范围中的连续重复元素
(算法函数对象)