- ranges[meta header]
- std::ranges[meta namespace]
- class template[meta id-type]
- cpp23[meta cpp]
namespace std::ranges {
template<input_range First, forward_range... Vs>
requires (view<First> && ... && view<Vs>)
class cartesian_product_view : public view_interface<cartesian_product_view<First, Vs...>> {…… }; // (1)
namespace views {
inline constexpr /*unspecified*/ cartesian_product = /*unspecified*/; // (2)
}
}
cartesian_product_view
は複数のRangeの直積をとり、その要素をtuple
として見せるview
。
このview
の要素数は、すべての指定したRangeの要素数の積である。
- (1):
cartesian_product_view
のクラス定義
- (2):
cartesian_product_view
を生成するカスタマイゼーションポイントオブジェクト(Rangeアダプタオブジェクトではない)
borrowed |
sized |
output |
input |
forward |
bidirectional |
random_access |
contiguous |
common |
viewable |
view |
|
(1) |
〇 |
〇 |
〇 |
(2) |
(3) |
|
(4) |
○ |
○ |
- (2): 式
views::cartesian_product(Es...)
の効果は次の通り
本説明に用いる説明専用要素を以下のように定義する。
namespace std::ranges {
template<bool Const, class First, class... Vs>
concept cartesian-product-is-random-access = // exposition only
(random_access_range<maybe-const<Const, First>> && ... &&
(random_access_range<maybe-const<Const, Vs>>
&& sized_range<maybe-const<Const, Vs>>));
template<class R>
concept cartesian-product-common-arg = // exposition only
common_range<R> || (sized_range<R> && random_access_range<R>);
template<bool Const, class First, class... Vs>
concept cartesian-product-is-bidirectional = // exposition only
(bidirectional_range<maybe-const<Const, First>> && ... &&
(bidirectional_range<maybe-const<Const, Vs>>
&& cartesian-product-common-arg<maybe-const<Const, Vs>>));
template<class First, class... Vs>
concept cartesian-product-is-common = // exposition only
cartesian-product-common-arg<First>;
template<class... Vs>
concept cartesian-product-is-sized = // exposition only
(sized_range<Vs> && ...);
template<bool Const, template<class> class FirstSent, class First, class... Vs>
concept cartesian-is-sized-sentinel = // exposition only
(sized_sentinel_for<FirstSent<maybe-const<Const, First>>,
iterator_t<maybe-const<Const, First>>> && ...
&& (sized_range<maybe-const<Const, Vs>>
&& sized_sentinel_for<iterator_t<maybe-const<Const, Vs>>,
iterator_t<maybe-const<Const, Vs>>>));
template<cartesian-product-common-arg R>
constexpr auto cartesian-common-arg-end(R& r) { // exposition only
if constexpr (common_range<R>) {
return ranges::end(r);
} else {
return ranges::begin(r) + ranges::distance(r);
}
}
}
#include <ranges>
#include <vector>
#include <list>
#include <print>
int main() {
std::vector v = {1, 2};
const std::list l = {'a', 'b', 'c'};
std::println("{}", std::views::cartesian_product(v, l));
// 空になる場合
std::println("{}", std::views::cartesian_product());
}
- std::views::cartesian_product[color ff0000]
[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c')]
[]