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:


#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++) {
looper(seq, holder);
return 0;

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

// end

The program dies on the line:


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 =;`

as i have never seen the at() member of vector. And its not referenced
on the sgi site

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().
I think erase() takes a pointer, not the element index:

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.


PS: Your post is off-topic in, 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.