std::ranges...ansform_view(3)
π NAME π
okay so bestie, this is the <pink>std::ranges::transform_view::transform_view</pink> page from the C++ Standard Libary β yes, the header really says βLibary,β and the page title is std::ranges...ansform_view(3). Weβre doing constructors today, slay. π
β¨
β¨ Synopsis β¨
Constructor (1) β (since C++20):
transform_view() requires std::default_initializable<V> &&
std::default_initializable<F> = default;
Constructor (2) β (since C++20):
constexpr explicit transform_view( V base, F fun );
Okay so this constructs a transform_view:
- Default constructor. Value-initializes the underlying view and the transformation function.
- Move constructs the underlying view from base and the transformation function from fun.
π Parameters π
baseβ viewfunβ transformation function
π¦ Example π¦
Demonstrates Ο approximation using serial expansion of arc tangent of 1:
atan(1) = Ο/4 ~ 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...
Run this code:
#include <iomanip>
#include <iostream>
#include <numbers>
#include <numeric>
#include <ranges>
int main()
{
std::cout << std::setprecision(15) << std::fixed;
auto atan1term = std::ranges::views::transform(
[](int n) { return ((n % 2) ? -1 : 1) * 1.0 / (2 * n + 1); }
);
for (const int iterations : {1, 2, 3, 4, 5, 10, 100, 1000, 1'000'000})
{
auto seq = std::ranges::views::iota(0, iterations) | atan1term;
const double accum = std::accumulate(seq.begin(), seq.end(), 0.0);
std::cout << "Ο ~ " << 4 * accum << " (iterations: " << iterations << ")\n";
}
std::cout << "Ο ~ " << std::numbers::pi << " (std::numbers::pi)\n";
}
π Possible output π
Ο ~ 4.000000000000000 (iterations: 1)
Ο ~ 2.666666666666667 (iterations: 2)
Ο ~ 3.466666666666667 (iterations: 3)
Ο ~ 2.895238095238096 (iterations: 4)
Ο ~ 3.339682539682540 (iterations: 5)
Ο ~ 3.041839618929403 (iterations: 10)
Ο ~ 3.131592903558554 (iterations: 100)
Ο ~ 3.140592653839794 (iterations: 1000)
Ο ~ 3.141591653589774 (iterations: 1000000)
Ο ~ 3.141592653589793 (std::numbers::pi)
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3714(P2711R1) β applied to C++20: as published, the multi-parameter constructor was not explicit; correct behavior is made explicit.P2325R3β applied to C++20: as published, the default constructor constructs a transform_view which does not contain an F; correct behavior is if F is not default_initializable, the transform_view is also not default_initializable.
okay thatβs the whole page, bestie π«Άπ β source: http://cppreference.com, version/date: 2024.06.10, footer title: std::ranges...ansform_view(3). byeeee β¨π
π¦ 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