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; 
        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
cout << OutputString << endl;  // This prints nothing why?

It prints.
That's the output I got, when I launched your program:

TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
cout << OutputString << endl;  // This prints nothing why?

It prints.
That's the output I got, when I launched your program:

TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT

I mean it doesn't print the output as I expected, i.e. list of 27 length 3 strings.

I have corrected the comment in the code.

The problem is the setting of strSoFar to whatever EnumAll returns and changing what the previous function needs to have in that variable.

Not setting strSoFar in your for loop gives you this effect:

Initial function call: strSoFar = ""
1st recurse: strSoFar = "T"
2nd recurse: strSoFar = "TC"
1st 3rd recurse: strSoFar = "TCC" (print from if statement)
Fall back to 2nd w/out setting strSoFar: strSoFar = "TC"
2nd 3rd recurse: strSoFar = "TCG" (print from if statement)
Fall back to 2nd w/out setting strSoFar: strSoFar = "TC"
3rd 3rd recurse: strSoFar = "TCT" (print from if statement)

Sorry if my numbering is confusing. It's early and I couldn't come up with anything better =)

Recursion is really tricky when you're first starting out, but keep working at it and it will become almost second nature.

Hope that helps you.

This article has been dead for over six months. Start a new discussion instead.