Arrays vs Vectors: Introductory Similarities and Differences
I'll add that arrays are very low-level constructs in C++ and you should try to stay away from them as much as possible when "learning the ropes" -- even Bjarne Stroustrup recommends this (he's the designer of C++).
Vectors come very close to the same performance as arrays, but with a great many conveniences and safety features. You'll probably start using arrays when interfacing with API's that deal with raw arrays, or when building your own collections.
arrays:
- are a builtin language construct;
- come almost unmodified from C89;
- provide just a contiguous, indexable sequence of elements; no bells and whistles;
- are of fixed size; you can't resize an array in C++ (unless it's an array of POD and it's allocated with
malloc
); - their size must be a compile-time constant unless they are allocated dynamically;
- they take their storage space depending from the scope where you declare them;
- if dynamically allocated, you must explicitly deallocate them;
- if they are dynamically allocated, you just get a pointer, and you can't determine their size; otherwise, you can use
sizeof
(hence the common idiomsizeof(arr)/sizeof(*arr)
, that however fails silently when used inadvertently on a pointer); - automatically decay to a pointers in most situations; in particular, this happens when passing them to a function, which usually requires passing a separate parameter for their size;
- can't be returned from a function; (Unless it is std::array)
- can't be copied/assigned directly;
- dynamical arrays of objects require a default constructor, since all their elements must be constructed first;
std::vector
:
- is a template class;
- is a C++ only construct;
- is implemented as a dynamic array;
- grows and shrinks dynamically;
- automatically manage their memory, which is freed on destruction;
- can be passed to/returned from functions (by value);
- can be copied/assigned (this performs a deep copy of all the stored elements);
- doesn't decay to pointers, but you can explicitly get a pointer to their data (
&vec[0]
is guaranteed to work as expected); - always brings along with the internal dynamic array its size (how many elements are currently stored) and capacity (how many elements can be stored in the currently allocated block);
- the internal dynamic array is not allocated inside the object itself (which just contains a few "bookkeeping" fields), but is allocated dynamically by the allocator specified in the relevant template parameter; the default one gets the memory from the freestore (the so-called heap), independently from how where the actual object is allocated;
- for this reason, they may be less efficient than "regular" arrays for small, short-lived, local arrays;
- when reallocating, the objects are copied (moved, in C++11);
- does not require a default constructor for the objects being stored;
- is better integrated with the rest of the so-called STL (it provides the
begin()
/end()
methods, the usual STLtypedef
s, ...)
Also consider the "modern alternative" to arrays - std::array
; I already described in another answer the difference between std::vector
and std::array
, you may want to have a look at it.