简体   繁体   English

std :: vector迭代器类型和允许的操作

[英]std::vector iterator type and permissible operations

C++ named requirements: ContiguousIterator refers to the type of an iterator to std::vector as contiguous. 以C ++命名的需求:ContiguousIteratorstd::vector的迭代器类型称为连续的。 But there is no definition provided for the type contiguous iterator here . 但没有提供该类型的连续迭代器定义在这里

std::vector::begin refers to the iterator type as random access iterator. std :: vector :: begin将迭代器类型称为随机访问迭代器。

Does this imply that contiguous iterator is of type random access? 这是否意味着连续的迭代器属于随机访问类型?

[a] contiguous iterator is of type random access? [a]连续迭代器是随机访问类型的吗?

Yes. 是。

A "contiguous iterator" is defined (see N3884 ) as 将“连续迭代器”定义为(参见N3884 )为

a random access iterator that also meets the following requirements: 满足以下要求的随机访问迭代器:

std::pointer_from(i) == std::addressof(*i) (when i is dereferenceable) std::pointer_from(i) == std::addressof(*i) (当i可取消引用时)

std::pointer_from(i + n) == std::pointer_from(i) + n (when i + n is a valid iterator) std::pointer_from(i + n) == std::pointer_from(i) + n (当i + n是有效的迭代器时)

So 所以

  • "contiguous iterator" imply "random access iterator" “连续迭代器”意味着“随机访问迭代器”

  • "random access iterator" doesn't imply "contiguous iterator" (see std::deque for a counterexample) “随机访问迭代器”并不意味着“连续迭代器”(有关反例,请参见std::deque

Yup. 对。 cppreference has a nice chart , that makes it clear that ContiguousIterator encompasses a superset of the features of RandomAccessIterator (which is itself a superset of BidirectionalIterator , which is a superset of ForwardIterator , etc.). cppreference有一个漂亮的图表 ,可以很清楚地看到ContiguousIterator包含RandomAccessIterator功能的超集(它本身是BidirectionalIterator的超集,它是ForwardIterator的超集等)。

CPPReference is leading you astray by portraying an iterator category and concept ContiguousIterator that do not appear in the C++ Standard. CPPReference被刻画迭代器种类和概念带坏你ContiguousIterator没有出现在C ++标准。 C++17 defines contiguity as a property of iterators, much like mutability. C ++ 17将连续性定义为迭代器的属性,就像可变性一样。 [iterator.requirements.general]/6 : [iterator.requirements.general] / 6

Iterators that further satisfy the requirement that, for integral values n and dereferenceable iterator values a and (a + n) , *(a + n) is equivalent to *(addressof(*a) + n) , are called contiguous iterators . 对于整数值n和可引用的迭代器值a(a + n)*(a + n)等于*(addressof(*a) + n)的要求进一步被称为连续迭代器

Notably this property is independent of iterator category. 值得注意的是,此属性与迭代器类别无关。 In theory, you could define an iterator that satisfies the contiguous iterator requirements and the requirements of any of the iterator categories. 从理论上讲,您可以定义一个满足连续迭代器要求和任何迭代器类别要求的迭代器。

In practice, I don't think this flexibility provides any expressiveness over a design in which contiguous iterators are a refinement of random access iterators. 实际上,我不认为这种灵活性比连续迭代器是对随机访问迭代器的改进的设计具有任何表现力。 In fact the standard library container requirements define contiguous container as ( [container.requirements]/13 ): 实际上,标准库容器要求将连续容器定义为( [container.requirements] / 13 ):

A contiguous container is a container that supports random access iterators and whose member types iterator and const_iterator are contiguous iterators. 连续容器是支持随机访问迭代器的容器,其成员类型iteratorconst_iterator是连续迭代器。

which does not precisely contradict [iterator.requirements.general]/6's notion that contiguity is independent of iterator category, but it does introduce an inconsistency that helps to cause confusion. 这并不与[iterator.requirements.general] / 6的观点相矛盾,即连续性独立于迭代器类别,但确实引入了一个不一致,这会引起混乱。

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

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