Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::ranges...ansform_view(3) C++ Standard Libarystd::ranges...ansform_view(3)

NAME
       std::ranges::transform_view::transform_view    -	   std::ranges::trans-
       form_view::transform_view

Synopsis
	  transform_view()    requires	  std::default_initializable<V>	    &&
       (1) (since C++20)
				    std::default_initializable<F> = default;
	  constexpr    explicit	   transform_view(    V	  base,	  F   fun   );
       (2) (since C++20)

	  Constructs a transform_view.

	  1) Default constructor. Value-initializes the	 underlying  view  and
       the transformation
	  function.
	  2) Move constructs the underlying view from base and the transforma-
       tion function
	  from fun.

Parameters
	  base - view
	  fun  - 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: " <<	itera-
       tions <<	")\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 retroac-
       tively to
	  previously published C++ standards.

	     DR	     Applied to	       Behavior	as published		  Cor-
       rect behavior
	  LWG 3714  C++20      the multi-parameter constructor was   made  ex-
       plicit
	  (P2711R1)	       not explicit
			       if F is not default_initializable,
	  P2325R3   C++20      the default constructor		    the	trans-
       form_view is also
			       constructs  a  transform_view  which    not de-
       fault_initializable
			       does not	contain	an F

http://cppreference.com		  2024.06.10	 std::ranges...ansform_view(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::ranges::transform_view::transform_view&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help