std::ranges...sform_view,(3)
π std::ranges...sform_view,(3) π
hey bestie, welcome to the std::ranges...sform_view,(3) page from the C++ Standard Libary manual π β yes, that manual name is spelled exactly like that, and yes, we're keeping it.
β¨ NAME β¨
bestie the page name is literally:
std::ranges::views::adjacent_transform,std::ranges::adjacent_transform_view,- and then the raw page repeats it:
std::ranges::views::adjacent_transform,std::ranges::adjacent_transform_view,
there's also a little - line sitting between those repeats in the raw page. iconic formatting, no notes π
β¨ Synopsis β¨
(1) is (since C++23) and it is giving class template energy:
Defined in header <ranges>
template< ranges::forward_range V, std::move_constructible F,
std::size_t N >
requires ranges::view<V> && (N > 0) && std::is_object_v<F> &&
std::regular_invocable<F&,
/*REPEAT*/(ranges::range_reference_t<V>, N)...> &&
/*can-reference*/<std::invoke_result_t<F&,
/*REPEAT*/(ranges::range_reference_t<V>, N)...>>
class adjacent_transform_view
: public ranges::view_interface<adjacent_transform_view<V, F, N>>
(2) is also (since C++23):
namespace views {
template< std::size_t N >
inline constexpr /* unspecified */ adjacent_transform = /* unspecified */;
}
(3) is also (since C++23):
namespace views {
inline constexpr auto pairwise_transform = adjacent_transform<2>;
}
π Call signature π
and here's the call signature, (since C++23) bestie:
template< ranges::viewable_range R, class F >
requires /* see below */
constexpr ranges::view auto adjacent_transform<N>( R&& r, F&& fun );
template< class F >
constexpr /*range adaptor closure*/ adjacent_transform<N>( F&& fun );
adjacent_transform_viewis a range adaptor that takes a view and an invocable objectfun, and produces a view whosei^thelement is a value that is the result of applyingfunto each element in[i, i + N)of the original view.Falways has arityN.
Let S be the size of the original view. Then the size of the produced view is:
S - N + 1, ifS >= N,0otherwise, and the resulting view is empty.
- The name
views::adjacent_transform<N>denotes aRangeAdaptorObject. Given subexpressionseandf, and a constant expressionN, the expressionviews::adjacent_transform<N>(e, f)is expression-equivalent to:((void)e, views::zip_transform(f)), ifNis equal to0(except that the evaluations ofeandfare indeterminately sequenced),adjacent_transform_view<views::all_t<decltype((e))>, std::decay_t<decltype((f))>, N>(e, f)otherwise.
- The name
views::pairwise_transformdenotes aRangeAdaptorObjectthat behaves exactly asviews::adjacent_transform<2>. In particular, the arity ofFis also2andfunis a binary invocable object.
adjacent_transform_view always models forward_range, and models bidirectional_range, random_access_range, or sized_range, if the adapted view type models the corresponding concept. periodt π
πΈ Member functions πΈ
constructor(C++23)β constructs aadjacent_transform_view(public member function)begin(C++23)β returns an iterator to the beginning (public member function)end(C++23)β returns an iterator or a sentinel to the end (public member function)size(C++23)β returns the number of elements. Provided only if the underlying (adapted) range satisfiessized_range. (public member function)
π«Ά Inherited from std::ranges::view_interface π«Ά
empty(C++20)β returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (public member function ofstd::ranges::view_interface<D>)cbegin(C++23)β returns a constant iterator to the beginning of the range. (public member function ofstd::ranges::view_interface<D>)cend(C++23)β returns a sentinel for the constant iterator of the range. (public member function ofstd::ranges::view_interface<D>)operator bool(C++20)β returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (public member function ofstd::ranges::view_interface<D>)front(C++20)β returns the first element in the derived view. Provided if it satisfiesforward_range. (public member function ofstd::ranges::view_interface<D>)back(C++20)β returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (public member function ofstd::ranges::view_interface<D>)operator[](C++20)β returns then^thelement in the derived view. Provided if it satisfiesrandom_access_range. (public member function ofstd::ranges::view_interface<D>)
π Deduction guides π
(none) β not a single one, bestie. truly minimal π
π¦ Member types π¦
InnerView(private) βranges::adjacent_view<V, N>.(exposition-only member type*)inner_iterator(private) βranges::iterator_t<const InnerView>, ifConstistrue; otherwise,ranges::iterator_t<InnerView>.(exposition-only member type*)inner_sentinel(private) βranges::sentinel_t<const InnerView>, ifConstistrue; otherwise,ranges::sentinel_t<InnerView>.(exposition-only member type*)
π Data members π
fun_(private) β/*movable-box*/<F>(exposition-only member object*)inner_(private) βranges::adjacent_view<V,N>(exposition-only member object*)
π§ Nested classes π§
iterator(C++23)β the iterator type(exposition-only member class template*)sentinel(C++23)β the sentinel type used whenadjacent_transform_viewis not acommon_range(exposition-only member class template*)
β Notes β
feature-test macro girlies, assemble:
__cpp_lib_ranges_zipβ value202110Lβ 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 <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 π
6 9 12 15
π References π
- C++23 standard (ISO/IEC 14882:2023):
- 26.7.27 Adjacent transform view
[range.adjacent.transform]
π See also π
ranges::adjacent_view,views::adjacent(C++23)β a view consisting of tuples of references to adjacent elements of the adapted view (class template) (range adaptor object)ranges::transform_view,views::transform(C++20)β a view of a sequence that applies a transformation function to each element (class template) (range adaptor object)ranges::zip_transform_view,views::zip_transform(C++23)β a view consisting of tuples of results of application of a transformation function to corresponding elements of the adapted views (class template) (customization point object)ranges::transform(C++20)β applies a function to a range of elements (niebloid)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::ranges...sform_view,(3) π«Άβ¨
π¦ 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