std::valarray::apply (3) Linux Manual Page
std::valarray<T>::apply – std::valarray<T>::apply
Synopsis
valarray<T> apply(T func(T)) const;
valarray<T> apply(T func(const T &)) const;
Returns a new valarray of the same size with values which are acquired by applying function func to the previous values of the elements.
Parameters
func – function to apply to the values
Return value
The resulting valarray with values acquired by applying function func.
Notes
The function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties:
Example
calculates and prints the first 10 factorials
// Run this code
#include <iostream>
#include <valarray>
#include <cmath>
int main()
{
std::valarray<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
v = v.apply([](int n) -> int {
return std::round(std::tgamma(n + 1));
});
for (auto n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
}
Output:
See also
for_each (function template)
