From: Joey Mukherjee on 15 Feb 2010 05:55 I have N number of sets of numbers and I want to get every possible combination from these numbers. For example: std::vector <int> set1; std::vector <int> set2; std::vector <int> set3; for (int i = 0; i < 2; ++i) set1.push_back (i); for (int i = 0; i < 16; ++i) set2.push_back (i); for (int i = 0; i < 9; ++i) set3.push_back (i); I want {0,0,0}, {0,0,1},.. {0,0,8}.. {1,15,8} The number of sets can be anything as well as the number of elements in each set. Ideally, I'd like to restrict certain values as well. I've been looking at next_permutation but I am at a loss for how to make this work with multiple sets. Can someone give me some help? Thanks, Joey -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Larry Evans on 15 Feb 2010 13:13 On Feb 15, 4:55 pm, Joey Mukherjee <joe...(a)gmail.com> wrote: > I have N number of sets of numbers and I want to get every possible > combination from these numbers. > > For example: > > std::vector <int> set1; > std::vector <int> set2; > std::vector <int> set3; > > for (int i = 0; i < 2; ++i) set1.push_back (i); > for (int i = 0; i < 16; ++i) set2.push_back (i); > for (int i = 0; i < 9; ++i) set3.push_back (i); > > I want {0,0,0}, {0,0,1},.. {0,0,8}.. {1,15,8} > > The number of sets can be anything as well as the number of elements > in each set. Ideally, I'd like to restrict certain values as well. > I've been looking at next_permutation but I am at a loss for how to > make this work with multiple sets. > > Can someone give me some help? This sounds like a cross-product of the sets. There is a c++metaprogramming method for calculating this at compile time: http://article.gmane.org/gmane.comp.lib.boost.user/55821/match=combinations I think you should be able to code a runtime version of this compile time version without too much trouble. HTH. -regards, Larry -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: U++ 2070 released Next: Why does inserter need Iterator template parameter |