lists and vectors

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

lists and vectors

 
0
  #1
May 21st, 2006
hello,
i have a few questions pls help me out with them coz am really confused ..

1. can lists or vectors passed as parameters in a function?
2. can the lists or vectors be returned ?

i want to use lists or vectors in classes i need to knw if this is possible .
pls help me out
thankx.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: lists and vectors

 
0
  #2
May 21st, 2006
1. can lists or vectors passed as parameters in a function?
Yes
2. can the lists or vectors be returned ?
Yes
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: lists and vectors

 
2
  #3
May 22nd, 2006
Beware of the performance costs, though. If you just pass a vector, as in the code below, you'll end up copying the entire vector, which takes an amount of time proportional to the length of the vector. Unless the compiler is unreasonably intelligent, which is never the case.

  1. int foo(vector<int> x) {
  2. return x.back() - x.front();
  3. }

So in situations like these, pass the object by reference, so that x refers to caller's vector.

  1. int foo(vector<int>& x) {
  2. return x.back() - x.front();
  3. }

The second version takes a constant amount of time.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

Re: lists and vectors

 
0
  #4
May 23rd, 2006
thankx ... am doing a project for school which involves a lot of file operations i wantedto avoid using an intermediate file as a buffer so thought of using lists/vectors which will be faster and easier .. but the cost overhead is higher but so is using the files so evens it out i think ...
thaknx
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,704
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: lists and vectors

 
0
  #5
May 23rd, 2006
Assuming you have enough memory, passing a list/vector should be faster than file access for the same information. Passing by reference does expose the material being passed to being changed, so remember to use the keyword const judiciously if you don't want that to happen.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC