简体   繁体   English

为什么 std::vector::iterator 不是连续迭代器?

[英]Why is std::vector::iterator not contiguous iterator?

According to the cppref page of std::vector :根据std::vectorcppref 页面

iterator LegacyRandomAccessIterator iterator LegacyRandomAccessIterator

Also from another cppref page :同样来自另一个cppref 页面

The following standard library types are LegacyContiguousIterators :以下标准库类型是LegacyContiguousIterators

vector::iterator for value_type other than bool.用于 value_type 的vector::iterator ,而不是 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.除了 bool 的 vector 之外,所有的向量迭代器都是连续的迭代器。 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.前一个声明根本没有提供尽可能多的信息,并且可能没有更新,因为 c++17 的变化引入了连续迭代器要求的名称。

std::vector<T>::iterator (for T other than bool ) is a contiguous iterator. std::vector<T>::iterator (对于bool以外的T一个连续的迭代器。

[vector.overview]/2 : [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. vector满足容器的所有要求,以及 [...] 对于连续容器的bool以外的元素类型的所有要求。

[container.requirements.general]/13 (emphasis mine): [container.requirements.general]/13 (强调我的):

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]).连续容器是其成员类型 iterator 和 const_iterator 满足 Cpp17RandomAccessIterator 要求 ([random.access.iterators]) 和model contiguous_iterator ([iterator.concept.contiguous]) 的容器。


According to the cppref page of std::vector :根据std::vectorcppref 页面

iterator LegacyRandomAccessIterator 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 [...]. std::vector (对于T不是bool )满足 [...] ContiguousContainer [...] 的要求。

Andthe page of ContiguousContainer says: ContiguousContainer 的页面说:

The type X satisfies ContiguousContainer if类型X满足ContiguousContainer如果

  • [...] [...]
  • The member types X::iterator and X::const_iterator are LegacyContiguousIterators成员类型X::iteratorX::const_iteratorLegacyContiguousIterators

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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