Besides the fact that the standard defines it to be contiguous, why is std::vector contiguous?
If it runs out of space, it needs to reallocate a new block and copy t
The standard C++ library defines a non-contiguous array-like container, too: std::deque. Iteration over a std::deque is much slower than iterating over a std::vector. If the operation is fairly trivial, it may be something like 5 times slower: these are actual times I get when comparing accumulating a sequence of integers. This is the cost you are paying for a non-contiguous representation!
The reason for this fairly steep slowdown is that gcc knows how to vectorize the loop over a std::vector but not for a std::deque. Even without vectorization the iteration is about 30% slower. That is, the fairly small cost of std::vector's re-allocations actually don't really matter that much!