std::unique
定义于头文件 <algorithm> |
||
template< class ForwardIt > ForwardIt unique( ForwardIt first, ForwardIt last ); |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt unique( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class ForwardIt, class BinaryPred > ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPred p ); |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class BinaryPred > |
(4) | (since C++17) |
从范围 [
first,
last)
的每个连续等价元素组中移除第一个元素之后的所有元素,并返回指向新范围末尾的 past-the-end 迭代器。
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 起) |
内容 |
[编辑] 解释
移除操作通过移动范围内的元素来完成,使得不被移除的元素出现在范围的开头。
- 移动通过 复制赋值(C++11 前)移动赋值(C++11 起)完成。
- 移除操作是稳定的:不被移除的元素的相对顺序保持不变。
- 范围
[
first,
last)
的底层序列不会因移除操作而缩短。给定 result 作为返回的迭代器
- 范围
[
result,
last)
中的所有迭代器仍然是 可解引用的。
- 范围
|
(C++11 起) |
[编辑] 参数
first, last | - | 定义要处理的元素范围的迭代器对 |
policy | - | 要使用的执行策略 |
p | - | 如果元素应被视为相等则返回 true 的二元谓词。 谓词函数的签名应等效于以下内容 bool pred(const Type1 &a, const Type2 &b); 虽然签名不需要具有 const &,但该函数不得修改传递给它的对象,并且必须能够接受类型(可能是 const) |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-解引用的 ForwardIt 的类型必须满足 MoveAssignable 的要求。 |
[编辑] 返回值
指向范围新末尾的 ForwardIt
。
[编辑] 复杂度
给定 N 为 std::distance(first, last)
[编辑] 异常
带有名为 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; } |
[编辑] 注解
对 unique
的调用通常后跟对容器的 erase
成员函数的调用,以实际从容器中移除元素。
[编辑] 示例
#include <algorithm> #include <iostream> #include <vector> int main() { // a vector containing several duplicate elements 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); // remove consecutive (adjacent) duplicates auto last = std::unique(v.begin(), v.end()); // v now holds {1 2 1 3 4 5 4 x x x}, where 'x' is indeterminate v.erase(last, v.end()); print(2); // sort followed by unique, to remove all duplicates std::sort(v.begin(), v.end()); // {1 1 2 3 4 4 5} print(3); last = std::unique(v.begin(), v.end()); // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate 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> 的公共成员函数) | |
(C++20) |
移除范围中连续重复的元素 (算法函数对象) |