xmllmx
xmllmx

Reputation: 42235

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?

Upvotes: 1

Views: 668

Answers (2)

eerorika
eerorika

Reputation: 238311

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.

Upvotes: 6

cpplearner
cpplearner

Reputation: 15813

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 [...].

And the page of ContiguousContainer says:

The type X satisfies ContiguousContainer if

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

Upvotes: 4

Related Questions