Thread: Vectors + Classes = Confusing

  1. #31
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Q: so is there any major advantage from using the vector class vs. using your own resizable dynamic array... (other than having prewritten/readily available code)
    Last edited by The Brain; 12-15-2004 at 05:11 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #32
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    A: Yes there is.
    Last edited by Thantos; 12-15-2004 at 05:21 PM.

  3. #33
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #34
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sure change the question on me after I already answered. Of course the answer is still the same. Now can you guess what the smarter question would have been?

  5. #35
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    where is the love?
    Last edited by The Brain; 12-15-2004 at 07:23 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #36
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Chances are good that your STL vector implementation has been tested and used by thousands of programmers, versus your own dynamic array, which is likely to be much less robust.

    The STL vector is so common that anybody who looks at, uses or modifies your code will very likely understand it from the beginning, versus your own dynamic array, which they will have to learn.

    The people who implemented the STL vector are most likely better at what they do than you are, so their version is probably safer, faster, and more robust than yours.

    The only potential major advantage I can think of for using your own version would be if there were specific optimizations that you could implement that could not be implemented with a vector and create a significant performance gain for your app.

  7. #37
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    The only potential major advantage I can think of for using your own version would be if there were specific optimizations that you could implement that could not be implemented with a vector and create a significant performance gain for your app.
    I was thinking that there would potentially be a great advantage from making your dynamic array application specific.. since vectors typically allocate memory exponentially.. and from an optimization perspective.. greatly enhance execution time by eliminating the inclusion of unecessary libraries.. (thus eliminating about a bazillion functions you would never use)
    Last edited by The Brain; 12-15-2004 at 05:59 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #38
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by The Brain
    I was thinking that there would potentially be a great advantage from making your dynamic array application specific.. since vectors typically allocate memory exponentially.. and from an optimization perspective.. greatly enhance execution time by eliminating the inclusion of unecessary libraries.. (thus eliminating about a bazillion functions you would never use)
    You can customize the way a vector (or any other STL container) allocates memory. I have done this before and I am pretty sure it would be easier than writing your own complete dynamic array solution.

    Execution time shouldn't be affected by the number of libraries or functions you use. Maybe executable size, but the difference would be negligible I would think, and they would certainly not outweigh the advantages I mentioned above.

  9. #39
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by The Brain
    greatly enhance execution time by eliminating the inclusion of unecessary libraries.. (thus eliminating about a bazillion functions you would never use)
    Except for possibly taking a little longer to load the executable into memory, having all those extra functions that are never used would not impact performance speed of the program at all. As for optimizing the functions that ARE called, the likelihood that you will be able to write more efficient functions than the hundreds/thousands/lots of people who have already given input on them is not very high, except in the case where you will *ONLY* be using a specialized subset of what the container will do for very specific purposes that could not possibly have been anticipated for a general robust container class.

  10. #40
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Well this is what I am thinking....

    vector memory allocation:

    |---|-----|--------|------------------|-------------------------------|----------------------------------------------------------------|


    vs. a fixed incremental memory allocation:

    |--------------|--------------|--------------|--------------|--------------|--------------|



    in just 6 memory allocations, one can see a significant reduction in allocated memory.. I think one has to weigh the cost between memory allocation vs. execution time to resize the array. Is there a right or wrong anwer? yes. The answer is application specific.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #41
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You can customize the allocation for a vector... it can be pretty much however you want it.
    Last edited by jlou; 12-15-2004 at 06:37 PM.

  12. #42
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    is STL still STL if it has been 'enhanced' by the programmer
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  13. #43
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Umm, yes, very much so. STL was designed to be enhancable by the programmer. It is a feature (which happens to be a major advantage over most in-house implementations ).

  14. #44
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    STL was designed to be enhancable by the programmer.
    so standard template library was designed to become non-standard.. at least at the user level.. (which is cool I think as the programmer annotates the change) thanks for the intellectual discussion everyone. It makes my brain happy.
    Last edited by The Brain; 12-15-2004 at 07:39 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  15. #45
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I think one has to weigh the cost between memory allocation vs. execution time to resize the array.
    I'd just like to point out, typically there will be far more memory for you to waste than execution time, especially when writing games

    so standard template library was designed to become non-standard.. at least at the user level..
    It isn't nonstandard, you're just not using the defaults As far as I know, the difference is just a call to a mode-switching function.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 07:19 AM
  2. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 04:19 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05:11 AM
  4. vectors and classes
    By jimothygu in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 07:53 PM
  5. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 10:04 PM