std::ranges::copy_if() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr copy_if_result<I, O>
copy_if( I first, S last, O result, Pred pred, Proj proj = {} );
// (2)
constexpr copy_if_result<ranges::borrowed_iterator_t<R>, O>
copy_if( R&& r, O result, Pred pred, Proj proj = {} );
参数类型是泛型的,并具有以下约束:
I
-std::input_iterator
S
-std::sentinel_for<I>
O
-std::weakly_incrementable
Proj
- (无)Pred
:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>>
- (2) -
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
对于所有重载,Proj
模板参数的默认类型为 std::identity
。
此外,每个重载都有以下约束
- (1) -
std::indirectly_copyable<I, O>
- (2) -
std::indirectly_copyable<ranges::iterator_t<R>, O>
// (1)
template<
std::input_iterator I,
std::sentinel_for<I> S,
std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
>
requires std::indirectly_copyable<I, O>
constexpr copy_if_result<I, O>
copy_if( I first, S last, O result, Pred pred, Proj proj = {} );
// (2)
template<
ranges::input_range R,
std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred
>
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr copy_if_result<ranges::borrowed_iterator_t<R>, O>
copy_if( R&& r, O result, Pred pred, Proj proj = {} );
辅助类型定义如下:
template< class I, class O >
using copy_if_result = ranges::in_out_result<I, O>;
将 [first
; last
) 定义的范围内的元素复制到从 result
开始的另一个范围。
-
(1) 仅复制谓词
pred
返回true
的元素。复制的元素的相对顺序得到保留。未定义行为如果
result
在 [first
;last
) 范围内,则行为未定义。 -
(2) 与 (1) 相同,但使用
r
作为源范围,如同使用ranges::begin(r)
作为first
和ranges::end(r)
作为last
。
本页描述的函数类实体是niebloids。
参数
first last | 要复制的元素范围。 |
r | 要复制的元素范围。 |
result | 目标范围的开头。 |
pred | 应用于投影元素的谓词。 |
proj | 应用于元素的投影。 |
返回值
一个 ranges::in_out_result
,包含一个等于 last
的输入迭代器和一个在最后一个复制元素之后的输出迭代器。
复杂度
恰好 last - first
次赋值。
恰好 last - first
次谓词和投影应用,介于 0
和 last - first
次赋值之间(每个谓词返回 true
的元素进行一次赋值)。
异常
(无)
可能的实现
copy_if(1) 和 copy_if(2)
struct copy_if_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
requires std::indirectly_copyable<I, O>
constexpr ranges::copy_if_result<I, O>
operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
{
for (; first != last; ++first)
if (std::invoke(pred, std::invoke(proj, *first)))
{
*result = *first;
++result;
}
return {std::move(first), std::move(result)};
}
template<ranges::input_range R, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr ranges::copy_if_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r),
std::move(result),
std::ref(pred), std::ref(proj));
}
};
inline constexpr copy_if_fn copy_if;
备注
实际上,ranges::copy_if
的实现会避免多次赋值,并且如果值类型是 TriviallyCopyable
并且迭代器类型满足 contiguous_iterator
,则使用诸如 std::memmove
等批量复制函数。
示例
以下代码使用 ranges::copy_if
将一个 std::vector
的内容复制到另一个 std::vector
,并显示生成的 std::vector
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
std::vector<int> source(10);
std::iota(source.begin(), source.end(), 0);
std::vector<int> destination;
std::ranges::copy_if(source.begin(), source.end(),
std::back_inserter(destination));
// or, alternatively,
// std::vector<int> destination(source.size());
// std::ranges::copy_if(source.begin(), source.end(), destination.begin());
// either way is equivalent to
// std::vector<int> destination = source;
std::cout << "destination contains: ";
std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
std::cout << "odd numbers in destination are: ";
std::ranges::copy_if_if(destination, std::ostream_iterator<int>(std::cout, " "),
[](int x) { return (x % 2) == 1; });
std::cout << '\n';
}
destination contains: 0 1 2 3 4 5 6 7 8 9
odd numbers in destination are: 1 3 5 7 9