Array and the vector

This is a discussion on Array and the vector within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Hi, I recently read in a book i have that it is no good learning about arrays, and stick with ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Array and the vector

    Hi, I recently read in a book i have that it is no good learning about arrays, and stick with and always use vectors. I know how to use both very well, and in general use vectors over arrays anyeay.

    My question is this:

    Why would people have to or want to learn about C-style arrays

    eg:
    Code:
    int num[10]={1,2,3,4 ect.. };[
    When vectors can do all the above can and is faster and has more capabilitys for sorting through a sequence like iterators?

    Many books teach arrays and vectors, but I think they should stick with vectors and use arrays as an side learning subject. That is only my view mind.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    vectors are dynamic containers. A static array is often more efficient than a vector because of this. Also, there are some things a vector cannot do, like the initialization you showed in your example (although that might be available in the next standard). Finally, an array can be sorted just as easily as a vector can.

    So in the occasional situations when one needs the ultimate efficiency, an array is obviously the better choice. You might not run into these situations very often, which is why you are told to use vectors instead of arrays. But other programmers might be in different situations where using an array is the best solution.

    Learning about arrays is a good idea regardless of whether you use them specifically instead of vectors or not. They are a data structure and understanding of arrays and how they work can be very beneficial to your understanding of what you are programming.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Thanks daved. That was just the answer I was looking for.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Are vectors really faster than arrays? (or does efficiency mean speed?)
    Entia non sunt multiplicanda praeter necessitatem

  5. #5
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Quote Originally Posted by Clyde
    Are vectors really faster than arrays? (or does efficiency mean speed?)
    Yes. Arrays are just a simple block of memory, so finding the right spot is a lot faster than something with dynamic allocation, eg. linked lists (where you have to traverse through each node), or vectors.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    There's no reason arrays would be necessarily faster than vectors, except in startup time of the program. The expression v[i] can be compiled down to the exact same instructions no matter whether v is a T* or a vector<T>. Arrays can use one or two pointers' less memory if you don't want to grow them or if you have their size hard-coded, of course.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    The power that vectors provide far outweighs any performance issues they may create...if any.


    With my old space game I fired off 5000 lasers, tracked them per frame, render them in brute force with no culling and did all of this on an old GeForce 3 64MB card at well over 120 FPS with no vsync.

    I doubt vectors are going to cause any major problems unless you are deleting and inserting multiple times per cycle or frame.

    I use vectors for nearly all of my game resource managers and they work like a charm. Currently I'm getting over 900 FPS so I have plenty of frames there to work with.

    As to the question should you always use vectors over arrays my answer is it depends on the task and the need.
    That's like saying should you always use a certain tool over another when working on your car. This is ludicrous. Use the right tool for the job.

    Vectors are not always the right tool.
    Arrays are not always the right tool.

    This is why we need people called programmers. So my answer is BOTH AND.
    Last edited by VirtualAce; 11-19-2006 at 09:18 PM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    When vectors can do all the above can and is faster and has more capabilitys for sorting through a sequence like iterators?
    Be more careful about saying complex things are faster... because they aren't.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why would people have to or want to learn about C-style arrays
    Working with legacy code, interfacing with libraries that use them, and writing code where vectors could be impractical. That last one could pop up on platforms where the overhead is just too much, or in critical applications that can't afford allocation errors.

    >When vectors can do all the above can and is faster and has more capabilitys for sorting through a sequence like iterators?
    It's trivial to write a container class for an array. The boost library has one, and then it can be used with the rest of the standard library in a syntactically consistent way.
    My best code is written with the delete key.

  10. #10
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    I don't quite see what the kerfuffle is about (although as I don't currently program in C++ maybe I'm not class-centric enough).

    You have an array of bytes. If you were to make a vector of it you'd split it up into a singular byte and a pointer to the next item (and these are usually 4 bytes on a 32-bit platform).

    Isn't the inefficiency obvious?

    You can view an array as a special contigious case of a vector whereby the next item is implicitly at the next memory address, elminating the need for a pointer.

    If the future of programming is to have everything in vectors, I think I'll just calmly walk out back and commit suicide, ta.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Isn't the inefficiency obvious?
    Not really. You're confusing vectors and lists. A vector is required to be stored in contiguous space, so the implementation is pretty much going to be a dynamic array. How else would we be able to do &v[0] and use it as an array of T?
    My best code is written with the delete key.

  12. #12
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    Oh right. Like I said, no experience of C++ so aside from cursory glances of things the only vector I recognise is the one with members x, y (and z).

    So you're saying it uses realloc under all that shiz?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So you're saying it uses realloc under all that shiz?
    It uses allocators, where the default allocator relies on new, so by default a vector is logically no different memory-wise from:
    Code:
    T *p = new T[N];
    My best code is written with the delete key.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    I'm not even sure there should be a discussion around vectors performance over arrays. For one, different libraries are free to implement them however they wish (despite probably they all being roughly the same).

    But most important is the fact STL containers are meant to provide us with specific data structure solutions out of the box. Not to replace standard structures and even less to try and outperform them; Vector is not a replacement for an array. It is an array wrapper (heck! probably it doesn't even need to be that).

    I'm inclined to believe (read, from Scott Meyers' books) that all things equal, vectors will match dynamic arrays performance for random access, but come short in terms of insertions and deletions. Concerning static arrays, vectors will outperform arrays for insertions and deletions and match them for random access.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm not even sure there should be a discussion around vectors performance over arrays.
    I am sure. We have our performance guarantees from the standard, and anything beyond that requires profiling for specific cases. No debate is necessary.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21