| | |
How to capture a string into variable in a recursive function in C++?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
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?
The expected output is:
the function below doesn't return the string as I expected?
c Syntax (Toggle Plain Text)
#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; }
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Oct 2009
Posts: 44
Reputation:
Solved Threads: 9
0
#2 19 Days Ago
C++ Syntax (Toggle Plain Text)
cout << OutputString << endl; // This prints nothing why?
That's the output I got, when I launched your program:
C++ Syntax (Toggle Plain Text)
TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 19 Days Ago
•
•
•
•
It prints.C++ Syntax (Toggle Plain Text)
cout << OutputString << endl; // This prints nothing why?
That's the output I got, when I launched your program:
C++ Syntax (Toggle Plain Text)
TCCGTGCGTACGTCCCGTGCGTACGTACCGTGCGTACGT
I have corrected the comment in the code.
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#4 19 Days Ago
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.
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.
![]() |
Similar Threads
- Argorithm: palindromes, write recursive function. (Computer Science)
- Recursive Function to Reverse an Integer (C++)
- Error in string reversal using recursive function (C)
- recursive function (C)
- recursive function (C++)
- Problem with string variable in void prnt function (C++)
Other Threads in the C++ Forum
- Previous Thread: My code is giving lots of errors
- Next Thread: Test your C/C++ Skills [Free Online Test]
| Thread Tools | Search this Thread |
.dll add api array assignment background binary bitmap borland c# c++ calling char char* class code compile compression console decode delete desktop development dialog digit domain download dwmapi dynamic echo ect embedded encryption equation error exam examples file fpx fstream function functions game gdi+ gmail graph gui html i/o ifstream incode insert int java jni kioti16 line linked linux math maze method multiple music number numbers output performance php plotting practice prime print problem program programming projects python read recursion recursive return search space sticky string struct temperature template text traverse tree tutorial url variable vector visual visualization visualstudio win32






