Thread: The difference between vectors and arrays

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    The difference between vectors and arrays

    So what is the difference between these types? Performance, ineffiency, what?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    106
    Basically vectors are dynamic arrays which mean you can add or delete contents. With arrays you have to manage memory, vectors does this for you... Um basically you want to use vectors when you are going to either be deleting or adding items to the end. Use an array when you want to just store a set number of values, also if you're looking to add or delete items in the the middle of an array I believe it's more efficient to use lists (deque?) I'm not 100% postive about this because I myself am still learning.
    Google returns a lot of info for this..

    when to use each:
    set number of data ----> array
    dynamic number of data, adding/deleting at end -----> vector
    dynamic number of data, adding/deleting in middle ---> lists

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A vector is a dynamic array. An array is simply a static array. It has a fixed amount of elements. A vector will expand its size as necessary to accommodate new elements.
    Vectors are generally your general choice container, unless you have special need.
    If you're going to add/delete at the beginning, a deque might be good.
    If you're going to add/delete in the middle, a linked list may be good.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think one rather important difference is that a vector is a dynamically allocated array, that can be partially uninitialized (unused memory is not initialized).

    When you allocate an array with new X[n] then exactly n objects will be created, no more, no less.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

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