跳到主要内容

std::remove() 算法

// (1)
template< class ForwardIt, class T >
constexpr ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );

// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
ForwardIt remove( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, const T& value );

从范围 [first; last) 中移除所有满足特定条件的元素,并返回新范围的尾后迭代器。

  • (1) 移除所有等于 value 的元素(使用 operator==)。

  • (2)(1) 相同,但根据 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 的类型不是 MoveAssignable,则 行为未定义.

移除是通过将范围中的元素移位(通过移动赋值 (C++11 起)复制赋值 (C++11 之前))来实现的,使得不被移除的元素出现在范围的开头。

重要

保留其余元素的相对顺序,且容器的物理大小不变。

警告

指向新逻辑末尾和范围物理末尾之间元素的迭代器仍然可解引用但元素本身的值未指定(根据MoveAssignable 后置条件)。 (C++11 起)

参数

first
last

要处理的元素范围。

要移除的元素的值。

policy

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

类型要求

ForwardItLegacyForwardIterator

返回值

新值范围的尾后迭代器(如果这不是 end,则它指向一个未指定的值,此迭代器与 end 之间的任何值迭代器也是如此)。

复杂度

给定 Nstd::distance(first, last)

最多 N 次与 value 使用 operator== 进行比较。

异常

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

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

可能的实现

remove (1)
template<class ForwardIt, class T>
ForwardIt remove(ForwardIt first, ForwardIt last, const T& value)
{
first = std::find(first, last, value);
if (first != last)
for (ForwardIt i = first; ++i != last;)
if (!(*i == value))
*first++ = std::move(*i);
return first;
}

备注

通常在调用 remove 之后会调用容器的 erase 成员函数,该函数会擦除未指定的值并将容器的物理大小减小以匹配其新的逻辑大小。这两个调用共同构成了所谓的Erase–remove idiom,可以通过自由函数 std::erase(对所有标准序列容器有重载)或 std::erase_if(对所有标准容器有重载)实现 (C++20 起)

命名相似的容器成员函数list::removelist::remove_ifforward_list::removeforward_list::remove_if 会擦除被移除的元素。

这些算法不能与关联容器一起使用,例如 std::setstd::map,因为它们的迭代器类型不能解引用为 MoveAssignable 类型(这些容器中的键不可修改)。

标准库还在 <cstdio> 中定义了 std::remove 的重载,它接受一个 const char* 并用于删除文件。

因为 std::remove 通过引用获取 value,如果它是范围 [first; last) 中元素的引用,则可能导致意外行为。

示例

以下代码通过将所有非空格字符左移然后擦除多余字符来从字符串中删除所有空格。这是一个Erase-remove idiom的例子。

Main.cpp
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <string_view>

int main()
{
std::string str1 {"Text with some spaces"};

auto noSpaceEnd = std::remove(str1.begin(), str1.end(), ' ');

// The spaces are removed from the string only logically.
// Note, we use view, the original string is still not shrunk:
std::cout << std::string_view(str1.begin(), noSpaceEnd)
<< " size: " << str1.size() << '\n';

str1.erase(noSpaceEnd, str1.end());

// The spaces are removed from the string physically.
std::cout << str1 << " size: " << str1.size() << '\n';

std::string str2 = "Text\n with\tsome \t whitespaces\n\n";
str2.erase(std::remove_if(str2.begin(),
str2.end(),
[](unsigned char x) { return std::isspace(x); }),
str2.end());
std::cout << str2 << '\n';
}
输出
Textwithsomespaces size: 23
Textwithsomespaces size: 18
Textwithsomewhitespaces
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”可查看本文档的所有更改。
悬停查看原始许可证。

std::remove() 算法

// (1)
template< class ForwardIt, class T >
constexpr ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );

// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
ForwardIt remove( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, const T& value );

从范围 [first; last) 中移除所有满足特定条件的元素,并返回新范围的尾后迭代器。

  • (1) 移除所有等于 value 的元素(使用 operator==)。

  • (2)(1) 相同,但根据 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 的类型不是 MoveAssignable,则 行为未定义.

移除是通过将范围中的元素移位(通过移动赋值 (C++11 起)复制赋值 (C++11 之前))来实现的,使得不被移除的元素出现在范围的开头。

重要

保留其余元素的相对顺序,且容器的物理大小不变。

警告

指向新逻辑末尾和范围物理末尾之间元素的迭代器仍然可解引用但元素本身的值未指定(根据MoveAssignable 后置条件)。 (C++11 起)

参数

first
last

要处理的元素范围。

要移除的元素的值。

policy

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

类型要求

ForwardItLegacyForwardIterator

返回值

新值范围的尾后迭代器(如果这不是 end,则它指向一个未指定的值,此迭代器与 end 之间的任何值迭代器也是如此)。

复杂度

给定 Nstd::distance(first, last)

最多 N 次与 value 使用 operator== 进行比较。

异常

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

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

可能的实现

remove (1)
template<class ForwardIt, class T>
ForwardIt remove(ForwardIt first, ForwardIt last, const T& value)
{
first = std::find(first, last, value);
if (first != last)
for (ForwardIt i = first; ++i != last;)
if (!(*i == value))
*first++ = std::move(*i);
return first;
}

备注

通常在调用 remove 之后会调用容器的 erase 成员函数,该函数会擦除未指定的值并将容器的物理大小减小以匹配其新的逻辑大小。这两个调用共同构成了所谓的Erase–remove idiom,可以通过自由函数 std::erase(对所有标准序列容器有重载)或 std::erase_if(对所有标准容器有重载)实现 (C++20 起)

命名相似的容器成员函数list::removelist::remove_ifforward_list::removeforward_list::remove_if 会擦除被移除的元素。

这些算法不能与关联容器一起使用,例如 std::setstd::map,因为它们的迭代器类型不能解引用为 MoveAssignable 类型(这些容器中的键不可修改)。

标准库还在 <cstdio> 中定义了 std::remove 的重载,它接受一个 const char* 并用于删除文件。

因为 std::remove 通过引用获取 value,如果它是范围 [first; last) 中元素的引用,则可能导致意外行为。

示例

以下代码通过将所有非空格字符左移然后擦除多余字符来从字符串中删除所有空格。这是一个Erase-remove idiom的例子。

Main.cpp
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <string_view>

int main()
{
std::string str1 {"Text with some spaces"};

auto noSpaceEnd = std::remove(str1.begin(), str1.end(), ' ');

// The spaces are removed from the string only logically.
// Note, we use view, the original string is still not shrunk:
std::cout << std::string_view(str1.begin(), noSpaceEnd)
<< " size: " << str1.size() << '\n';

str1.erase(noSpaceEnd, str1.end());

// The spaces are removed from the string physically.
std::cout << str1 << " size: " << str1.size() << '\n';

std::string str2 = "Text\n with\tsome \t whitespaces\n\n";
str2.erase(std::remove_if(str2.begin(),
str2.end(),
[](unsigned char x) { return std::isspace(x); }),
str2.end());
std::cout << str2 << '\n';
}
输出
Textwithsomespaces size: 23
Textwithsomespaces size: 18
Textwithsomewhitespaces
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”可查看本文档的所有更改。
悬停查看原始许可证。