std::ranges::fold_left_first
定义于头文件 <algorithm> |
||
调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S, /*indirectly-binary-left-foldable*/<std::iter_value_t<I>, I> F > |
(1) | (自 C++23 起) |
template< ranges::input_range R, /*indirectly-binary-left-foldable*/< |
(2) | (自 C++23 起) |
辅助概念 |
||
template< class F, class T, class I > concept /*indirectly-binary-left-foldable*/ = /* see description */; |
(3) | (仅为阐述*) |
左折叠给定范围的元素,即返回链式表达式的求值结果f(f(f(f(x1, x2), x3), ...), xn)
,其中 x1
, x2
, ..., xn
是范围的元素。
非正式地,ranges::fold_left_first
的行为类似于 std::accumulate 的接受二元谓词的重载,不同之处在于 *first 在内部用作初始元素。
如果 [
first,
last)
不是有效范围,则行为未定义。
[
first,
last)
。 等价于 return ranges::fold_left_first_with_iter(std::move(first), last, f).value。 辅助概念 |
||
template< class F, class T, class I, class U > concept /*indirectly-binary-left-foldable-impl*/ = |
(3A) | (仅为阐述*) |
template< class F, class T, class I > concept /*indirectly-binary-left-foldable*/ = |
(3B) | (仅为阐述*) |
此页面上描述的类似函数的实体是 算法函数对象(非正式地称为 niebloids),即
目录 |
[编辑] 参数
first, last | - | 定义要折叠元素范围的迭代器-哨位对 |
r | - | 要折叠的元素范围 |
f | - | 二元函数对象 |
[编辑] 返回值
类型为 std::optional<U> 的对象,包含给定范围在 f 上的左折叠结果,其中 U 等价于 decltype(ranges::fold_left(std::move(first), last, std::iter_value_t<I>(*first), f))。
如果范围为空,则返回 std::optional<U>()。
[编辑] 可能的实现
struct fold_left_first_fn { template<std::input_iterator I, std::sentinel_for<I> S, /*indirectly-binary-left-foldable*/<std::iter_value_t<I>, I> F> requires std::constructible_from<std::iter_value_t<I>, std::iter_reference_t<I>> constexpr auto operator()(I first, S last, F f) const { using U = decltype( ranges::fold_left(std::move(first), last, std::iter_value_t<I>(*first), f) ); if (first == last) return std::optional<U>(); std::optional<U> init(std::in_place, *first); for (++first; first != last; ++first) *init = std::invoke(f, std::move(*init), *first); return std::move(init); } template<ranges::input_range R, /*indirectly-binary-left-foldable*/< ranges::range_value_t<R>, ranges::iterator_t<R>> F> requires std::constructible_from<ranges::range_value_t<R>, ranges::range_reference_t<R>> constexpr auto operator()(R&& r, F f) const { return (*this)(ranges::begin(r), ranges::end(r), std::ref(f)); } }; inline constexpr fold_left_first_fn fold_left_first; |
[编辑] 复杂度
恰好 ranges::distance(first, last) - 1 次(假设范围非空)函数对象 f 的应用。
[编辑] 注解
下表比较了所有约束折叠算法
折叠函数模板 | 起始于 | 初始值 | 返回类型 |
---|---|---|---|
ranges::fold_left | 左 | init | U |
ranges::fold_left_first | 左 | 首元素 | std::optional<U> |
ranges::fold_right | 右 | init | U |
ranges::fold_right_last | 右 | 尾元素 | std::optional<U> |
ranges::fold_left_with_iter | 左 | init |
(1) ranges::in_value_result<I, U> (2) ranges::in_value_result<BR, U>, 其中 BR 是 ranges::borrowed_iterator_t<R> |
ranges::fold_left_first_with_iter | 左 | 首元素 |
(1) ranges::in_value_result<I, std::optional<U>> (2) ranges::in_value_result<BR, std::optional<U>> 其中 BR 是 ranges::borrowed_iterator_t<R> |
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_ranges_fold |
202207L |
(C++23) | std::ranges 折叠算法 |
[编辑] 示例
#include <algorithm> #include <array> #include <functional> #include <ranges> #include <utility> int main() { constexpr std::array v{1, 2, 3, 4, 5, 6, 7, 8}; static_assert ( *std::ranges::fold_left_first(v.begin(), v.end(), std::plus{}) == 36 && *std::ranges::fold_left_first(v, std::multiplies{}) == 40320 ); constexpr std::array w { 1, 2, 3, 4, 13, 1, 2, 3, 4, 13, 1, 2, 3, 4, 13, 1, 2, 3, 4, }; static_assert ( "Find the only value that (by precondition) occurs odd number of times:" && *std::ranges::fold_left_first(w, [](int p, int q){ return p ^ q; }) == 13 ); constexpr auto pairs = std::to_array<std::pair<char, float>> ({ {'A', 3.0f}, {'B', 3.5f}, {'C', 4.0f} }); static_assert ( "Get the product of all pair::second in pairs:" && *std::ranges::fold_left_first ( pairs | std::ranges::views::values, std::multiplies{} ) == 42 ); }
[编辑] 参考
- C++23 标准 (ISO/IEC 14882:2024)
- 27.6.18 折叠 [alg.fold]
[编辑] 参见
(C++23) |
左折叠一个元素范围 (算法函数对象) |
(C++23) |
右折叠一个元素范围 (算法函数对象) |
(C++23) |
右折叠一个元素范围,使用尾元素作为初始值 (算法函数对象) |
(C++23) |
左折叠一个元素范围,并返回一个 pair (迭代器, 值) (算法函数对象) |
左折叠一个元素范围,使用首元素作为初始值,并返回一个 pair (迭代器, optional) (算法函数对象) | |
对一个元素范围求和或折叠 (函数模板) | |
(C++17) |
类似于 std::accumulate,但顺序不定 (函数模板) |