std::ranges...product_view(3)
πΈ NAME πΈ
std::ranges::views::cartesian_product, std::ranges::cartesian_product_view β okay bestie, this is the whole name, no cap π
This page is std::ranges...product_view(3) from the C++ Standard Libary manual, and we are about to make the cartesian product make sense. β¨
β¨ Synopsis β¨
Defined in header <ranges> π«Ά
template< ranges::input_range First, ranges::forward_range... Vs >
requires (ranges::view<First> && ... && ranges::view<Vs>) (1)
(since C++23)
class cartesian_product_view
: public ranges::view_interface<cartesian_product_view<First, Vs...>>
namespace views {
inline constexpr /*unspecified*/ cartesian_product = (2)
(since C++23)
/*unspecified*/;
}
Call signature
template< ranges::viewable_range... Rs >
requires /* see below */
(since C++23)
constexpr auto cartesian_product( Rs&&... rs );
Helper concepts
template< bool Const, class First, class... Vs >
concept __cartesian_product_is_random_access =
(ranges::random_access_range<__maybe_const<Const, First>> && (3)
(exposition
... &&
only*)
(ranges::random_access_range<__maybe_const<Const, Vs>> &&
ranges::sized_range<__maybe_const<Const, Vs>>));
template< class R >
concept __cartesian_product_common_arg =
(exposition
ranges::common_range<R> || (4)
only*)
(ranges::sized_range<R> &&
ranges::random_access_range<R>);
template< bool Const, class First, class... Vs >
concept __cartesian_product_is_bidirectional =
(ranges::bidirectional_range<__maybe_const<Const, First>> &&
(exposition
... && (5)
only*)
(ranges::bidirectional_range<__maybe_const<Const, Vs>> &&
__cartesian_product_common_arg<__maybe_const<Const, Vs>>));
template< class First, class... Vs >
(exposition
concept __cartesian_product_is_common = (6)
only*)
__cartesian_product_common_arg<First>;
template< class... Vs >
(exposition
concept __cartesian_product_is_sized = (7)
only*)
(ranges::sized_range<Vs> && ...);
template< bool Const, template<class> class FirstSent, class First, class... Vs >
concept __cartesian_is_sized_sentinel =
(ranges::sized_sentinel_for<FirstSent<__maybe_const<Const, First>>,
ranges::iterator_t<__maybe_const<Const, First>>> && ... (8)
(exposition
&&
only*)
(ranges::sized_range<__maybe_const<Const, Vs>> &&
ranges::sized_sentinel_for<iterator_t<__maybe_const<Const, Vs>>,
ranges::iterator_t<__maybe_const<Const, Vs>>>));
Helper function templates
template< __cartesian_product_common_arg R >
constexpr auto __cartesian_common_arg_end( R& r ) {
if constexpr (ranges::common_range<R>)
(exposition
return ranges::end(r); (9)
only*)
else
return ranges::begin(r) + ranges::distance(r);
}
So hereβs the tea, item by item π
cartesian_product_viewis a range adaptor that takes n views, wheren > 0, and produces a view of tuples calculated by the n-ary cartesian product of the provided ranges. The size of produced view is a multiple of sizes of provided ranges, while each element is a tuple (of references) of the size n.views::cartesian_productis a customization point object.- When calling with no argument,
views::cartesian_product()is expression-equivalent toviews::single(std::tuple()). - Otherwise,
views::cartesian_product(rs...)is expression-equivalent toranges::cartesian_product_view<views::all_t<decltype((rs))>...>(rs...).
- When calling with no argument,
- Determines if
cartesian_productis a random access range (see alsorandom_access_range). - Determines if
cartesian_productis a common range (see alsocommon_range). - Determines if
cartesian_productis a bidirectional range (see alsobidirectional_range). - Determines if
cartesian_productsatisfies the helper concept__cartesian_product_is_common(see alsocommon_range). - Determines if
cartesian_productis a sized range (see alsosized_range). - Determines if
cartesian_productuses sized sentinel. - Returns the end of the produced view. Participates in overload resolution only if
cartesian_productsatisfies the helper concept__cartesian_product_common_arg.
The First range passed to cartesian_product_view is treated specially, since it is only passed through a single time. As a result, several constraints are relaxed on it:
Firstis aninput_rangeinstead offorward_range;Firstdoes not have to be asized_rangein order for thecartesian_product_viewto berandom_access_rangeorcommon_range;Firstdoes not have to becommon_rangein order for thecartesian_product_viewto bebidirectional_range.
π Member functions π
- constructor β constructs a
cartesian_product_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 the 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_rangeorforward_range. (C++20) (public member function ofstd::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::emptyis applicable to it. (C++20) (public member function ofstd::ranges::view_interface<D>) - front β returns the first element in the derived view. Provided if it satisfies
forward_range. (C++20) (public member function ofstd::ranges::view_interface<D>) - back β returns the last element in the derived view. Provided if it satisfies
bidirectional_rangeandcommon_range. (C++20) (public member function ofstd::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 ofstd::ranges::view_interface<D>)
π Deduction guides π
Thatβs the heading, bestie β no extra details listed here. π
π¦ Nested classes π¦
- iterator β the iterator type. (C++23) (exposition-only member class template*)
π Notes π
- Feature-test macro β
__cpp_lib_ranges_cartesian_product - Value β
202207L - Std β
(C++23) - Feature β
std::ranges::cartesian_product_view
π₯ Example π₯
// Run this code
#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++);
}
π Output π
(A 1 Ξ±) (A 1 Ξ²) (A 1 Ξ³) (A 1 Ξ΄)
(A 2 Ξ±) (A 2 Ξ²) (A 2 Ξ³) (A 2 Ξ΄)
(A 3 Ξ±) (A 3 Ξ²) (A 3 Ξ³) (A 3 Ξ΄)
(B 1 Ξ±) (B 1 Ξ²) (B 1 Ξ³) (B 1 Ξ΄)
(B 2 Ξ±) (B 2 Ξ²) (B 2 Ξ³) (B 2 Ξ΄)
(B 3 Ξ±) (B 3 Ξ²) (B 3 Ξ³) (B 3 Ξ΄)
π§ References π§
- C++23 standard (ISO/IEC 14882:2023):
- 26.7.31 Cartesian product view [range.stride]
π See also π
ranges::zip_viewβ a view consisting of tuples of references to corresponding elements of the adapted views. (C++23) (class template)views::zipβ a view consisting of tuples of references to corresponding elements of the adapted views. (C++23) (customization point object)
Source/version/date: cppreference.com, 2024.06.10, std::ranges...product_view(3) β bye bestie, go make something iconic πβ¨πΈ
π¦ 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