C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Vectors passed as parameters to recursive functions.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
lugal
Guest





PostPosted: Wed Mar 23, 2005 5:20 pm    Post subject: Vectors passed as parameters to recursive functions. Reply with quote



I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning

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

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

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

void looper(vector nseq, vector<int> comb) {
if(nseq.size()>0) {
vector<int> tseq = nseq.at(0);
for(int i=0; i<tseq.size(); i++) {
vector 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]);

It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ali Çehreli
Guest





PostPosted: Wed Mar 23, 2005 11:04 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote



"lugal" <lcrees (AT) gmail (DOT) com> wrote

Quote:
I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning

#include <vector
#include


#include <iostream>

Quote:
using namespace std;

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

Prefer giving hard-to-read types meaningful names to make the code easier to
follow. (I had to do this to be able to read your code). I just chose to
name them Row and Table:

typedef vector<int> Row;
typedef vector<Row> Table;

Now looper and master will be:

void looper(Table nseq, Row comb);
Table master;

But wait! Just like in C, arguments are passed by value (i.e. copied) to
functions. Table and Row are potentially expensive types to copy. Since you
don't modify nseq or comb in looper, you should consider passing the
arguments by const reference:

void looper(Table const & nseq, Row const & comb);

[Note: You will need do this below at the definition as well.]

[...]

for(i=0; i<=n; i++)

Where was that 'i' defined? Didn't you send the same code that you have
trouble with?

[...]

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

That line should not compile. vector::erase does not take an int. Did you
want to remove the first element of gseq as in:

gseq.erase(gseq.begin());

If your intent was really to erase the first element of the vector, you
should consider using some other container because removing elements from a
vector is done by moving the following elements to the beginning of the
vector, by copying elements to the ones that are erased. I think that
std::list or std::deque would be better choices in this program.

[...]

Quote:
The program dies on the line:

tcomb.push_back(tseq[i]);

I am surprised that the program could be compiled at all. Other than that, I
don't see anyting wrong with that line.

Quote:
It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?

There is nothing special about the vector or recursive functions. Types that
are expensive to copy should better be passed by const reference, to any
function. vector of vector could be very expensive to copy.

Ali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dirk Sigurdson
Guest





PostPosted: Wed Mar 23, 2005 11:17 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote



You should probably be passing the vectors in by reference. If you don't
pass by reference, then every time you call looper it's going create a copy
of your vector<vector and vector<int> on the stack. I would imagine
that the error that you're seeing is as a result of a stack overflow.

Declare looper as:

void looper(vector <vector& nesq, vector<int>& comb);


On 3/23/05 9:20 AM, in article
[email]1111575652.763265.21290 (AT) g14g2000cwa (DOT) googlegroups.com[/email], "lugal"
<lcrees (AT) gmail (DOT) com> wrote:

Quote:
I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning

#include <vector
#include using namespace std;

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

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

void looper(vector nseq, vector<int> comb) {
if(nseq.size()>0) {
vector<int> tseq = nseq.at(0);
for(int i=0; i<tseq.size(); i++) {
vector 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]);

It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Wed Mar 23, 2005 11:17 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

"lugal" <lcrees (AT) gmail (DOT) com> writes:

Quote:
I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning

#include <vector
#include

Please note that there is no Standard C++ header (all?) compiler will offer such a header for backward compatibility,
though.


Quote:
The program dies on the line:

tcomb.push_back(tseq[i]);

It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?

No.

It seems that the tseq in question doesn't have i+1 elements.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
REH
Guest





PostPosted: Wed Mar 23, 2005 11:18 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote


"lugal" <lcrees (AT) gmail (DOT) com> wrote

Quote:
I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning

#include <vector
#include using namespace std;

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

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

void looper(vector nseq, vector<int> comb) {
if(nseq.size()>0) {
vector<int> tseq = nseq.at(0);
for(int i=0; i<tseq.size(); i++) {
vector 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]);

It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?

I'm really not sure if you are trolling or not. You defined a recursive

function that takes a vector of a vector as a value parameter. This is
going to make a copy of the 2d vector on every invocation. You should have
send it in as a reference.

Again, I'm not sure of your motives, but people just seem to love making
purposely inefficient programs in a language they dislike to show how "bad"
it is compared against an efficient version done in their favorite one.





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Schwab
Guest





PostPosted: Thu Mar 24, 2005 11:44 am    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

Ali Çehreli wrote:
Quote:
"lugal" <lcrees (AT) gmail (DOT) com> wrote in message

gseq.erase(0);


That line should not compile. vector::erase does not take an int.

And of course, that choked on my compiler, too. I wonder, though: Is
it possible that if std::vector::iterator were just a pointer type, this
0 could be interpreted as null? This might prevent compile-time
reporting of this error.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Thu Mar 24, 2005 12:00 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

lugal wrote:
Quote:
I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning
[ program snipped ]
// end

The program dies on the line:

tcomb.push_back(tseq[i]);

It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?

Dude! your program does not even compile. In particular, one of the
loop counters is not declared. Also, my compiler did not recognize
the call vector<vector::erase(int), which I replaced with
erase(gseq.begin()).

After I fixed these two problems, the program ran, without problems.

Having used vectors rather effectively for all sorts of things, I
suspect that your program needs some extra polish, particularly around
that gseq.erase(0) statement, which is probably doing something really
whacky and unexpected if your compiler digested it.

I am assuming that the undeclared loop counter is a typo, and that the
code you posted is otherwise an accurate representation of your problem.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Friedhelm Hoerner
Guest





PostPosted: Thu Mar 24, 2005 12:01 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

"lugal" <lcrees (AT) gmail (DOT) com> wrote

Quote:
I'm new to C++, coming from a background in other languages that allow
this solution to work (Python). When I run the following code:

//beginning

#include <vector
#include using namespace std;

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

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

void looper(vector nseq, vector<int> comb) {
if(nseq.size()>0) {
vector<int> tseq = nseq.at(0);
for(int i=0; i<tseq.size(); i++) {
vector 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]);

It seems vectors can't be passed effectively as parameters to recursive
functions. Is this true?

Well, it is not effective to pass vectors by value, but given enough
time and memory there is no other restriction;-). Others already
pointed out proper modifications.

The real answer to your Problem is hidden in Ali's response:

gseq.erase(0);

should not compile because vector<T>::erase() requires an
vector<T>::iterator as a parameter.
On the other hand vector<T>::iterator is actually a pointer and
therefore 0 may be taken as a valid (null) iterator.
Therefore you erase memory at adress 0 and that results in corrupt
memory - the next statement may not execute...

HTH Friedhelm.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Marco Manfredini
Guest





PostPosted: Thu Mar 24, 2005 2:26 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

REH wrote:

Quote:

"lugal" <lcrees (AT) gmail (DOT) com> wrote in message
It seems vectors can't be passed effectively as parameters to
recursive functions. Is this true?

I'm really not sure if you are trolling or not. You defined a
recursive
function that takes a vector of a vector as a value parameter. This
is
going to make a copy of the 2d vector on every invocation. You should
have send it in as a reference.

Again, I'm not sure of your motives, but people just seem to love
making purposely inefficient programs in a language they dislike to
show how "bad" it is compared against an efficient version done in
their favorite one.

He said, that he is new to C++ and comes from a Python background. And
therefore, not surprising, his program is written in a pythonish way.
He assumes that non-scalar types are passed by reference, which is the
idea you have about how argument passing should work, if you come from
Java, C# or a scripting language.

Marco


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gary Labowitz
Guest





PostPosted: Fri Mar 25, 2005 11:05 am    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

"Marco Manfredini" <ok_nospam_ok (AT) phoyd (DOT) net> wrote

Quote:
REH wrote:
snip
He said, that he is new to C++ and comes from a Python background. And
therefore, not surprising, his program is written in a pythonish way.
He assumes that non-scalar types are passed by reference, which is the
idea you have about how argument passing should work, if you come from
Java, C# or a scripting language.

Except for Java, of course, where all parameters are passed by value.
--
Gary



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Sat Mar 26, 2005 9:30 am    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

Gary Labowitz wrote:
Quote:
"Marco Manfredini" <ok_nospam_ok (AT) phoyd (DOT) net> wrote in message
He said, that he is new to C++ and comes from a Python background. And
therefore, not surprising, his program is written in a pythonish way.
He assumes that non-scalar types are passed by reference, which is the
idea you have about how argument passing should work, if you come from
Java, C# or a scripting language.

Except for Java, of course, where all parameters are passed by value.

In Java, all values that are not builtin types (like int) are
references. Furthermore, there is not syntax for dereferencing
these references, like (*ptr) in C/C++; if you want a copy of the
object, you create a new reference with a method call, such as clone().
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Schwab
Guest





PostPosted: Sat Mar 26, 2005 9:31 am    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

Gary Labowitz wrote:
Quote:
"Marco Manfredini" <ok_nospam_ok (AT) phoyd (DOT) net> wrote in message
news:2304769.eEN2JPQYOS (AT) technoboredom (DOT) net...

REH wrote:

snip

He said, that he is new to C++ and comes from a Python background. And
therefore, not surprising, his program is written in a pythonish way.
He assumes that non-scalar types are passed by reference, which is the
idea you have about how argument passing should work, if you come from
Java, C# or a scripting language.


Except for Java, of course, where all parameters are passed by value.

How would one pass a large object by value in Java?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Mar 26, 2005 5:45 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

In article <2qydnaX88Z_kgdnfRVn-og (AT) comcast (DOT) com>, Antoun Kanawati
<antounk (AT) comcast (DOT) net> writes
Quote:
In Java, all values that are not builtin types (like int) are
references. Furthermore, there is not syntax for dereferencing
these references, like (*ptr) in C/C++; if you want a copy of the
object, you create a new reference with a method call, such as clone().

Actually Java uses named values for instances of built-in types but
objects of user defined types are nameless and there is no mechanism for
naming them, all we can do is provide a named reference to the object.
That named reference is passed and returned by value. It behaves far
more like a pointer than as a C++ reference.

I know these differences are subtle and an endless cause of confusion to
programmers moving between C++ and Java.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Mar 26, 2005 5:45 pm    Post subject: Re: Vectors passed as parameters to recursive functions. Reply with quote

In article <9-Gdne9J88Beh9nfRVn-hw (AT) rcn (DOT) net>, Jeff Schwab
<jeffrey.schwab (AT) rcn (DOT) com> writes
Quote:
How would one pass a large object by value in Java?

How do you pass an object at all? All you can do in Java is copy a
hidden pointer. I am sure the OP was alluding to this with his throw
away comment.

Quote:


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gary Labowitz
Guest





PostPosted: Sun Mar 27, 2005 2:18 am    Post subject: Re: Vectors passed as parameters to recursive functions. [OT Reply with quote

"Jeff Schwab" <jeffrey.schwab (AT) rcn (DOT) com> wrote

Quote:
Gary Labowitz wrote:
"Marco Manfredini" <ok_nospam_ok (AT) phoyd (DOT) net> wrote in message
news:2304769.eEN2JPQYOS (AT) technoboredom (DOT) net...

REH wrote:

snip

He said, that he is new to C++ and comes from a Python background. And
therefore, not surprising, his program is written in a pythonish way.
He assumes that non-scalar types are passed by reference, which is the
idea you have about how argument passing should work, if you come from
Java, C# or a scripting language.


Except for Java, of course, where all parameters are passed by value.

How would one pass a large object by value in Java?

You don't. You pass a reference variable by value. It is "roughly"
equivalent to passing a pointer -- except it isn't a pointer in Java. This
is OT here, so transfer to Java ng if you wish to explore further.
--
Gary



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.