简体   繁体   English

理解迭代器/const_iterator 实现

[英]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 makes std::vector<double> a class. std::vector是 std 命名空间中的类模板,它使std::vector<double>成为一个类。

std::vector<T>::iterator is a nested type under std::vector<T> std::vector<T>::iteratorstd::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 says vector::iterator or vector::const_iterator have any particular relationship to std::iterator (even in standards predating deprecation of std::iterator ). std::vector的规范中没有任何内容表明vector::iteratorvector::const_iteratorstd::iterator有任何特殊关系(即使在std::iterator弃用之前的std::iterator )。 Also, although iterator and const_iterator are types declared in the scope of vector , there is no requirement that vector have a member of either type - iterator and const_iterator are part of the interface of std::vector eg overloads of the member begin() returns those types, but nothing is said about how those function obtain the iterator they return此外,虽然iteratorconst_iterator是在vector范围内声明的类型,但不要求vector具有任一类型的成员 - iteratorconst_iteratorstd::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 容器定义了那些成员类型( iteratorconst_iterator等),通常具有某些迭代器类别的类型别名,例如LegacyRandomAccessIterator ,其定义为“指向数组元素的指针满足LegacyRandomAccessIterator所有要求”。

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

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