Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

arrays

vector

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 its elements be default constructible (vs copy (C++03) or move (C++0x) constructible)
  • is linearly swappable (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.


Same reasoning when using a C-style static array rather than a std::vector. And for that, I kindly refer you to here.


std::array has a fixed (compile time) size, while std::vector can grow.

As such, std::array is like using a C array, while std::vector is like dynamically allocating memory.


I use my own personal hand coded Array<> template class, which has a simpler API compared with std::array or std::vector. For example:

To use a dynamic Array:

Array<>  myDynamicArray; // Note array size is not given at compile time
myDynamicArray.resize(N); // N is a run time value

...

To use a static Array, fixed size at compile time:

Array<100> myFixedArry;

I believe it has a better syntax than std::array, or std::vector. Also extremely efficient.