Python to C++ conversion substituting vectors for lists in a recursive function

L

lugal

I'm new to C++, coming from a Python background. I wrote the following
code in C++ based on the Python code found here:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478

//beginning

#include <vector>
#include <iostream.h>
using namespace std;

void looper(vector <vector<int> > nseq, vector<int> comb);
vector <vector<int> > master;

int main() {
int n, C;
vector <vector<int> > seq;
vector<int> holder;
cout << "Enter constant: ";
cin >> C;
cout << "Enter n: ";
cin >> n;
for(i=0; i<=n; i++) {
vector <int> tmp;
for(int j=0; j<=C; j++) {
tmp.push_back(j);
}
seq.push_back(tmp);
}
looper(seq, holder);
return 0;
}

void looper(vector <vector<int> > nseq, vector<int> comb) {
if(nseq.size()>0) {
vector<int> tseq = nseq.at(0);
for(int i=0; i<tseq.size(); i++) {
vector <vector<int> > gseq = nseq;
vector<int> tcomb = comb;
gseq.erase(0);
tcomb.push_back(tseq);
looper(gseq, tcomb);
}
} else {
master.push_back(comb);
}
}

// end

The program dies on the line:

tcomb.push_back(tseq);

in the recursive function. Is my C++ translation accurate from the
original Python?
 
M

MyHaz

I didn't look at the original code but i you should know that passing
vectors directly (i.e by value) to functions is not a good idea
(results in big copy), its better to use `const vector<> &` or
`vector<> &` (i.e. by reference). In general you should try to reduce
the number of vector coping to a minimum.

As for the error in the code, it would be better to see the example of
the error, be it compiler or other wise.

I have a question about this line

`vector<int> tseq = nseq.at(0);`

as i have never seen the at() member of vector. And its not referenced
on the sgi site http://www.sgi.com/tech/stl/Vector.html

what version of the STL are you using?
 
P

Peter Otten

lugal wrote:

Your code has an undeclared int i in main().
gseq.erase(0);

I think erase() takes a pointer, not the element index:

gseq.erase(qseq.begin());
in the recursive function. Is my C++ translation accurate from the
original Python?

Coming from a Python background, you should have learned that the proper way
to answer that question is to write a test suite.

Generally speaking, if you want to learn a new language it is probably
better to extend examples written in that language than to translate code
written in another language you are already familiar with. What is
idiomatic in one language may be clumsy and inefficient in another.

Peter

PS: Your post is off-topic in c.l.py, consider asking for further/better
advice in a C++ newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top