A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/ranges/adjacent_transform_view.html below:

std::ranges::views::adjacent_transform, std::ranges::adjacent_transform_view, std::ranges::views::pairwise_transform - cppreference.com

std::ranges::adjacent_transform_view (1) (since C++23) namespace views {

    template< std::size_t N >
    constexpr /* unspecified */ adjacent_transform = /* unspecified */;

}
(2) (since C++23) namespace views {

    inline constexpr auto pairwise_transform = adjacent_transform<2>;

}
(3) (since C++23)

Call signature

(since C++23)

template< class F >
constexpr /*range adaptor closure*/ adjacent_transform<N>( F&& fun );

(since C++23) 1) adjacent_transform_view

is a range adaptor that takes a

view

and an invocable object

fun

, and produces a

view

whose

ith

element is a value that is the result of applying

fun

to each element in

[ii + N)

of the original view.

F

always has

arity N

.

Let

S

be the size of the original view. Then the size of produced view is:

3)

The name

views::pairwise_transform

denotes a

RangeAdaptorObject

that behaves exactly as

views::adjacent_transform<2>

. In particular, the arity of

F

is also

2

and

fun

is a binary invocable object.

adjacent_transform_view always models forward_range, and models bidirectional_range, random_access_range, or sized_range, if adapted view type models the corresponding concept.

[edit] Member functions constructs a adjacent_transform_view
(public member function) [edit] returns an iterator to the beginning
(public member function) [edit] returns an iterator or a sentinel to the end
(public member function) [edit] returns the number of elements, provided only if the underlying (adapted) range satisfies sized_range
(public member function) [edit] returns the approximate size of the resulting approximately_sized_range
(public member function) [edit] Inherited from std::ranges::view_interface returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range
(public member function of std::ranges::view_interface<D>) [edit] returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>) [edit] returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
(public member function of std::ranges::view_interface<D>) [edit] returns the first element in the derived view, provided if it satisfies forward_range
(public member function of std::ranges::view_interface<D>) [edit] returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range
(public member function of std::ranges::view_interface<D>) [edit] returns the nth element in the derived view, provided only if it satisfies random_access_range
(public member function of std::ranges::view_interface<D>) [edit] [edit] Nested types [edit] Data members Member Description /*movable-box*/<F> fun_ (private) the transforming invocable object
(exposition-only member object*) ranges::adjacent_view<V,N> inner_ (private) the stored view
(exposition-only member object*) [edit] Nested classes the iterator type
(exposition-only member class template*) the sentinel type used when adjacent_transform_view is not a common_range
(exposition-only member class template*) [edit] Notes

views::adjacent_transform only accepts foward ranges even when N is 0.

[edit] Example
#include <array>
#include <iostream>
#include <ranges>
 
int main()
{
    constexpr static std::array data{1, 2, 3, 4, 5, 6};
    constexpr int window{3};
 
    auto Fun = [](auto... ints) { return (... + ints); };
    // Alternatively, the Fun could be any ternary (if window == 3) callable, e.g.:
    // auto Fun = [](int x, int y, int z) { return x + y + z; };
 
    constexpr auto view = data | std::views::adjacent_transform<window>(Fun);
 
    static_assert(
        view.size() == (data.size() - window + 1)
        && std::array{6, 9, 12, 15}
        == std::array{view[0], view[1], view[2], view[3]}
        && view[0] == Fun(data[0], data[1], data[2])
        && view[1] == Fun(data[1], data[2], data[3])
        && view[2] == Fun(data[2], data[3], data[4])
        && view[3] == Fun(data[3], data[4], data[5])
    );
 
    for (int x : view)
        std::cout << x << ' ';
    std::cout << '\n';
}

Output:

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior LWG 4098 C++23 views::adjacent_transform<0> used to accept input-only ranges made rejected [edit] References
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4