| | |
lists and vectors
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
So in situations like these, pass the object by reference, so that
The second version takes a constant amount of time.
C Syntax (Toggle Plain Text)
int foo(vector<int> x) { return x.back() - x.front(); }
So in situations like these, pass the object by reference, so that
x refers to caller's vector. C Syntax (Toggle Plain Text)
int foo(vector<int>& x) { return x.back() - x.front(); }
The second version takes a constant amount of time.
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- MERGING LISTS need help with this one (C++)
- Vectors, Functions, and Sorting... oh my! (C++)
- C/ Need Help with Linked Lists please (C)
- making sorted lists (was: Help Me!) (C)
- stack of linked lists (C++)
Other Threads in the C Forum
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






