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 的类型不可移动赋值 (MoveAssignable) ,那么行为未定义。  | 
(C++11 起) | 
目录 | 
[编辑] 解释
移除元素是通过将范围内的元素移动位置,使得不需要被移除的元素会在范围的开头出现的方式实现的。
- 元素通过复制赋值(C++11 前)移动赋值(C++11 起)实现。
 - 移除操作是稳定的:不需要被移除的元素的相对顺序保持不变。
 -  移除操作不会缩短 
[first,last)的底层序列。给定 result 为返回的迭代器: 
-  
[result,last)中的所有迭代器仍然可解引用。 
-  
 
  | 
(C++11 起) | 
[编辑] 参数
| first, last | - | 要处理的元素范围的迭代器对 | 
| value | - | 要移除的元素值 | 
| policy | - | 所用的执行策略 | 
| p | - |  如果应该移除元素则返回 true 的一元谓词。 对每个(可为 const 的)   | 
| 类型要求 | ||
 -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; }  | 
[编辑] 注解
调用 remove 之后通常跟随调用容器的 erase 成员函数来从容器中实际移除元素。这两个调用一并被称为擦除移除手法。
| 
 以下非成员函数也可以达到相同的效果:  | 
(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)) << ",大小:" << str1.size() << '\n'; str1.erase(noSpaceEnd, str1.end()); // 物理移除字符串中的空格。 std::cout << "4) " << std::quoted(str1) << ",大小:" << 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",大小:15 4) "QuickRedDog",大小:11 5) "JumpedOverALazyFox"
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 283 | C++98 |  T 需要是可相等比较 (EqualityComparable) 的,但是 ForwardIt 的值类型不一定是 T
 | 
 改成要求 ForwardIt 的值类型是可复制赋值 (CopyAssignable) 的  | 
[编辑] 参阅
|    复制范围并忽略满足特定条件的元素  (函数模板)  | |
|    移除范围中连续重复元素   (函数模板)  | |
|    (C++20)(C++20)  | 
  移除满足特定条件的元素  (算法函数对象)  |