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 

returning vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Richard
Guest





PostPosted: Wed Oct 26, 2005 4:00 pm    Post subject: returning vector Reply with quote



what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.


Back to top
Gianni Mariani
Guest





PostPosted: Wed Oct 26, 2005 4:07 pm    Post subject: Re: returning vector Reply with quote



Richard wrote:
Quote:
what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.




vector<int> f()
{
vector<int> xxx;

return xxx;
}

(rather an expensive operation if the vector is big however!)

Back to top
mlimber
Guest





PostPosted: Wed Oct 26, 2005 4:09 pm    Post subject: Re: returning vector Reply with quote



Richard wrote:
Quote:
what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.

Presumably you mean something like this:

vector<int> GetVec()
{
vector<int> v;
// Do something here to fill the vector
return v;
}

The problem is that this function returns by value, which means the
vector will likely be copied in its entirety, which could get expensive
unless your compiler can figure out that it doesn't need to copy it
(but I wouldn't depend on that). Alternately, you could pass the vector
in by reference:

void GetVec2( vector<int>& v )
{
// You might need to check to see if v holds anthing on entry
// (existing elements might be fine or might not be)

// Do something here to fill/change the vector
}

Now, no copying is involved no matter what optimizations you use.

There are some other techniques, too, but this latter one is probably
what you want.

Cheers! --M


Back to top
Victor Bazarov
Guest





PostPosted: Wed Oct 26, 2005 4:11 pm    Post subject: Re: returning vector Reply with quote

Richard wrote:
Quote:
what is the syntax for returning a vector?

Returning where? From where?

Quote:
temp is a vector

of what? What do you mean by "temp is a vector".

Quote:
return temp; ?

Most likely.

Quote:
return temp<>; ?

This is very likely a syntax error.

Quote:
return temp<int>; ?

No, but you may be getting close if 'temp' is a template-id.

Quote:
I got a book that does not talk about how to do this.

Get a different book.

Quote:
Any helps are
appreciated.

Read FAQ 5.8 while you're at it.

V

Back to top
Mirek Fidler
Guest





PostPosted: Wed Oct 26, 2005 4:45 pm    Post subject: Re: returning vector Reply with quote

Quote:
vector<int> GetVec()
{
vector<int> v;
// Do something here to fill the vector
return v;
}

The problem is that this function returns by value, which means the
vector will likely be copied in its entirety, which could get expensive
unless your compiler can figure out that it doesn't need to copy it
(but I wouldn't depend on that). Alternately, you could pass the vector
in by reference:

Or wait for r-value references or use NTL now:

http://upp.sourceforge.net/srcdoc-us.html

Mirek

Back to top
Kaz Kylheku
Guest





PostPosted: Wed Oct 26, 2005 4:56 pm    Post subject: Re: returning vector Reply with quote

Gianni Mariani wrote:
Quote:
vector<int> f()
{
vector<int> xxx;

return xxx;
}

(rather an expensive operation if the vector is big however!)

Why is that? Couldn't the vector data could be shared by reference
between the two copies of the vector, subject to copy-on-write?

Unix-like operating systems have a fork() function which this for an
entire address space.

It's perfectly good style to return strings from functions in C++, and
strings are essentially vectors of characters.


Back to top
Mirek Fidler
Guest





PostPosted: Wed Oct 26, 2005 5:08 pm    Post subject: Re: returning vector Reply with quote

Kaz Kylheku wrote:
Quote:
Gianni Mariani wrote:

vector<int> f()
{
vector<int> xxx;

return xxx;
}

(rather an expensive operation if the vector is big however!)


Why is that? Couldn't the vector data could be shared by reference
between the two copies of the vector, subject to copy-on-write?

It could not. operator[] overloading mechanism is not robust enough for
that:

Part of std::vector definition is that you are get non-const reference for

xxx[index]

now consider

vector<int> a;
vector<int> b;
.....
T& x = a[index];
b = a;
x = 123;

- there is no way how to detect last assignment and perform copy ("on
write"). You can to some degree solve this problem with operator[]
returning some sort of proxy instead of reference, but such solution
would render vector less usable...

Mirek

Back to top
John Harrison
Guest





PostPosted: Wed Oct 26, 2005 7:21 pm    Post subject: Re: returning vector Reply with quote

Richard wrote:
Quote:
what is the syntax for returning a vector?

temp is a vector

return temp; ?

return temp<>; ?

return temp<int>; ?

I got a book that does not talk about how to do this. Any helps are
appreciated.



return temp of course.

Your book most likely doesn't talk about because the expert who wrote it
didn't realise that newbies often think things have to be different,
i.e. there must be some special syntax for vectors of something. Temp is
a variable, it matters not one bit that it is a vector, string, pointer,
or anything else, to return the value contained in a variable is always
the same syntax.

john

Back to top
Valentin Samko
Guest





PostPosted: Sat Oct 29, 2005 2:33 pm    Post subject: Re: returning vector Reply with quote

Gianni Mariani wrote:
Quote:
vector<int> f()
{
vector<int> xxx;
return xxx;
}
(rather an expensive operation if the vector is big however!)

Only if your compiler does not do NRVO. The standard allows optimising the copy away.
Unfortunately only a few compilers do this, and because of this in a general case I prefer

template<class OutIt>
void f(OutIt out)
{
// (for example std::copy(begin, end, out))
}

std::vector<int> v;
f(std::back_inserter(v));

This will always avoid the extra copy of the vector, no matter how good your compiler is.

--

Valentin Samko - http://www.valentinsamko.com

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
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.