[英]Understanding iterator/const_iterator implementation
I know how to use iterators on a surface level, but I am trying to understand how iterators work in relation to a container class such as vector
.我知道如何在表面级别使用迭代器,但我试图了解迭代器如何与容器类(例如
vector
。
According to why do we put :: (scope resoulation operator) before iterator?根据为什么我们在迭代器之前放 ::(范围解析运算符)?
std::vector
is a class template in the std namespace, which makesstd::vector<double>
a class.std::vector
是 std 命名空间中的类模板,它使std::vector<double>
成为一个类。
std::vector<T>::iterator
is a nested type understd::vector<T>
std::vector<T>::iterator
是std::vector<T>
下的嵌套类型
From what I understand, the class template vector
has a member of type iterator
which it gets from the class template in #include <iterator>
.据我所知,类模板
vector
有一个iterator
类型的成员,它从#include <iterator>
的类模板中获取。
This is confusing because when I look in http://www.cplusplus.com/reference/iterator/iterator/ there is no const_iterator
class template in #include <iterator>
that I can see?这令人困惑,因为当我查看http://www.cplusplus.com/reference/iterator/iterator/时,
#include <iterator>
没有我可以看到的const_iterator
类模板?
Gathered information from the comments:从评论中收集的信息:
std::iterator
from #include <iterator>
is deprecated as of C++17来自
#include <iterator>
std::iterator
从 C++17 开始被弃用
Before it was deprecated, it was possible for STL containers (which are implementation defined) to implemented iterators with the help of the std::iterator
template class.它被废弃之前,对STL容器(这是实现定义)来实现的迭代器与的帮助下是可能
std::iterator
模板类。
For vector
for example:以
vector
为例:
There is nothing in the specification of
std::vector
that saysvector::iterator
orvector::const_iterator
have any particular relationship tostd::iterator
(even in standards predating deprecation ofstd::iterator
).std::vector
的规范中没有任何内容表明vector::iterator
或vector::const_iterator
与std::iterator
有任何特殊关系(即使在std::iterator
弃用之前的std::iterator
)。 Also, althoughiterator
andconst_iterator
are types declared in the scope ofvector
, there is no requirement thatvector
have a member of either type -iterator
andconst_iterator
are part of the interface ofstd::vector
eg overloads of the memberbegin()
returns those types, but nothing is said about how those function obtain the iterator they return此外,虽然
iterator
和const_iterator
是在vector
范围内声明的类型,但不要求vector
具有任一类型的成员 -iterator
和const_iterator
是std::vector
接口的一部分,例如成员begin()
重载返回这些类型,但没有说明这些函数如何获得它们返回的迭代器
What is and isn't required in a STL container is: STL 容器中需要和不需要的是:
"a begin and end function that returns iterators (or raw pointers as that satisfies the requirements for an iterator). There is nothing that says the iterator needs to be implemented in the container."
“返回迭代器(或满足迭代器要求的原始指针)的开始和结束函数。没有任何内容表明迭代器需要在容器中实现。”
STL containers define those member types ( iterator
, const_iterator
, etc) usually with type alias for some iterator category like LegacyRandomAccessIterator
which defined as "A pointer to an element of an array satisfies all requirements of LegacyRandomAccessIterator
". STL 容器定义了那些成员类型(
iterator
、 const_iterator
等),通常具有某些迭代器类别的类型别名,例如LegacyRandomAccessIterator
,其定义为“指向数组元素的指针满足LegacyRandomAccessIterator
所有要求”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.