RandomAccessIterator 是否暗示数据在内存中是连续的?
Does RandomAccessIterator imply that the data is continuous in memory?
其他迭代器类型肯定不暗示它们指向连续数据,但我想知道我是否可以将 RandomAccessIterators 视为指向连续数据缓冲区——即它们可以转换为指向数据的指针。
这个假设是否正确?如果 it
是一个 RandomAccessIterator,我能否始终安全地使用 &*it
并获得一个指针,不仅指向一个元素,而且指向一个连续的缓冲区?
不,这不是一个有效的假设。标准库本身在std::deque
中有一个反例:
As opposed to std::vector
, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays.
std::deque
的iterator
是一个RandomAccessIterator:
A deque is a sequence container that, like a vector ([vector]), supports random access iterators.
如果你想要连续内存的保证,你不需要等待很长时间:保证将由 C++17 中的 ContiguousIterator 提供。1
ContiguousIterator 概念是在以 N4284, and adopted in November 2014
结尾的文档序列中提出的
其他迭代器类型肯定不暗示它们指向连续数据,但我想知道我是否可以将 RandomAccessIterators 视为指向连续数据缓冲区——即它们可以转换为指向数据的指针。
这个假设是否正确?如果 it
是一个 RandomAccessIterator,我能否始终安全地使用 &*it
并获得一个指针,不仅指向一个元素,而且指向一个连续的缓冲区?
不,这不是一个有效的假设。标准库本身在std::deque
中有一个反例:
As opposed to
std::vector
, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays.
std::deque
的iterator
是一个RandomAccessIterator:
A deque is a sequence container that, like a vector ([vector]), supports random access iterators.
如果你想要连续内存的保证,你不需要等待很长时间:保证将由 C++17 中的 ContiguousIterator 提供。1
ContiguousIterator 概念是在以 N4284, and adopted in November 2014
结尾的文档序列中提出的