returning vector

R

Richard

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.
 
G

Gianni Mariani

Richard said:
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!)
 
M

mlimber

Richard said:
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
 
V

Victor Bazarov

Richard said:
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
 
M

Mirek Fidler

vector said:
{
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
 
K

Kaz Kylheku

Gianni said:
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.
 
M

Mirek Fidler

Kaz said:
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
 
J

John Harrison

Richard said:
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
 
V

Valentin Samko

Gianni said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top