std:: remove_copy, std:: remove_copy_if
|
定义于头文件
<algorithm>
|
||
| (1) | ||
|
template
<
class
InputIt,
class
OutputIt,
class
T
>
OutputIt remove_copy
(
InputIt first, InputIt last,
|
(自 C++20 起为 constexpr)
(C++26 前) |
|
|
template
<
class
InputIt,
class
OutputIt,
class
T
=
typename
std::
iterator_traits
|
(自 C++26 起) | |
| (2) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
T
>
|
(自 C++17 起)
(C++26 前) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
|
(自 C++26 起) | |
|
template
<
class
InputIt,
class
OutputIt,
class
UnaryPred
>
OutputIt remove_copy_if
(
InputIt first, InputIt last,
|
(3) | (自 C++20 起为 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
UnaryPred
>
|
(4) | (自 C++17 起) |
将范围
[
first
,
last
)
中的元素复制到始于
d_first
的另一范围,同时省略满足特定条件的元素。
|
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 起) |
如果 * d_first = * first 无效 (C++20 前) * first 不可写入 d_first (C++20 起) ,则程序非良构。
如果源范围和目标范围重叠,则行为未定义。
目录 |
参数
| first, last | - | 定义待复制元素源 范围 的迭代器对 |
| d_first | - | 目标范围的起始位置 |
| value | - | 不进行复制的元素值 |
| policy | - | 使用的 执行策略 |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
-
OutputIt
必须满足
LegacyOutputIterator
的要求。
|
||
-
ForwardIt1, ForwardIt2
必须满足
LegacyForwardIterator
的要求。
|
||
-
UnaryPred
必须满足
Predicate
的要求。
|
||
返回值
指向最后一个被复制元素之后位置的迭代器。
复杂度
给定 N 为 std:: distance ( first, last ) :
对于带有 ExecutionPolicy 的重载,如果
ForwardIt1
的
value_type
不满足
可移动构造
要求,则可能会产生性能开销。
异常
带有名为
ExecutionPolicy
模板参数的重载按以下方式报告错误:
-
如果作为算法一部分调用的函数执行抛出异常,且
ExecutionPolicy是某个 标准策略 ,则调用 std::terminate 。对于其他任何ExecutionPolicy,其行为由实现定义。 - 如果算法无法分配内存,则抛出 std::bad_alloc 。
可能的实现
| remove_copy (1) |
|---|
template<class InputIt, class OutputIt, class T = typename std::iterator_traits<InputIt>::value_type> constexpr OutputIt remove_copy(InputIt first, InputIt last, OutputIt d_first, const T& value) { for (; first != last; ++first) if (!(*first == value)) *d_first++ = *first; return d_first; } |
| remove_copy_if (3) |
template<class InputIt, class OutputIt, class UnaryPred> constexpr OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred p) { for (; first != last; ++first) if (!p(*first)) *d_first++ = *first; return d_first; } |
注释
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 列表初始化 用于算法 ( 1,2 ) |
示例
#include <algorithm> #include <complex> #include <iomanip> #include <iostream> #include <iterator> #include <string> #include <vector> int main() { // 动态移除井号字符 '#' std::string str = "#Return #Value #Optimization"; std::cout << "处理前: " << std::quoted(str) << '\n'; std::cout << "处理后: \""; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), '#'); std::cout << "\"\n"; // 动态移除 {1, 3} 数值 std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}}; std::remove_copy(nums.begin(), nums.end(), std::ostream_iterator<std::complex<double>>(std::cout), #ifdef __cpp_lib_algorithm_default_value_type {1, 3}); // T 自动推导 #else std::complex<double>{1, 3}); #endif }
输出:
处理前: "#Return #Value #Optimization" 处理后: "Return Value Optimization" (2,2)(4,8)
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 779 | C++98 |
要求
T
满足
EqualityComparable
要求,但
ForwardIt
的值类型并不总是
T
|
改为要求
* d_first = * first 表达式必须合法 |
参见
|
移除满足特定条件的元素
(函数模板) |
|
|
(C++11)
|
复制元素范围到新位置
(函数模板) |
|
(C++11)
|
复制范围并将元素划分为两组
(函数模板) |
|
(C++20)
(C++20)
|
复制元素范围并忽略满足特定条件的元素
(算法函数对象) |