Is it possible to returning a vector from a function?

Started by
4 comments, last by NewbieA 19 years, 11 months ago
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]
Advertisement
std::vector<int> func(){std::vector<int> i;return i;}
Just declare the function to return vector, the same way you''d declare it to return anything else.
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]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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?

Got it. Thanks all

Thanks for all the help

This topic is closed to new replies.

Advertisement