std::valarray::apply(3)
π std::valarray::apply β the C++ Standard Library glow-up π
okay so bestie, this is the <std::valarray::apply(3)> page from the C++ Standard Library, version 2024.06.10 β and we're about to make it make sense, no cap. β¨
πΈ NAME πΈ
std::valarray::apply β literally just std::valarray::apply, she's iconic like that π
π¦ SYNOPSIS π¦
valarray<T> apply( T func(T) ) const;
valarray<T> apply( T func(const T&) ) const;
Returns a brand-new valarray, same size as the original, where every value has been acquired by applying the function func to the previous values of the elements. it's giving transformation era, fr fr π₯
π PARAMETERS π
funcβ the function you're applying to the values. that's the whole vibe check right there π«Ά
β RETURN VALUE β
The resulting valarray, with values acquired by applying the function func. simple as that, no notes π
π NOTES π
okay so here's where it gets a little spicy β the function can be implemented with a return type that's different from std::valarray. not me explaining the plot twist π
In that case, the replacement type has the following properties (yes, all of them, we don't skip):
- All const member functions of
std::valarrayare provided. std::valarray,std::slice_array,std::gslice_array,std::mask_arrayandstd::indirect_arraycan be constructed from the replacement type.- For every function taking a
const std::valarray<T>&exceptbegin()andend()(since C++11), identical functions taking the replacement types shall be added; - For every function taking two
const std::valarray<T>&arguments, identical functions taking every combination ofconst std::valarray<T>&and replacement types shall be added. - The return type does not add more than two levels of template nesting over the most deeply-nested argument type.
bestie, if you're implementing a replacement type, these bullet points are not optional. read them twice. π
π₯ Possible implementation π₯
Following straightforward implementations can be replaced by expression templates for a higher efficiency β iykyk, that's the efficiency glow-up β¨
template<class T>
valarray<T> valarray<T>::apply(T func(T)) const
{
valarray<T> other = *this;
for (T& i : other)
i = func(i);
return other;
}
template<class T>
valarray<T> valarray<T>::apply(T func(const T&)) const
{
valarray<T> other = *this;
for (T& i : other)
i = func(i);
return other;
}
π Example π
Calculates and prints the first 10 factorials β and honestly? the numbers ate. π
// Run this code
#include <cmath>
#include <iostream>
#include <valarray>
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:
1 2 6 24 120 720 5040 40320 362880 3628800
look at those factorials go ππ₯
π See also π
for_eachβ applies a function to a range of elements (function template)ranges::for_each(C++20) β applies a function to a range of elements (niebloid)
Hidden category: π - Pages with unreviewed LWG DR marker
and that's the whole page, bestie β no skips, no summaries, every detail present and accounted for π«Άβ¨
this has been <std::valarray::apply(3)> from the C++ Standard Library, 2024.06.10 β go forth and apply your functions, ily, 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