Re: returning a vector from a funciton
From: Affan Syed (asyed_at_usc.edu)
Date: 11/27/04
- Next message: Peter Koch Larsen: "Re: find number of digits"
- Previous message: Jonathan Mcdougall: "Re: C/C++ undefinded symbol problem"
- Maybe in reply to: Affan Syed: "returning a vector from a funciton"
- Next in thread: Ali Çehreli: "Re: returning a vector from a funciton"
- Reply: Ali Çehreli: "Re: returning a vector from a funciton"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Nov 2004 12:33:51 -0800
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
"Michael" <slick_mick_00@hotmail.com> wrote in message
news:coa5nv$n4$1@titan.btinternet.com...
> 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
>
>
> "Ali Cehreli" <acehreli@yahoo.com> wrote in message
> news:pan.2004.11.27.11.27.54.421113@yahoo.com...
>> > 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
>>
>
>
- Next message: Peter Koch Larsen: "Re: find number of digits"
- Previous message: Jonathan Mcdougall: "Re: C/C++ undefinded symbol problem"
- Maybe in reply to: Affan Syed: "returning a vector from a funciton"
- Next in thread: Ali Çehreli: "Re: returning a vector from a funciton"
- Reply: Ali Çehreli: "Re: returning a vector from a funciton"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|