I am trying to create a function to create a Cartesian product of a variable number of input ranges using the STL style. My main format is that the function takes a fixed range and the beginning of the output range, and then the variational number of bidirectional input iterators.
template <
typename BidirectionalIterator,
typename OutputIterator,
typename... Args
>
void cartesian_product(
BidirectionalIterator first,
BidirectionalIterator last,
OutputIterator result,
Args&&... args
);
My idea for argsis what I am doing from it tuple, then I iterate over this one tupleto extract the elements. This would require me to follow a few basic steps:
- Make
tupleoutargs - Separate each iterator in the newly created
tuple - Build each iterator in sequence
tuplein sequence so that we get all possible combinations of values โโin ranges.
3: A = {0, 1} B = {2, 3}, A x B = {(0, 2), (0, 3), (1, 2), (1, 3)}.
, :
auto arg_tuple = std::make_tuple(std::forward<Args>(args)...);
. , - push_back , *result . , ostream , , :
template <typename Tuple, typename T>
auto operator<<(const Tuple &lhs, const T &rhs)
-> decltype(std::tuple_cat(lhs, std::make_tuple(rhs)))
{
return std::tuple_cat(lhs, std::make_tuple(rhs));
}
, , . - :
template <typename T>
auto pre_increment(T &x) -> decltype(++x) {
return ++x;
}
3000 for_each tuple, .
, ++ 14 . ++ 11.
boost::fusion , , .