473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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
2 1546
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: François Miville-Dechêne | last post by:
I find your language very nice, it is actually more than three quarters of what I had been dreaming for years a programming language should be. But I mourn the passing of APL, another interpreted...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort†method. For example: ...
99
by: Shi Mu | last post by:
Got confused by the following code: >>> a >>> b >>> c {1: , ], 2: ]} >>> c.append(b.sort()) >>> c {1: , ], 2: , None]}
21
by: bambam | last post by:
Would someone like to suggest a replacement for this? It works ok, but it doesn't look like any of the other code: tempList = sampleList= for port in tempList: pagefound = False for i in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.