By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

returning vector

P: n/a
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.
Oct 26 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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!)
Oct 26 '05 #2

P: n/a
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

Oct 26 '05 #3

P: n/a
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
Oct 26 '05 #4

P: n/a
> 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
Oct 26 '05 #5

P: n/a
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.

Oct 26 '05 #6

P: n/a
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
Oct 26 '05 #7

P: n/a
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
Oct 26 '05 #8

P: n/a
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
Oct 29 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.