跳到主要内容

std::unique() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt unique( ForwardIt first, ForwardIt last );

// (2)
template< class ForwardIt, class BinaryPredicate >
constexpr ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );

// (3)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt unique( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );

// (4)
template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate >
ForwardIt unique( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, BinaryPredicate p );

消除范围 [first; last) 中每个连续等价元素组中除第一个元素外的所有元素,并返回表示该范围新逻辑末尾的 past-the-end 迭代器。

  • (1) 元素使用 operator== 进行比较。

  • (2) 元素使用给定的二元谓词 p 进行比较。

  • (3 - 4)(1 - 2) 相同,但根据 policy 执行。

    重载决议

    这些重载仅在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (C++20 之前) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (C++20 起)true 时参与重载决议。

重要

移除通过移动范围中的元素来实现,使得要擦除的元素被覆盖。

未定义行为

行为未定义

如果它不是等价关系。

参数

first
last

要处理的元素范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

p

二元谓词,如果元素应被视为相等,则返回 true

函数的签名应与以下内容等效

bool fun(const Type1& a, const Type2& b);
  • 签名不需要包含 const&
  • 函数不得修改传递给它的对象。
  • 必须接受 Type1Type2 类型(可能带 const)的所有值,无论值类别如何(因此不允许 Type1&除非对于 Type1,移动等同于复制,否则也不允许 Type1 (C++11 起)
  • Type1Type2 的类型必须使得 ForwardIt 类型的对象可以被解引用,然后隐式转换为它们。

类型要求

ForwardItLegacyForwardIterator
解引用 ForwardItMoveAssignable

返回值

对于非空范围,精确地执行 td::distance(first, last) - 1 次相应谓词的应用。

复杂度

给定 Nstd::distance(first, last)

最多执行 N 次谓词 p 的应用。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

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

可能的实现

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 (2)
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;
}

备注

剩余元素的相对顺序得到保留,容器的物理大小不变。在 [r; last)(如果有)中的迭代器,其中 r 是返回值,仍然可解引用,但元素本身具有未指定的值。

通常在调用 std::unique 之后会调用容器的 erase 成员函数,该函数会擦除未指定的值,并将容器的物理大小减小以匹配其新的逻辑大小。

示例

以下代码通过将所有非空格字符左移然后擦除多余的空格来从字符串中唯一化所有空格。这是擦除-移除习语的一个示例。

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::unique() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt unique( ForwardIt first, ForwardIt last );

// (2)
template< class ForwardIt, class BinaryPredicate >
constexpr ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );

// (3)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt unique( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );

// (4)
template< class ExecutionPolicy, class ForwardIt, class BinaryPredicate >
ForwardIt unique( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, BinaryPredicate p );

消除范围 [first; last) 中每个连续等价元素组中除第一个元素外的所有元素,并返回表示该范围新逻辑末尾的 past-the-end 迭代器。

  • (1) 元素使用 operator== 进行比较。

  • (2) 元素使用给定的二元谓词 p 进行比较。

  • (3 - 4)(1 - 2) 相同,但根据 policy 执行。

    重载决议

    这些重载仅在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (C++20 之前) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (C++20 起)true 时参与重载决议。

重要

移除通过移动范围中的元素来实现,使得要擦除的元素被覆盖。

未定义行为

行为未定义

如果它不是等价关系。

参数

first
last

要处理的元素范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

p

二元谓词,如果元素应被视为相等,则返回 true

函数的签名应与以下内容等效

bool fun(const Type1& a, const Type2& b);
  • 签名不需要包含 const&
  • 函数不得修改传递给它的对象。
  • 必须接受 Type1Type2 类型(可能带 const)的所有值,无论值类别如何(因此不允许 Type1&除非对于 Type1,移动等同于复制,否则也不允许 Type1 (C++11 起)
  • Type1Type2 的类型必须使得 ForwardIt 类型的对象可以被解引用,然后隐式转换为它们。

类型要求

ForwardItLegacyForwardIterator
解引用 ForwardItMoveAssignable

返回值

对于非空范围,精确地执行 td::distance(first, last) - 1 次相应谓词的应用。

复杂度

给定 Nstd::distance(first, last)

最多执行 N 次谓词 p 的应用。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

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

可能的实现

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 (2)
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;
}

备注

剩余元素的相对顺序得到保留,容器的物理大小不变。在 [r; last)(如果有)中的迭代器,其中 r 是返回值,仍然可解引用,但元素本身具有未指定的值。

通常在调用 std::unique 之后会调用容器的 erase 成员函数,该函数会擦除未指定的值,并将容器的物理大小减小以匹配其新的逻辑大小。

示例

以下代码通过将所有非空格字符左移然后擦除多余的空格来从字符串中唯一化所有空格。这是擦除-移除习语的一个示例。

Main.cpp
#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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。