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?
Asked
Active
Viewed 101 times
0
SomeWittyUsername
- 17,608
- 3
- 41
- 82
JTK
- 15
- 3
-
Yes. It will reallocate the elements if they won't fit in the currently allocated space. – wally Oct 25 '16 at 20:04
-
this is called contiguous storage, and there are two standard containers that guarantee it: vector and array. In C++17 they'll add an iterator with this guarantee: ContiguousIterator. – jaggedSpire Oct 25 '16 at 20:04
-
That kind of storage allows you to retrieve elements in such data structures in O(1) complexity (very fast). – Spidey Oct 25 '16 at 20:06
-
@Spidey It also happens to be C compatible (by passing the pointer retrieved from .data()) – Borgleader Oct 25 '16 at 20:07
1 Answers
0
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.
abelenky
- 61,055
- 22
- 102
- 154