std::ranges...ansform_view(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

std::ranges...ansform_view(3)

okay bestie, welcome to the glowing C++ Standard Libary page for std::ranges...ansform_view(3) β€” yes, that spelling of β€œLibary” is staying, I don’t make the rules πŸ’…. This is the whole zip-transform situation, and it’s lowkey iconic.


πŸ’– NAME πŸ’–

std::ranges::views::zip_transform,std::ranges::zip_transform_view β€” and the description is literally the same string again: std::ranges::views::zip_transform,std::ranges::zip_transform_view.


✨ Synopsis ✨

Defined in header <ranges>:

template< std::move_constructible F, ranges::input_range... Views
>

    requires (ranges::view<Views> && ...) && (sizeof...(Views) >
0) &&
              std::is_object_v<F> && std::regular_invocable<
                  F&, ranges::range_reference_t<Views>...> &&
(1) (since C++23)
              /*can-reference*/<std::invoke_result_t<
                  F&, ranges::range_reference_t<Views>...>>
class zip_transform_view

     : public ranges::view_interface<zip_transform_view<F,
Views...>>
namespace views {

    inline constexpr /*unspecified*/ zip_transform =
(2) (since C++23)
/*unspecified*/;

}

Call signature:

template< class F, ranges::viewable_range... Rs >

    requires /* see below */
(since C++23)

constexpr auto zip_transform( F&& f, Rs&&... rs );
  1. zip_transform_view is a range adaptor that takes an invocable object and one or more views, and produces a view whose i^th element is the result of applying the invocable object to the i^th elements of all views. A type T models the exposition-only concept /*can-reference*/ if and only if T& is a valid type.
  2. views::zip_transform is a customization point object.

When calling with one argument f, let FD be std::decay_t<decltype(f)>, if:

  • FD models copy_constructible,
  • FD& models regular_invocable, and
  • std::invoke_result_t<FD&> is an object type,

then views::zip_transform(f) is expression-equivalent to ((void)f, auto(views::empty<std::decay_t<std::invoke_result_t<FD&>>>)). Otherwise, the call to views::zip_transform is ill-formed.

When calling with more than one arguments f and rs..., views::zip_transform(f, rs...) is expression-equivalent to ranges::zip_transform_view(f, rs...).

zip_transform_view models the concepts random_access_range, bidirectional_range, forward_range, input_range, common_range, and sized_range when the underlying ranges::zip_view<Views...> models respective concepts.


πŸŽ€ Member functions πŸŽ€

  • constructor β€” constructs a zip_transform_view. (C++23) (public member function)
  • begin β€” returns an iterator to the beginning. (C++23) (public member function)
  • end β€” returns an iterator or a sentinel to the end. (C++23) (public member function)
  • size β€” returns the number of elements. Provided only if each underlying (adapted) range satisfies sized_range. (C++23) (public member function)

🌸 Inherited from std::ranges::view_interface 🌸

  • empty β€” returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range. (C++20) (public member function of std::ranges::view_interface<D>)
  • cbegin β€” returns a constant iterator to the beginning of the range. (C++23) (public member function of std::ranges::view_interface<D>)
  • cend β€” returns a sentinel for the constant iterator of the range. (C++23) (public member function of std::ranges::view_interface<D>)
  • operator bool β€” returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (C++20) (public member function of std::ranges::view_interface<D>)
  • front β€” returns the first element in the derived view. Provided if it satisfies forward_range. (C++20) (public member function of std::ranges::view_interface<D>)
  • back β€” returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range. (C++20) (public member function of std::ranges::view_interface<D>)
  • operator[] β€” returns the n^th element in the derived view. Provided if it satisfies random_access_range. (C++20) (public member function of std::ranges::view_interface<D>)

🧚 Deduction guides 🧚

The page just drops the heading Deduction guides here β€” no extra details listed, bestie.


πŸ¦‹ Member types πŸ¦‹

  • InnerView (private) β€” ranges::zip_view<Views...>. (exposition-only member type*)
  • ziperator (private) β€” ranges::iterator_t<const InnerView> if Const is true, otherwise ranges::iterator_t<InnerView>. (exposition-only member type*)
  • zentinel (private) β€” ranges::sentinel_t<const InnerView> if Const is true, otherwise ranges::sentinel_t<InnerView>. (exposition-only member type*)

🌈 Data members 🌈

  • zip_ (private) β€” An underlying view object of type InnerView. (exposition-only member object*)
  • fun_ (private) β€” A wrapped invocable object of type movable-box<F>. (exposition-only member object*)

⭐ Nested classes ⭐

  • iterator β€” the iterator type. (C++23) (exposition-only member class template*)
  • sentinel β€” the sentinel type used when the underlying zip_view is not a common_range. (C++23) (exposition-only member class template*)

πŸ’œ Notes πŸ’œ

Feature-test macro:

  • __cpp_lib_ranges_zip β€” value 202110L, std (C++23), feature: std::ranges::zip_view, std::ranges::zip_transform_view, std::ranges::adjacent_view, std::ranges::adjacent_transform_view.

πŸ“ Example πŸ“

// Run this code

#include <array>
#include <iostream>
#include <list>
#include <ranges>
#include <vector>

void print(auto const rem, auto const& r)
{
    std::cout << rem << '{';
    for (char o[]{0,' ',0}; auto const& e : r)
        std::cout << o << e, *o = ',';
    std::cout << "}\n";
}

int main()
{
    auto v1 = std::vector<float>{1, 2, 3};
    auto v2 = std::list<short>{1, 2, 3, 4};
    auto v3 = std::to_array({1, 2, 3, 4, 5});

    auto add = [](auto a, auto b, auto c) { return a + b + c; };

    auto sum = std::views::zip_transform(add, v1, v2, v3);

    print("v1:  ", v1);
    print("v2:  ", v2);
    print("v3:  ", v3);
    print("sum: ", sum);
}

πŸŽ€ Output πŸŽ€

v1:  {1, 2, 3}
v2:  {1, 2, 3, 4}
v3:  {1, 2, 3, 4, 5}
sum: {3, 6, 9}

πŸ”₯ See also πŸ”₯

  • ranges::zip_view, views::zip β€” a view consisting of tuples of references to corresponding elements of the adapted views. (C++23) (class template) (customization point object)
  • ranges::transform_view, views::transform β€” a view of a sequence that applies a transformation function to each element. (C++20) (class template) (range adaptor object)
  • ranges::elements_view, views::elements β€” takes a view consisting of tuple-like values and a number N and produces a view of N^th element of each tuple. (C++20) (class template) (range adaptor object)

πŸ’– source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::ranges...ansform_view(3) πŸ’–

πŸ“¦ where this page comes from