By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,399 Members | 886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,399 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/Coo.../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[i]);
looper(gseq, tcomb);
}
} else {
master.push_back(comb);
}
}

// end

The program dies on the line:

tcomb.push_back(tseq[i]);

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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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?

Jul 18 '05 #2

P: n/a
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.


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.