2009年9月2日

Memory Continuity of "std::vector"

寫了那麼久的C++, 一直無法確定std::vector的元素(element)到底有沒有被置放在連續的記憶體(memory)上。

Ogre的Forum上有人給了解答, 摘自C++ 2003 standard:

"23.2.4 Class template vector [lib.vector]
1 A vector is a kind of sequence that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."
Publisher: Unknown - 星期三, 9月 02, 2009