std::copy_if() 算法
- 自 C++20 起
- 自 C++17 起
- 自 C++11 起
// (1)
template< class InputIt, class OutputIt, class UnaryPredicate >
constexpr OutputIt copy_if( InputIt first, InputIt last,
OutputIt d_first,
UnaryPredicate pred );
// (2)
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class UnaryPredicate >
ForwardIt2 copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first,
UnaryPredicate pred );
// (1)
template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if( InputIt first, InputIt last,
OutputIt d_first,
UnaryPredicate pred );
// (2)
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class UnaryPredicate >
ForwardIt2 copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first,
UnaryPredicate pred );
// (1)
template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if( InputIt first, InputIt last,
OutputIt d_first,
UnaryPredicate pred );
将由 [first
; last
) 定义的范围内的元素复制到从 d_first
开始的另一个范围。
-
(1) 仅复制谓词
pred
返回true
的元素。未定义行为如果
d_first
在范围 [first
;last
) 内,则行为未定义。 -
(2) 与 (1) 相同,但根据
policy
执行。
这些重载仅在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(直到 C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(自 C++20 起) 为 true
时参与重载决议。
参数
first second | 要复制的元素范围。 |
d_first | 目标范围的开头。 |
policy | 要使用的执行策略。有关详细信息,请参阅执行策略。 |
pred | 一元谓词,对于所需元素返回 表达式 |
类型要求
InputIt | LegacyInputIterator |
OutputIt | LegacyOutputIterator |
ForwardIt1 ForwardIt2 | LegacyForwardIterator |
一元谓词 | 谓词 |
返回值
指向目标范围内元素的输出迭代器,位于最后一个复制元素之后。
复杂度
谓词精确应用 last - first
次,介于 0
和 last - first
次赋值之间(对于谓词为 true 的每个元素进行赋值)。
对于带有 ExecutionPolicy
的重载,如果 ForwardIt1
的值类型不是 可移动构造的
,则可能会有性能开销。
异常
带有模板参数 ExecutionPolicy
的重载报告错误如下
- 如果作为算法一部分调用的函数执行时抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用std::terminate
。对于其他任何ExecutionPolicy
,行为是实现定义的. - 如果算法未能分配内存,则抛出
std::bad_alloc
。
可能的实现
copy_if (1)
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last,
OutputIt d_first, UnaryPredicate pred)
{
for (; first != last; ++first)
{
if (pred(*first))
{
*d_first = *first;
++d_first;
}
}
return d_first;
}
备注
实际上,如果值类型是平凡可复制的
,并且迭代器类型满足旧式连续迭代器
,std::copy_if
的实现会避免多次赋值,并使用批量 copy_if 函数,例如std::memmove
。
当 copy_if 重叠范围时,向左 copy_if(目标范围的开始在源范围之外)时适合使用std::copy_if
,而向右 copy_if(目标范围的结束在源范围之外)时适合使用std::copy_if_backward
。
示例
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
std::vector<int> from_vector(10);
std::iota(from_vector.begin(), from_vector.end(), 0);
std::vector<int> to_vector;
std::copy_if(from_vector.begin(), from_vector.end(),
std::back_inserter(to_vector));
// or, alternatively,
// std::vector<int> to_vector(from_vector.size());
// std::copy_if(from_vector.begin(), from_vector.end(), to_vector.begin());
// either way is equivalent to
// std::vector<int> to_vector = from_vector;
std::cout << "to_vector contains: ";
std::copy_if(to_vector.begin(), to_vector.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
std::cout << "odd numbers in to_vector are: ";
std::copy_if_if(to_vector.begin(), to_vector.end(),
std::ostream_iterator<int>(std::cout, " "),
[](int x) { return x % 2 != 0; });
std::cout << '\n';
std::cout << "to_vector contains these multiples of 3: ";
to_vector.clear();
std::copy_if_if(from_vector.begin(), from_vector.end(),
std::back_inserter(to_vector),
[](int x) { return x % 3 == 0; });
for (int x : to_vector)
std::cout << x << ' ';
std::cout << '\n';
}
to_vector contains: 0 1 2 3 4 5 6 7 8 9
odd numbers in to_vector are: 1 3 5 7 9
to_vector contains these multiples of 3: 0 3 6 9