简体   繁体   中英

Why is std::vector::iterator not contiguous iterator?

According to the cppref page of std::vector :

iterator LegacyRandomAccessIterator

Also from another cppref page :

The following standard library types are LegacyContiguousIterators :

vector::iterator for value_type other than bool.

Which is correct?

Both are correct. All vector iterators are random access iterators. All vector iterators except those of vector of bool are contiguous iterators. Note that all contiguous iterators are also random access iterators.

The former claim does simply not provide as much information, and may have not been updated since changes from c++17 which introduced the name for for the requirements of contiguous iterator.

std::vector<T>::iterator (for T other than bool ) is a contiguous iterator.

[vector.overview]/2 :

A vector meets all of the requirements of a container and [...] for an element type other than bool , of a contiguous container.

[container.requirements.general]/13 (emphasis mine):

A contiguous container is a container whose member types iterator and const_iterator meet the Cpp17RandomAccessIterator requirements ([random.access.iterators]) and model contiguous_iterator ([iterator.concept.contiguous]).


According to the cppref page of std::vector :

iterator LegacyRandomAccessIterator

It doesn't mean that the iterator is not a contiguous iterator. It's not even talking about contiguous iterator.

The same page also mentions:

std::vector (for T other than bool ) meets the requirements of [...] ContiguousContainer [...].

Andthe page of ContiguousContainer says:

The type X satisfies ContiguousContainer if

  • [...]
  • The member types X::iterator and X::const_iterator are LegacyContiguousIterators

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM