JTK JTK - 1 month ago 8
C++ Question

In C++, does a vector occupy successive memory slots?

If I define

vector<double> v;
does this vector occupy successive memory slots? If it occupies successive memory slots, what would happen if I add some new element to this vector while there is not enough memory slots right after the last element?

Answer

http://en.cppreference.com/w/cpp/container/vector

The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements.

Comments