View Poll Results: Arrays of Vectors?

Voters
28. You may not vote on this poll
  • array

    3 10.71%
  • vector

    6 21.43%
  • depends on situation...

    19 67.86%

arrays or vectors

This is a discussion on arrays or vectors within the C++ Programming forums, part of the General Programming Boards category; Originally posted by Polymorphic OOP No, actually you're wrong here. A vector is usually implemented as an array, NOT a ...

  1. #16
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Polymorphic OOP
    No, actually you're wrong here. A vector is usually implemented as an array, NOT a linked list. they "expand" by reallocating and/or reserving memory. This I am sure of.
    hmmm, i was actually taught that way too (in java). but like i said above ( i think it was this thread), this guy from EA said the STL was evil because its ADT's are based on linked lists. either way, i prefer to write my own.

  2. #17
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Whats an array?!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #18
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Serious question: What are the advantages of using a linked list? When should one use a linked list instead of an array or vector? From what little I've seen, linked lists just look like a hassle to use.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #19
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by joshdick
    Serious question: What are the advantages of using a linked list? When should one use a linked list instead of an array or vector? From what little I've seen, linked lists just look like a hassle to use.
    linked list can save you on memory. however, you have to use sequential search. arrays and vectors can be searched with binary or random access. but at the same time, can take up more memory. it depends on what you are using it for and which you feel is the best to use in the situation.

  5. #20
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by alpha
    linked list can save you on memory...
    arrays and vectors ... can take up more memory
    You got it mixed up. linked lists will actually take up more memory than arrays because each node in a linked list has to have a pointer to at least one other node.

    The reasons you use a linked list is because you can add and remove elements (particularly adding and removing elements in the middle of the list) without having to reallocate any memory that has values which aren't changing, while with an array, you will many times have to completely reallocate more memory and copy all of the data over, and to remove an element in an array, you have to copy all the ones after it back one space.

    It's all about data access, creation, and removal -- are you going to be adding and removing a lot of something, more particularly, are you going to be adding and removing a lot of something in no predefined order. If so, then a linked list makes much more sense. An example would be a game with a bunch of enemies on the screen. You have no idea what order the player will be killing the enemies in, so you use a linked list making it more efficient to remove an element from the middle.

    You can usually use arrays as stacks or queues of predetermined maximum lenght. One perfect example I can give of this is "the stack" that you should be familiar with as a C++ programmer. If not, then go back to lesson 1

  6. #21
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Polymorphic OOP
    You got it mixed up. linked lists will actually take up more memory than arrays because each node in a linked list has to have a pointer to at least one other node.
    Thanks. That makes much more sense.

  7. #22
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You got it mixed up. linked lists will actually take up more memory than arrays because each node in a linked list has to have a pointer to at least one other node.
    Assuming the array is filled, then that's true.
    However, if you're having a variable amount of data, an array must have enough room for extra elements, while a linked list only allocates what it needs. This usually leads to sizeof(Array) > sizeof(LinkedList).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #23
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Magos
    Assuming the array is filled, then that's true.
    However, if you're having a variable amount of data, an array must have enough room for extra elements, while a linked list only allocates what it needs. This usually leads to sizeof(Array) > sizeof(LinkedList).
    that's true too. i think that's what i was thinking when i posted it. assuming you only have a few nodes and a big array, then the linked list should take up less memory. but assuming the same size for linked list and array, then array should take up less space.

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I just read this article and it says you should prefer deques over vectors:

    http://www.gotw.ca/publications/mill10.htm

  10. #25
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Should i consider myself sociaal outcast upon finding this discussion entertaining reading???

  11. #26
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by DirX
    Should i consider myself sociaal outcast upon finding this discussion entertaining reading???
    yes.

  12. #27
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    darn...... thats not good

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

Similar Threads

  1. C++ Vectors versus normal arrays
    By _izua_ in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2007, 02:59 AM
  2. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:06 AM
  3. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 12:11 PM
  4. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 09:10 AM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 03:45 PM

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