Stdtransform Inclusive Scan In C++

In this article, we will discuss the std::transforminclusivescan function with its syntax, parameters, examples, and benefits.

What is the std::transform_inclusive_scan function?

With the exception of a unary function that is first applied to each input item, the transforminclusivescan function is an intrinsic C++ function that functions similarly to inclusive_scan.

Its functionality is to use unaryop to modify every element from the start to the last, and then binaryop with a specified range is used to compute an inclusive prefix sum operation. The i-th sum procedure includes an inclusive specified for the i-th input element.

Syntax:

It has the following syntax:

Example

template < class InputItrator, class OutputItrator,
          class BinaryOperation, class UnaryOperation >

OutputItrator transform_inclusive_scan( InputItrator first, 
                                        InputItrator last,
                                        OutputItrator d_first,
                                        BinaryOperation binary_op,
                                        UnaryOperation unary_op 
                                     );

Parameters:

  • First and last: The range of an items sum is defined by the beginning and last elements.
  • d_first: Here, the destination range starts.
  • Unary_op: Every element in the input range will undergo the specified operation.
  • Binary_op: This operation is to be applied to the outcomes of other binary and unary operations and check whether or not init(initial val) will be supplied.
  • Type requirements:

  • InputItrator: The Inputiterator class is one type of iterator that can read from the pointed-to element. Once it is increased, the validity of the single-pass procedure is lost for all subsequent copies.
  • OutputItrator: An iterator that can be written to the indicated element is the OutputIterator.
  • Example:

Let us take an example to illustrate the std::transforminclusivescan method in C++.

Example

#include <iostream>
#include <vector>
using namespace std;
namespace jtpInputIterator {
template <class InputItrator, class OutputItrator,
class BinaryOperation, class UnaryOperation>
OutputItrator transform_inclusive_scan(InputItrator first,InputItrator last,OutputItrator d_first,BinaryOperation binary_op,UnaryOperation unary_op)
{
*d_first = unary_op(*first);
first++;
d_first++;
for (auto it = first; it != last; it++) {
*d_first = binary_op(unary_op(*it), *(d_first - 1));
d_first++;
}
return d_first;
}
}
int main()
{
vector<int> InputVector{ 11, 22, 33, 44, 55, 66, 77, 88 };
vector<int> OutputVector(8);
jtpInputIterator::transform_inclusive_scan(InputVector.begin(),
InputVector.end(), OutputVector.begin(),
[](auto xx, auto yy) {
return xx + yy;
},
[](auto xx) {
return xx * xx;
});
for (auto item : OutputVector) {
cout << item << " ";
}
cout << std::endl;
return 0;
}

Output:

Benefits of transform_inclusive_scan:

A function called std::transforminclusivescan was added to the header in C++17. It uses a binary associative operation defined by the given binary functor to execute a parallel inclusive scan on the input sequence. When a binary operation is performed on any element in the series, including the current element, an inclusive scan computes and stores the partial results in the output sequence.

The following are some advantages of C++'s std::transforminclusivescan:

  • Parallel Execution: The ability to use std::transforminclusivescan to carry out the operation in parallel is one of its main advantages. Using several cores or threads for computing might result in noticeable performance gains, particularly for large datasets.
  • Functional Programming Style: It enables us to describe the binary operation as a functor in a functional programming style. Keeping the operation and scanning logic separate encourages code clarity and reusability.
  • Flexibility: Any associative function can be used as the binary operation, allowing for a wide range of computations to be done. It covers both user-defined custom operations and standard arithmetic operations like addition and multiplication.
  • Inclusive Scan: Exclusive scan excludes the present element from the partial findings, whereas inclusive scan includes it. It is helpful for many algorithms that require us to collect values, including the one we are now using.
  • Combining Standard Library with Integration: Since std::transforminclusivescan is a component of the C++ standard library, it functions well with other standard library data structures and algorithms. By doing this, compatibility and interoperability with current codebases are guaranteed.
  • Performance Optimization: Standard library implementations of algorithms frequently include performance optimizations adapted to various hardware architectures and usage patterns. These optimizations can be utilized using std::transforminclusivescan, leading to effective execution.
  • Conclusion:

In conclusion, the std::transforminclusivescan function offers several advantages in terms of efficiency, flexibility, and code readability by delivering a simple and effective method for carrying out parallel inclusive scans on C++ sequences.

Input Required

This code uses input(). Please provide values below: