Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning vector

Reply
Thread Tools

returning vector

 
 
Richard
Guest
Posts: n/a
 
      10-26-2005
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.


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      10-26-2005
Richard wrote:
> 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!)
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-26-2005
Richard wrote:
> 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-26-2005
Richard wrote:
> what is the syntax for returning a vector?


Returning where? From where?

> temp is a vector


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

> return temp; ?


Most likely.

> return temp<>; ?


This is very likely a syntax error.

> return temp<int>; ?


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

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


Get a different book.

> Any helps are
> appreciated.


Read FAQ 5.8 while you're at it.

V
 
Reply With Quote
 
Mirek Fidler
Guest
Posts: n/a
 
      10-26-2005
> 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$pick_$en-us.html

Mirek
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      10-26-2005
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?

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.

 
Reply With Quote
 
Mirek Fidler
Guest
Posts: n/a
 
      10-26-2005
Kaz Kylheku wrote:
> 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-26-2005
Richard wrote:
> 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
 
Reply With Quote
 
Valentin Samko
Guest
Posts: n/a
 
      10-29-2005
Gianni Mariani wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57