跳到主要内容

std::remove_if() 算法

// (1)
template< class ForwardIt, class UnaryPredicate >
constexpr ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );

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

  • (1) 移除谓词 p 返回 true 的所有元素。

  • (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))范围内的元素来完成,使不被移除的元素出现在范围的开头。

重要

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

警告

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

参数

first
last

要处理的元素范围。

policy

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

p

一元谓词,如果元素应被移除,则返回 true

对于类型为(可能为 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,且不得修改 v。因此,不允许使用参数类型 VT&,也不允许使用 VT,除非对于 VT 移动等同于复制。 (自 C++11 起)

类型要求

ForwardItLegacyForwardIterator
UnaryPredicatePredicate

返回值

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

复杂度

给定 Nstd::distance(first, last)

谓词 p 最多应用 N 次。

异常

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

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

可能的实现

remove_if (1)
template<class ForwardIt, class UnaryPredicate>
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
{
first = std::find_if(first, last, p);
if (first != last)
for (ForwardIt i = first; ++i != last;)
if (!p(*i))
*first++ = std::move(*i);
return first;
}

备注

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

名称类似的容器成员函数 list::remove_iflist::remove_if_ifforward_list::remove_ifforward_list::remove_if_if 会擦除 remove_if 的元素。

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

示例

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

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_if(str1.begin(), str1.end(), ' ');

// The spaces are remove_ifd 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 remove_ifd 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_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_if() 算法

// (1)
template< class ForwardIt, class UnaryPredicate >
constexpr ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );

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

  • (1) 移除谓词 p 返回 true 的所有元素。

  • (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))范围内的元素来完成,使不被移除的元素出现在范围的开头。

重要

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

警告

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

参数

first
last

要处理的元素范围。

policy

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

p

一元谓词,如果元素应被移除,则返回 true

对于类型为(可能为 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,且不得修改 v。因此,不允许使用参数类型 VT&,也不允许使用 VT,除非对于 VT 移动等同于复制。 (自 C++11 起)

类型要求

ForwardItLegacyForwardIterator
UnaryPredicatePredicate

返回值

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

复杂度

给定 Nstd::distance(first, last)

谓词 p 最多应用 N 次。

异常

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

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

可能的实现

remove_if (1)
template<class ForwardIt, class UnaryPredicate>
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
{
first = std::find_if(first, last, p);
if (first != last)
for (ForwardIt i = first; ++i != last;)
if (!p(*i))
*first++ = std::move(*i);
return first;
}

备注

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

名称类似的容器成员函数 list::remove_iflist::remove_if_ifforward_list::remove_ifforward_list::remove_if_if 会擦除 remove_if 的元素。

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

示例

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

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_if(str1.begin(), str1.end(), ' ');

// The spaces are remove_ifd 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 remove_ifd 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_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 页面。它可能为了改进或编辑偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。