views::transform
C++23Apply a function to each element lazily.
Section hub
template< ranges::input_range First, ranges::forward_range... Vs >
requires (ranges::view<First> && ... && ranges::view<Vs>)
class cartesian_product_view
: public ranges::view_interface<cartesian_product_view<First, Vs...>>
(since C++23)
namespace views {
inline constexpr /*unspecified*/ cartesian_product = /*unspecified*/;
}
(since C++23)
Call signature
template< ranges::viewable_range... Rs >
requires /* see below */
constexpr ranges::view auto cartesian_product( Rs&&... rs );
(since C++23)
Helper concepts
template< bool Const, class First, class... Vs >
concept /*cartesian-product-is-random-access*/ =
(ranges::random_access_range</*maybe-const*/<Const, First>> && ... &&
(ranges::random_access_range</*maybe-const*/<Const, Vs>> &&
ranges::sized_range</*maybe-const*/<Const, Vs>>));
(exposition only*)
template< class R >
concept /*cartesian-product-common-arg*/ =
ranges::common_range<R> ||
(ranges::sized_range<R> && ranges::random_access_range<R>);
(exposition only*)
template< bool Const, class First, class... Vs >
concept /*cartesian-product-is-bidirectional*/ =
(ranges::bidirectional_range</*maybe-const*/<Const, First>> && ... &&
(ranges::bidirectional_range</*maybe-const*/<Const, Vs>> &&
/*cartesian-product-common-arg*/</*maybe-const*/<Const, Vs>>));
(exposition only*)
template< class First, class... Vs >
concept /*cartesian-product-is-common*/ =
/*cartesian-product-common-arg*/<First>;
(exposition only*)
template< class... Vs >
concept /*cartesian-product-is-sized*/ =
(ranges::sized_range<Vs> && ...);
(exposition only*)
template< bool Const, template<class> class FirstSent, class First, class... Vs >
concept /*cartesian-is-sized-sentinel*/ =
(std::sized_sentinel_for<FirstSent</*maybe-const*/<Const, First>>,
ranges::iterator_t</*maybe-const*/<Const, First>>> && ... &&
(ranges::sized_range</*maybe-const*/<Const, Vs>> &&
std::sized_sentinel_for<ranges::iterator_t<
/*maybe-const*/<Const, Vs>>,
ranges::iterator_t</*maybe-const*/<Const, Vs>>>));
(exposition only*)
Helper function templates
template< /*cartesian-product-common-arg*/ R >
constexpr auto /*cartesian-common-arg-end*/( R& r )
{
if constexpr (ranges::common_range<R>)
return ranges::end(r);
else
return ranges::begin(r) + ranges::distance(r);
}
(exposition only*)
Feature-test macro Value Std Feature __cpp_lib_ranges_cartesian_product 202207L (C++23) std::ranges::cartesian_product_view
#include <array>
#include <iostream>
#include <list>
#include <ranges>
#include <string>
#include <vector>
void print(std::tuple<char const&, int const&, std::string const&> t, int pos)
{
const auto& [a, b, c] = t;
std::cout << '(' << a << ' ' << b << ' ' << c << ')' << (pos % 4 ? " " : "\n");
}
int main()
{
const auto x = std::array{'A', 'B'};
const auto y = std::vector{1, 2, 3};
const auto z = std::list<std::string>{"α", "β", "γ", "δ"};
for (int i{1}; auto const& tuple : std::views::cartesian_product(x, y, z))
print(tuple, i++);
}
This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.
Start here for the adapters most people reach for first when building pipelines.
Apply a function to each element lazily.
Keep only elements that satisfy a predicate.
Keep the first N elements from a source range.
Skip the first N elements and expose the rest.
Split a range into non-overlapping fixed-size subranges.
Flatten a range of ranges into a single lazy sequence.
These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.
Normalize a range into a view-compatible form.
Adapt iterator/sentinel pairs into a common-range shape.
Wrap an existing range by reference.
Store and expose a range with unique ownership.
Package iterator + sentinel as a view-like object.
Project tuple-like elements to their key component.
Project tuple-like elements to their value component.
Newer adapters, kept as a compact scan list with only standard badges.
A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.