• Advertisement

Archived

This topic is now archived and is closed to further replies.

Is it possible to returning a vector from a function?

This topic is 5036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Is it possible to returning a vector from a function? If i have a vector inside a function, and i want to pass it outside. How can i do it? is it possible? any examples? or i have to pass each of the values in the vector one by one?
// how to return myDoubles ?


CheckMyDoubles();


CheckMyDoubles()
{
vector<double>   myDoubles;

return myDoubles;
}

Thanks very much for any helps, ( I know you guys will say try not to ask questions with abstract of codes..., but the code already grown to a certain of length. Crossing a couple of other source files... So showing everything here would be not quite possible... ) [edited by - NewbieA on May 8, 2004 6:52:26 AM] [edited by - NewbieA on May 8, 2004 6:53:08 AM]

Share this post


Link to post
Share on other sites
Advertisement

std::vector<int> func()
{
std::vector<int> i;
return i;
}

Share this post


Link to post
Share on other sites
Just declare the function to return vector, the same way you''d declare it to return anything else.

Share this post


Link to post
Share on other sites
It''s usually better to pass an empty vector into the function by reference and have the function put the stuff in it. That way you don''t have to relate to the problems with the vector being returned by value.

IE:

void CheckMyDoubles( vector<double>& vec )
{
vec.push_back( 42.0 );
// ...

}


// ...


vector<double> vec;

CheckMyDoubles( vec );


--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Arild Fines
It''s usually better to pass an empty vector into the function by reference and have the function put the stuff in it. That way you don''t have to relate to the problems with the vector being returned by value.


Eh? What problems are there, despite some needless copying?

Share this post


Link to post
Share on other sites

  • Advertisement