std:: remove, std:: remove_if
|
定义于头文件
<algorithm>
|
||
| (1) | ||
|
template
<
class
ForwardIt,
class
T
>
ForwardIt remove ( ForwardIt first, ForwardIt last, const T & value ) ; |
(自 C++20 起为 constexpr)
(C++26 前) |
|
|
template
<
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
<
ForwardIt
>
::
value_type
>
|
(自 C++26 起) | |
| (2) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
T
>
ForwardIt remove
(
ExecutionPolicy
&&
policy,
|
(自 C++17 起)
(C++26 前) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
|
(自 C++26 起) | |
|
template
<
class
ForwardIt,
class
UnaryPred
>
ForwardIt remove_if ( ForwardIt first, ForwardIt last, UnaryPred p ) ; |
(3) | (自 C++20 起为 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
ForwardIt remove_if
(
ExecutionPolicy
&&
policy,
|
(4) | (自 C++17 起) |
移除范围
[
first
,
last
)
中所有满足特定条件的元素,并返回指向范围新末尾的尾后迭代器。
|
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 前) | |
|
若 * first 的类型不可 移动赋值 ,则行为未定义。 |
(C++11 起) |
目录 |
说明
移除操作通过移动范围内的元素来实现,使得不需要被移除的元素出现在范围的起始位置。
- 移位操作通过 复制赋值 (C++11 前) 移动赋值 (C++11 起) 完成。
- 移除操作是稳定的:不被移除元素的相对顺序保持不变。
-
底层序列
[first,last)的范围不会因移除操作而缩短。设返回的迭代器为 result :
-
-
范围
[result,last)内的所有迭代器仍保持 可解引用 特性。
-
范围
|
(since C++11) |
参数
| first, last | - | 定义待处理元素范围的迭代器对 |
| value | - | 要移除的元素值 |
| policy | - | 使用的执行策略 |
| p | - |
一元谓词,当元素应被移除时返回
true
。
表达式
p
(
v
)
必须可转换为
bool
,其中
|
| 类型要求 | ||
-
ForwardIt
必须满足
LegacyForwardIterator
的要求。
|
||
-
UnaryPredicate
必须满足
Predicate
的要求。
|
||
返回值
指向新值范围末尾的迭代器(若该迭代器不等于 end ,则其指向未指定的值,且该迭代器与 end 之间的所有迭代器均指向未指定值)。
复杂度
给定 N 为 std:: distance ( first, last ) :
异常
带有名为
ExecutionPolicy
模板参数的重载按以下方式报告错误:
-
如果作为算法一部分调用的函数执行抛出异常,且
ExecutionPolicy是某个 标准策略 ,则会调用 std::terminate 。对于任何其他ExecutionPolicy,其行为由实现定义。 - 如果算法无法分配内存,将抛出 std::bad_alloc 。
可能的实现
| remove (1) |
|---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> 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_if (3) |
template<class ForwardIt, class UnaryPred> ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPred 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; } |
标签内的C++代码未作翻译,仅对页面中的非代码文本进行了简体中文翻译。
注释
对
remove
的调用通常紧随容器
erase
成员函数的调用,以实际从容器中移除元素。这两个调用共同构成了所谓的
erase-remove idiom
。
|
相同的效果也可以通过以下非成员函数实现:
|
(since C++20) |
同名的容器 成员函数 list::remove 、 list::remove_if 、 forward_list::remove 和 forward_list::remove_if 会擦除被移除的元素。
这些算法不能用于关联容器,例如 std::set 和 std::map ,因为它们的迭代器类型解引用后不是 MoveAssignable 类型(这些容器中的键不可修改)。
标准库还在 <cstdio> 中定义了 std::remove 的一个重载版本,该版本接受 const char * 类型参数,用于删除文件。
由于
std::remove
通过引用接收
value
参数,若该引用指向区间
[
first
,
last
)
内的元素,可能导致预期外的行为。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 算法列表初始化 ( 1,2 ) |
示例
以下代码通过将所有非空格字符左移并擦除多余部分来移除字符串中的所有空格。这是 擦除-移除惯用法 的示例。
#include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <iomanip> #include <iostream> #include <string> #include <string_view> #include <vector> int main() { std::string str1{"Quick Red Dog"}; std::cout << "1) " << std::quoted(str1) << '\n'; const auto noSpaceEnd = std::remove(str1.begin(), str1.end(), ' '); std::cout << "2) " << std::quoted(str1) << '\n'; // 空格仅从逻辑上从字符串中移除 // 注意:我们使用视图,原始字符串实际尚未收缩: std::cout << "3) " << std::quoted(std::string_view(str1.begin(), noSpaceEnd)) << ", size: " << str1.size() << '\n'; str1.erase(noSpaceEnd, str1.end()); // 空格已从物理上从字符串中移除 std::cout << "4) " << std::quoted(str1) << ", size: " << str1.size() << '\n'; std::string str2 = "Jumped\n Over\tA\vLazy \t Fox\r\n"; str2.erase(std::remove_if(str2.begin(), str2.end(), [](unsigned char x) { return std::isspace(x); }), str2.end()); std::cout << "5) " << std::quoted(str2) << '\n'; std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}}; #ifdef __cpp_lib_algorithm_default_value_type nums.erase(std::remove(nums.begin(), nums.end(), {1, 3}), nums.end()); #else nums.erase(std::remove(nums.begin(), nums.end(), std::complex<double>{1, 3}), nums.end()); #endif assert((nums == std::vector<std::complex<double>>{{2, 2}, {4, 8}})); }
输出:
1) "Quick Red Dog" 2) "QuickRedDog Dog" 3) "QuickRedDog", size: 15 4) "QuickRedDog", size: 11 5) "JumpedOverALazyFox"
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 283 | C++98 |
T
被要求满足
EqualityComparable
要求,但
ForwardIt
的值类型并不总是
T
|
改为要求
ForwardIt
的值类型
满足 CopyAssignable 要求 |
参见
|
复制范围元素并忽略满足特定条件的元素
(函数模板) |
|
|
移除范围内的连续重复元素
(函数模板) |
|
|
(C++20)
(C++20)
|
移除满足特定条件的元素
(算法函数对象) |