returning a vector from a funciton

A

Affan Syed

Hi,
first let me apologize before hand for not knowing this trivial issue and my
inability to find it after searching on the web for an answer to what seems
to be starightforward. I have used vectors (STL) for some time but have
never had the need to actually return one from a function. what i mean is
that does something like the code below work? (this is very artificial case
but just consider that this is imp to what i want to do and there isnt a way
around it)

vector<myClass> RetVector(){
vector<myClass> retVector;
 
A

Ali Cehreli

I have used vectors (STL) for some time but have
never had the need to actually return one from a function. what i mean is
that does something like the code below work?

It works; I do that all the time.
vector<myClass> RetVector(){
vector<myClass> retVector;
.
/// add a few myClass objects to the vector
.
return retVector;
}

int main (){
vector<myClass> getVector;

getVector = RetVector();

The code above has default construction and assignment. You might
prefer

vector<myClass> getVector = RetVector();

which will involve just the construction of the getVector object under
many compilers. You should try it in your environment...
///use the vector to do some computation
return 1;

Returning 1 normally indicates error. EXIT_SUCCESS is a better choice
for successful return from main.
}


What i mean is that does the copy constructor do the right thing and I now
have faithfull copy of the orignal vector that i can now use to index
through and play to my hearts content?

Yes. Again though, the way you wrote it, the assignment operator is
used, not the copy constructor.

Now it all depends on the implementation of myClass (e.g. the
assignment operator, the copy constructor, etc.) As long as it's
implemented correctly, you are safe.

Ali
 
M

Michael

will it be quicker to do:

bool GetVector( vector<myClass>& v)
{
assert(v.size() == 0);
//Populate v;

return true;
}

int main()
{
vector<myClass> myVec;
if( GetVector(myVec) )
{
}
}

??
To reduce copying of data items?

Regard Mike
 
P

Pavel Vozenilek

Affan Syed said:
vector<myClass> RetVector()
Consider:

auto_ptr<vector<myClass> > RefVector() {
....
auto_ptr<vector<myClass> > result(new vector<myClass>());
result->reserve(expected items #);
....
return result.
}


No copying. Safe.

/Pavel
 
A

Affan Syed

That is exactly the idea that i am trying to do now.. It clicked me as soon
as I had posted the note., and to top it it works perfectly fine.
Thanks
Affan
 
C

Cy Edmunds

Affan Syed said:
Hi,
first let me apologize before hand for not knowing this trivial issue and
my inability to find it after searching on the web for an answer to what
seems to be starightforward. I have used vectors (STL) for some time but
have never had the need to actually return one from a function. what i
mean is that does something like the code below work? (this is very
artificial case but just consider that this is imp to what i want to do
and there isnt a way around it)

vector<myClass> RetVector(){
vector<myClass> retVector;
.
/// add a few myClass objects to the vector
.
return retVector;
}

int main (){
vector<myClass> getVector;

getVector = RetVector();

///use the vector to do some computation
return 1;
}


What i mean is that does the copy constructor do the right thing and I now
have faithfull copy of the orignal vector that i can now use to index
through and play to my hearts content?


Thanks
Affan

Another great option: send the values to an output iterator. That still
works with a std::vector but also works with just about any other container.
e.g.

template <typename OITER>
void
function345(OITER oi)
{
*oi++ = 3;
*oi++ = 4;
*oi++ = 5;
}

The user can call it lots of ways:

std::vector<int> yadda; // or any other standard container
function345(std::back_inserter(yadda));

but also

int answer[3];
function345(answer);

Of course your function may somehow be tied directly to std::vector. If so
this isn't appropriate. But most functions which return a sequence of values
don't care how the client stores them.
 
G

Guest

I am rearranging the order of replies to maintain the natural flow of
conversation.

It might or might not be quicker. You have to test before to decide. In the
way I use it the most, it doesn't matter; because usually, the definitions
of such functions appear fully defined before their uses. (As in the example
I've given above.)

When the compiler applies RVO or NRVO (return value optimization or named
return value optimization), there is no copying involved.

Affan Syed said:
That is exactly the idea that i am trying to do now.. It clicked me as soon
as I had posted the note., and to top it it works perfectly fine.

If it works for you, great!

The two functions do not have the same semantics though. The latter version,
the one that modifies the argument, must also document what happens if the
argument is not empty:

a) emit some type of error (e.g. assert, as suggested by Michael)
b) clear before adding new objects
c) append the new objects
d) something else

The first implementation does not have this concern. In that case, the
decision is pushed to the caller.

Ali
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top