I have several data that looks like this:
Vector1_elements = T,C,A
Vector2_elements = C,G,A
Vector3_elements = C,G,T
..... up to ...
VectorK_elements = ...
#Note also that the member of each vector is always 3.
What I want to do is to create all combination of elements in Vector1 through out VectorK. Hence in the end we hope to get this output (using Vector1,2,3):
TCC
TCG
TCT
TGC
TGG
TGT
TAC
TAG
TAT
CCC
CCG
CCT
CGC
CGG
CGT
CAC
CAG
CAT
ACC
ACG
ACT
AGC
AGG
AGT
AAC
AAG
AAT
The problem I am having now is that the following code of mine does that by hardcoding the loops. Since number of Vectors can be varied, we need a flexible way to get the same result. Is there any?
This code of mine can only handle up to 3 Vectors (hardcoded):
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main ( int arg_count, char *arg_vec[] ) {
vector <string> Vec1;
Vec1.push_back("T");
Vec1.push_back("C");
Vec1.push_back("A");
vector <string> Vec2;
Vec2.push_back("C");
Vec2.push_back("G");
Vec2.push_back("A");
vector <string> Vec3;
Vec3.push_back("C");
Vec3.push_back("G");
Vec3.push_back("T");
for (int i=0; i<Vec1.size(); i++) {
for (int j=0; j<Vec2.size(); j++) {
for (int k=0; k<Vec1.size(); k++) {
cout << Vec1[i] << Vec2[i] << Vec3[k] << endl;
}
}
}
return 0;
}
Use next_permutation function implemented in std of stl
The simplest way to approach this is to use recursion. The function will have one loop in it and will call itself, merging itself with the output of the recursive call. Of course, recursion can be converted to iteration if you're worried about stack space, but at least as a starting point, the recursive solution will probably be easiest for you.
A C++0x solution. Provided, of course, your compiled supports it (currently GCC 4.5 and VS2010, I think).
The following compiles and works with GCC 4.5 using
-std=c++0x
switch. The use of variadic templates makes it possible to combine arbitrary number of containers. I am sure you can come up with a more idiomatic solution.This will do the trick:
Call with:
I too am interested in building some sort of easy to rinse and repeat combinatorial. I am familiar with the odometer driven type approach, if you will, where you've got walking indices. Something along those lines. The point is, to easily build out the tuples across an arbitrary set of unrelated vectors.
This does not quite answer your question, I don't think, but you could build static/design time combinations using a variadic production such as the following, where T1-3 are arbitrary types:
Assuming you've got a vector that looks something like this:
Like I said, this is a design time consideration. It does not build tuples across a run time range of vectors. That's the down side. The up side, however, is that you gain compile time comprehension of your vectored tuples.
Again, not quite what you, or even I, are after, but maybe it helps spark favorable feedback.
Since you seem to want each output be the length of individual vectors, and you seem to know that each vector is 3 elements wide from
using recursion for a general solution seems a bit overkill here.
You may use something like that :