std::vector and contiguous memory of multidimensional arrays

后端 未结 6 1818
青春惊慌失措
青春惊慌失措 2020-11-27 07:42

I know that the standard does not force std::vector to allocate contiguous memory blocks, but all implementations obey this nevertheless.

Suppo

6条回答
  •  孤独总比滥情好
    2020-11-27 07:58

    Elements of a Vector are gauranteed to be contiguous as per C++ standard.
    Quotes from the standard are as follows:

    From n2798 (draft of C++0x):

    23.2.6 Class template vector [vector]

    1 A vector is a sequence container 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().

    C++03 standard (23.2.4.1):

    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().

    Also, see here what Herb Sutter's views on the same.

提交回复
热议问题