简体   繁体   English

迭代器与 STL 容器的关系

[英]Relationship of iterators and STL containers

I understand the general idea behind iterators and I can use them at a basic level, but I don't get what happens under the hood and the relationship they have with STL containers.我了解迭代器背后的一般概念,并且可以在基本级别上使用它们,但我不明白幕后发生的事情以及它们与 STL 容器的关系。

From reading http://www.cplusplus.com/reference/iterator/iterator/ :从阅读http://www.cplusplus.com/reference/iterator/iterator/

  • Iterators are objects defined with a class template迭代器是使用 class 模板定义的对象
  • "Iterators are classified into five categories depending on the functionality they implement" “迭代器根据它们实现的功能分为五类”

  • There are different types of iterators (input, output, forward, bidirectional, random access)有不同类型的迭代器(输入、output、正向、双向、随机访问)

My assumptions are (I will be using vector throughout as an example)我的假设是(我将在整个过程中使用vector作为示例)

  • In STL containers, for example vector , the iterator is a nested class template and is created for each unique vector type.在 STL 容器中,例如vector ,迭代器是一个嵌套的 class 模板,是为每个唯一的vector类型创建的。
  • An iterator being considered to be one particular type of iterators is just a concept because it ultimately depends on what is implemented as member functions in the vector class.被认为是一种特定类型的迭代器的迭代器只是一个概念,因为它最终取决于vector class 中作为成员函数实现的内容。
    • My reasoning is that for example: std::list<int>::iterator iterator;我的理由是,例如: std::list<int>::iterator iterator; which is a bidirectional iterator vs std::vector<int>::iterator iterator;这是一个双向迭代器 vs std::vector<int>::iterator iterator; which is a random access iterator are both declared with ::iterator and there is no distinction made.这是一个随机访问迭代器,都用::iterator声明,并且没有区别。
  • Functions like begin() and end() are overloaded in the vector classbegin()end()这样的函数在vector class 中被重载

I hope I am making sense, please correct me.我希望我有道理,请纠正我。

Let's go point by point.让我们逐点介绍 go。

Iterators are objects defined with a class template迭代器是使用 class 模板定义的对象

Not necessarily.不必要。 Iterators are objects that have certain operations .迭代器是具有某些操作的对象。 Pointers are such objects, as are objects of various class types.指针就是这样的对象,各种 class 类型的对象也是如此。

Each container defines a member type container::iterator , and another member type container::const_iterator .每个容器定义一个成员类型container::iterator和另一个成员类型container::const_iterator

These can be directly (a nested class), or it could be with a type alias referring to some other typename.这些可以是直接的(嵌套类),也可以是引用其他类型名的类型别名。

Iterators are classified into five categories depending on the functionality they implement迭代器根据它们实现的功能分为五类

As of C++14, yes.从 C++14 开始,是的。 C++17 and C++20 each introduce another category. C++17 和 C++20 分别介绍了另一个类别。

There are different types of iterators (input, output, forward, bidirectional, random access)有不同类型的迭代器(输入、output、正向、双向、随机访问)

Those are the (C++14) categories, but there are an infinity of types within each category.这些是 (C++14) 类别,但每个类别中有无限种类型 std::vector<int>::iterator is a random access iterator, and so is double * , but they are distinct types. std::vector<int>::iterator是随机访问迭代器, double *也是如此,但它们是不同的类型。 These categories overlap, the definitions are in terms of previous ones in the hierachy.这些类别重叠,定义是根据层次结构中的先前类别。 RandomAccess is defined as an extension to Bidirectional , which is defined as an extension to Forward , which is defined as an extension to Input . RandomAccess被定义为 Bidirectional 的扩展, Bidirectional被定义为 Forward 的扩展, Forward被定义为Input的扩展。

In STL containers, for example vector , the iterator is a nested class template and is created for each unique vector type.在 STL 容器中,例如vectoriterator是一个嵌套的 class 模板,是为每个唯一的向量类型创建的。

Mostly.大多。 In many implementations it is, but in the particular case of std::vector<T> , no rule stops an implementation from using T * as the iterator type.在许多实现中是这样,但在std::vector<T>的特定情况下,没有规则阻止实现使用T *作为iterator类型。

An iterator being considered to be one particular type of iterators is just a concept because it ultimately depends on what is implemented as member functions in the vector class.被认为是一种特定类型的迭代器的迭代器只是一个概念,因为它最终取决于向量 class 中作为成员函数实现的内容。

Yes.是的。 C++ has a notion Concept , which are just labels for things that behave in similar ways. C++ 有一个概念Concept ,它只是行为相似的事物的标签。 Because of how templates work, there does not need to be any relationship between the types that satisfy a particular Concept.由于模板的工作方式,满足特定概念的类型之间不需要任何关系。 Contrast Java and C#, where interface s have to be explicitly mentioned in the definition of a type.对比 Java 和 C#,其中interface必须在类型定义中明确提及。

Functions like begin() and end() are overloaded in the vector class像 begin() 和 end() 这样的函数在向量 class 中被重载

This is literally true, but probably not what you mean.这确实是真的,但可能不是你的意思。 Each container does have two member functions named begin .每个容器都有两个名为begin的成员函数。 container::iterator container::begin() and container::const_iterator container::begin() const . container::iterator container::begin()container::const_iterator container::begin() const

There is also the free function template std::begin , which is specialised for each container and also (C style) arrays.还有免费的 function 模板std::begin ,它专门用于每个容器以及(C 风格)arrays。

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

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