| | |
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: 48
Reputation:
Solved Threads: 10
0
#2 24 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 24 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.
0
#4 24 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 |
ada add api array based binary bitmap c++ calculator char char* class classes client commentinghelp compression console convert data decide development directshow display download ebook email embed encode encryption error file filter format fstream function functions game gdi+ graph guessing hash int integer introductory java keyboard linker lnk2019 math matrix method modal newbie node noob number numbers objects output parallel parameter partnership php pointer pong prime problem proficiency program programing programming python qt read record recursion recursive regqueryvalueex return richedit rpg samples selection send set snakes stop string studio subclass template templates text toolbar toolkit tree url variable visual win32






