std::transf...clusive_scan(3)
π std::transform_inclusive_scan π
okay so bestie, welcome to the C++ Standard Library page for std::transform_inclusive_scan β the manpage header calls it std::transf...clusive_scan(3) and says C++ Standard Libary (their typo, not mine π
). This is the inclusive-scan-with-a-transform-glow-up situation, and we're gonna get through every single overload together. It lives in header <numeric>, arrived since C++17, and overloads (1) and (3) are constexpr since C++20. periodt. β¨
π NAME π
std::transform_inclusive_scan β std::transform_inclusive_scan. Yes, the NAME line is literally that, twice, and it ate. π
β¨ SYNOPSIS β¨
Defined in header <numeric>.
template< class InputIt, class OutputIt,
class BinaryOp, class UnaryOp > (since C++17)
OutputIt transform_inclusive_scan (1)
(constexpr since C++20)
( InputIt first, InputIt last, OutputIt d_first,
BinaryOp binary_op, UnaryOp unary_op );
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class BinaryOp, class UnaryOp >
ForwardIt2 transform_inclusive_scan (2) (since C++17)
( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
BinaryOp binary_op, UnaryOp unary_op );
template< class InputIt, class OutputIt,
class BinaryOp, class UnaryOp, class T > (since C++17)
OutputIt transform_inclusive_scan (3)
(constexpr since C++20)
( InputIt first, InputIt last, OutputIt d_first,
BinaryOp binary_op, UnaryOp unary_op, T init );
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class BinaryOp, class UnaryOp, class T >
ForwardIt2 transform_inclusive_scan (4) (since C++17)
( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
BinaryOp binary_op, UnaryOp unary_op, T init );
- Computes the inclusive prefix sum using
op. For each integeriin[0, std::distance(first, last)), performs the following operations in order: - Creates a sequence which is formed by the values transformed from the elements of
[first, iter]in order byunary_op, whereiteris the nextith iterator offirst. - Computes the generalized noncommutative sum of the sequence over
binary_op. - Assigns the result to
*dest, wheredestis the nextith iterator ofd_first. - Same as (1), but each sequence created is formed by
initfollowed by the elements of[first, iter]in order.
2,4) Same as (1,3), but executed according to policy.
These overloads participate in overload resolution only if:
- until C++20:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>is true. - since C++20:
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>is true.
The generalized noncommutative sum of a sequence of elements over a binary operation binary_op is defined as follows:
- If the sequence only has one element, the sum is the value of the element.
- Otherwise, performs the following operations in order:
- Selects any two adjacent elements
elem1andelem2from the sequence. - Calculates
binary_op(elem1, elem2)and replaces the two elements in the sequence with the result. - Repeats steps 1 and 2 until there is only one element in the sequence.
The result is non-deterministic if the binary_op is not associative (such as floating-point addition). π«
For overloads (1,2), if binary_op(unary_op(*first), unary_op(*first)) is not convertible to the value type of decltype(first), the program is ill-formed.
For overloads (3,4), if any of the following values is not convertible to T, the program is ill-formed:
binary_op(init, init)binary_op(init, unary_op(*first))binary_op(unary_op(*first), unary_op(*first))
If any of the following conditions is satisfied, the behavior is undefined:
- For overloads (1,2), the value type of
decltype(first)is notMoveConstructible. - For overloads (3,4),
Tis notMoveConstructible. unary_oporbinary_opmodifies any element of[first, last).unary_oporbinary_opinvalidates any iterator or subrange of[first, last).
π«Ά PARAMETERS π«Ά
first,lastβ the range of elements to sum.d_firstβ the beginning of the destination range; may be equal tofirst.policyβ the execution policy to use. See execution policy for details.initβ the initial value.unary_opβ unaryFunctionObjectthat will be applied to each element of the input range. The return type must be acceptable as input tobinary_op.binary_opβ binaryFunctionObjectthat will be applied to the result ofunary_op, the results of otherbinary_op, andinitif provided.
π TYPE REQUIREMENTS π
InputItmust meet the requirements ofLegacyInputIterator.OutputItmust meet the requirements ofLegacyOutputIterator.ForwardIt1,ForwardIt2must meet the requirements ofLegacyForwardIterator.
π¦ RETURN VALUE π¦
Iterator to the element past the last element written. slay. β¨
πΈ COMPLEXITY πΈ
Given N as std::distance(first, last):
- 1-4)
O(N)applications ofunary_opandbinary_oprespectively.
π EXCEPTIONS π
The overloads with a template parameter named ExecutionPolicy report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π NOTES π
unary_opis never applied toinit.- The parameter
initappears last, differing fromstd::transform_exclusive_scan, because it is optional for this function.
π§ EXAMPLE π§
// Run this code
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
std::vector data{3, 1, 4, 1, 5, 9, 2, 6};
auto times_10 = [](int x) { return x * 10; };
std::cout << "10 times exclusive sum: ";
std::transform_exclusive_scan(data.begin(), data.end(),
std::ostream_iterator<int>(std::cout, " "),
0, std::plus<int>{}, times_10);
std::cout << "\n10 times inclusive sum: ";
std::transform_inclusive_scan(data.begin(), data.end(),
std::ostream_iterator<int>(std::cout, " "),
std::plus<int>{}, times_10);
std::cout << '\n';
}
Output:
10 times exclusive sum: 0 30 40 80 90 140 230 250
10 times inclusive sum: 30 40 80 90 140 230 250 310
π SEE ALSO π
partial_sumβ computes the partial sum of a range of elements (function template).transformβ applies a function to a range of elements, storing results in a destination range (function template).inclusive_scan(C++17) β similar tostd::partial_sum, includes thei^thinput element in thei^thsum (function template).transform_exclusive_scan(C++17) β applies an invocable, then calculates exclusive scan (function template).
okay bestie, that's the whole page π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::transf...clusive_scan(3) Β· mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms