What is the difference between std::array and std::vector? When do you use one over other? [duplicate] What is the difference between std::array and std::vector? When do you use one over other? [duplicate] arrays arrays

What is the difference between std::array and std::vector? When do you use one over other? [duplicate]


std::array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.

std::vector is a small class containing pointers into the heap. (So when you allocate a std::vector, it always calls new.) They are slightly slower to access because those pointers have to be chased to get to the arrayed data... But in exchange for that, they can be resized and they only take a trivial amount of stack space no matter how large they are.

[edit]

As for when to use one over the other, honestly std::vector is almost always what you want. Creating large objects on the stack is generally frowned upon, and the extra level of indirection is usually irrelevant. (For example, if you iterate through all of the elements, the extra memory access only happens once at the start of the loop.)

The vector's elements are guaranteed to be contiguous, so you can pass &vec[0] to any function expecting a pointer to an array; e.g., C library routines. (As an aside, std::vector<char> buf(8192); is a great way to allocate a local buffer for calls to read/write or similar without directly invoking new.)

That said, the lack of that extra level of indirection, plus the compile-time constant size, can make std::array significantly faster for a very small array that gets created/destroyed/accessed a lot.

So my advice would be: Use std::vector unless (a) your profiler tells you that you have a problem and (b) the array is tiny.


I'm going to assume that you know that std::array is compile-time fixed in size, while std::vector is variable size. Also, I'll assume you know that std::array doesn't do dynamic allocation. So instead, I'll answer why you would use std::array instead of std::vector.

Have you ever found yourself doing this:

std::vector<SomeType> vecName(10);

And then you never actually increase the size of the std::vector? If so, then std::array is a good alternative.

But really, std::array (coupled with initializer lists) exists to make C-style arrays almost entirely worthless. They don't generally compete with std::vectors; they compete more with C-style arrays.

Think of it as the C++ committee doing their best to kill off almost all legitimate use of C-style arrays.


std::array

  • is an aggregate
  • is fixed-size
  • requires that itselements be default constructible (vscopy (C++03) or move (C++0x)constructible)
  • is linearlyswappable (vs constant time)
  • is linearly movable (vs constant time)
  • potentially pays one less indirection than std::vector

A good use case is when doing things 'close-to-the-metal', while keeping the niceties of C++ and keeping all the bad things of raw arrays out of the way.