1

The way I see this, they both have the same function except std::vector seems more flexible, so when would I need to use array, and could I use std::vector only? This is not a new question, the original questions didn't have the answers I was looking for

Lendrit Ibrahimi
  • 157
  • 1
  • 12
  • If you need a "dynamic" array, then [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) is the natural solution. It should in general be the default container for everything. But if you want a statically sized array created at time of compilation (like a C-style array is) but wrapped in a nice C++ object then [`std::array`](http://en.cppreference.com/w/cpp/container/array) might be a better choice. – Some programmer dude Mar 05 '17 at 18:29
  • It's sensible to use an array (or `std::array`) when you don't need the flexibility of `std::vector`. – molbdnilo Mar 05 '17 at 18:33
  • Dynamic memory, heap? – Lendrit Ibrahimi Mar 05 '17 at 18:34
  • There's very little reason to use an old-fashioned array in C++, unless you have very stringent performance requirements (although a good compiler should be able to produce comparable code), or you're interfacing with a C API (although there are `std::array` and `std::vector` methods that will return a pointer to the underlying C array). – Barmar Mar 05 '17 at 18:38

3 Answers3

6

One interesting thing to note is that while iterators will be invalidated in many functions with vectors, that is not the case with arrays. Note: std::swap with std::array the iterator will still point to the same spot.

See more: http://en.cppreference.com/w/cpp/container/array

Good summary of advantages of arrays: https://stackoverflow.com/a/4004027/7537900

This point seemed most interesting:

fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed

Not having tested that, I'm not sure it's actually true though.

Here is a discussion in regards to 2D Vectors vs Arrays in regards to the competitive programming in Code Chef: https://discuss.codechef.com/questions/49278/whether-to-use-arrays-or-vectors-in-c

Apparently memory is not contiguous in 2 dimensions in 2D vectors, only one dimension, however in 2D arrays it is.

Community
  • 1
  • 1
3

As a rule of thumb, you should use:

  • a std::array if the size in fixed at compile time
  • a std::vector is the size is not fixed at compile time
  • a pointer on the address of their first element is you need low level access
  • a raw array if you are implementing a (non standard) container

Standard containers have the ability to know their size even when you pass them to other function, what raw arrays don't, and have enough goodies to never use raw arrays in C++ code without specific reasons. One could be a bottleneck that would require low level optimization, but only after profiling to identify the bottleneck. And you should benchmark in real condition whether the standard containers actually add any overload.

The only good reason I can think of is if you implement a special container. As standard containers are not meant to be derived, you have only two choices, either have you class contain a standard container and end in a container containing a container with delegations everywhere, or mimic a standard container (by copying code from a well knows implementation), and specialize it. In that case, you will find yourself managing directly raw arrays.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

When using std:vector, the only performance hit would be when the capacity is reached, as the memory must be relocated to accomodate a larger number of objects in contiguous memory space on the heap

Thus here is a summary of both in regards to flexibility and performance:

std::array; Reallocation is not possible and thus no perfomance hit will occur due to relocation of memory on the heap.

std::vector; Only affects performance if capacity is exceeded and reallocation occurs. You can use reserve(size) to provide a rough estimate to the maximum amount of objects you'll need. This allows greater flexibility compared to std::array but will of course, have to reallocate memory if the reserved space is exceeded.