I tried to print all the possible combination of members of several vectors. Why the function below doesn t return the string as I expected?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
string EnumAll(const vector<vector<string> > &allVecs, size_t vecIndex, string
strSoFar)
{
string ResultString;
if (vecIndex >= allVecs.size())
{
//cout << strSoFar << endl;
ResultString = strSoFar;
//return ResultString;
}
for (size_t i=0; i<allVecs[vecIndex].size(); i++) {
strSoFar=EnumAll(allVecs, vecIndex+1, strSoFar+allVecs[vecIndex][i]);
}
ResultString = strSoFar; // Updated but still doesn t return the string.
return ResultString;
}
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");
vector <vector<string> > allVecs;
allVecs.push_back(Vec1);
allVecs.push_back(Vec2);
allVecs.push_back(Vec3);
string OutputString = EnumAll(allVecs,0,"");
// print the string or process it with other function.
cout << OutputString << endl; // This prints nothing why?
return 0;
}
The expected output is:
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