std::remove_copy, std::remove_copy_if
| 定义于头文件 <algorithm>
|
||
template< class InputIt, class OutputIt, class T >
OutputIt remove_copy( InputIt first, InputIt last,
OutputIt d_first, const T& value );
|
(1) | (到 C++26) |
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 );
|
(从 C++26 起) | |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 remove_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, const T& value );
|
(2) | (从 C++17 起) (到 C++26) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class T = typename std::iterator_traits
<ForwardIt1>::value_type >
ForwardIt2 remove_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, const T& value );
|
(从 C++26 起) | |
template< class InputIt, class OutputIt, class UnaryPred >
OutputIt remove_copy_if( InputIt first, InputIt last,
OutputIt d_first, UnaryPred p );
|
(3) | (从 C++20 起为 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class UnaryPred >
ForwardIt2 remove_copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, UnaryPred p );
|
(4) | (从 C++17 起) |
复制来自范围 [first, last) 的元素到始于 d_first 的另一范围,并忽略满足特定条件的元素。
value 的元素(使用 operator== 比较)。p 对其返回 true 的元素。policy 执行。true 时才参与重载决议:
|
|
(到 C++20) |
|
|
(从 C++20 起) |
如果 *d_first = *first 无效(到 C++20)*first 不可写入writable 到 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++20)(C++20) |
|
(C++11) |
将元素范围复制到新位置 (函数模板 & 算法函数对象) |
(C++20)(C++20) |
|
(C++11) |
将一个范围复制并将元素分为两组 (函数模板 & 算法函数对象) |
(C++20) |
|
(C++20)(C++20) |
复制元素范围,省略满足特定条件的元素 (算法函数对象) |