C
cayblood
Hello, I have been interested in something kind of like the
next_permutation from the STL algorithm library, except that I want it
to find possible combinations of vector elements. Here is a more
detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which
contains an arbitrary number of elements, generate a new vector in
which each element consists of one element taken from its corresponding
inner vector. Generate all possible combinations of this new vector.
I'm sure this is confusing, and my wording is not very good, so here is
a concrete example of what I'm looking for:
original vector a = [[1, 2, 3], [4, 5, 6], [7, 8]]
first combination would be [1, 4, 7]
next combination would be [1, 4, 8]
next combination would be [1, 5, 7]
next combination would be [1, 5, 8]
next combination would be [1, 6, 7]
next combination would be [1, 6, 8]
next combination would be [2, 4, 7]
next combination would be [2, 4, 8]
next combination would be [2, 5, 7]
next combination would be [2, 5, 8]
next combination would be [2, 6, 7]
next combination would be [2, 6, 8]
next combination would be [3, 4, 7]
next combination would be [3, 4, 8]
next combination would be [3, 5, 7]
next combination would be [3, 5, 8]
next combination would be [3, 6, 7]
last combination would be [3, 6, 8]
If you know before coding what the size of the original vector is going
to be, this can be accomplished with a simple set of nested loops.
However, making it work with arbitrary sizes of vectors is harder. It
would be even cooler if I could come up with some code that could work
with any level of vector nesting, producing something like the boost
library's multi-dimensional arrays for vectors with more than two
levels of nesting.
Any thoughts? I'm not an expert at C++ templates so this is somewhat
difficult for me.
Thanks,
Carl Youngblood
next_permutation from the STL algorithm library, except that I want it
to find possible combinations of vector elements. Here is a more
detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which
contains an arbitrary number of elements, generate a new vector in
which each element consists of one element taken from its corresponding
inner vector. Generate all possible combinations of this new vector.
I'm sure this is confusing, and my wording is not very good, so here is
a concrete example of what I'm looking for:
original vector a = [[1, 2, 3], [4, 5, 6], [7, 8]]
first combination would be [1, 4, 7]
next combination would be [1, 4, 8]
next combination would be [1, 5, 7]
next combination would be [1, 5, 8]
next combination would be [1, 6, 7]
next combination would be [1, 6, 8]
next combination would be [2, 4, 7]
next combination would be [2, 4, 8]
next combination would be [2, 5, 7]
next combination would be [2, 5, 8]
next combination would be [2, 6, 7]
next combination would be [2, 6, 8]
next combination would be [3, 4, 7]
next combination would be [3, 4, 8]
next combination would be [3, 5, 7]
next combination would be [3, 5, 8]
next combination would be [3, 6, 7]
last combination would be [3, 6, 8]
If you know before coding what the size of the original vector is going
to be, this can be accomplished with a simple set of nested loops.
However, making it work with arbitrary sizes of vectors is harder. It
would be even cooler if I could come up with some code that could work
with any level of vector nesting, producing something like the boost
library's multi-dimensional arrays for vectors with more than two
levels of nesting.
Any thoughts? I'm not an expert at C++ templates so this is somewhat
difficult for me.
Thanks,
Carl Youngblood