std::remove_copy, std::remove_copy_if
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| 在标头 <algorithm> 定义
|
||
| (1) | ||
template< class InputIt, class OutputIt, class T > OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first, const T& value ); |
(C++20 起为 constexpr) (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 起) | |
| (2) | ||
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > ForwardIt2 remove_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, const T& value ); |
(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 开始的另一范围,省略满足特定判别标准的元素。
1) 忽略所有等于(用
operator== 比较)value 的元素。3) 忽略所有谓词
p 对其返回 true 的元素。2,4) 同 (1,3),但按照
policy 执行。 这些重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(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):
1,2) 应用 N 次
operator== 进行比较。3,4) 应用 N 次谓词
p。对带有 ExecutionPolicy 的重载,ForwardIt1 的 value_type 不满足可移动构造 (MoveConstructible) 时会有性能开销。
异常
拥有名为 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 = "#返回#值#优化";
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
}
输出:
擦除前:#返回#值#优化
擦除后:返回值优化
(2,2)(4,8)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 779 | C++98 | T 需要是可相等比较 (EqualityComparable) 的,但是 ForwardIt 的值类型不一定是 T
|
改成要求*d_first = *first 合法
|
参阅
| 移除满足特定条件的元素 (函数模板) | |
(C++11) |
复制范围中元素到新位置 (函数模板) |
(C++11) |
复制范围并将元素分为两组 (函数模板) |
(C++20)(C++20) |
复制范围并忽略满足特定条件的元素 (算法函数对象) |