注册 登录
LUPA开源社区 返回首页

fandaozhang的个人空间 http://www.lupaworld.com/?381944 [收藏] [复制] [分享] [RSS]

我的博客

什么是迭代器?

热度 1已有 2024 次阅读2011-6-13 11:06 |系统分类:IT技术|

What Is an Iterator?

什么是迭代器?

 

So, what's really going on with the syntax? If vector<int>::const_iterator can be a typedef for some other type, then what is the actual type of the iterator?

在语法上什么是真正的迭代器?如果vector<int>::const_iterator可以通过typedef定义为其他类型,那么迭代器的实际类型是什么?

 

The answer is shamelessly dodgy: it depends. As far as the standard containers go, the standard does not specify exactly what an iterator is.

回答是…(你懂的)。就连标准容器,也没有明确说迭代器到底是什么。

 

What it does say is that, for example, the class vector has to at least have a public typedef that looks like the following, where the token unspecified is a type that can be whatever the implementation wants:

他说的是,例如,vector类必须有一个如下的typedef(或者就是以下的写法是可以编译运行的),语句中的unspecified是任意想要实现的类型。

 

// In <vector>, within the declaration of vector  vector里可以如此声明

typedef unspecified iterator;

typedef unspecified const_iterator;

 

Your favorite standard library implementation can use any type it wants in place of unspecified, so long as that type meets the strict requirements for the operations it must provide as defined by the standard.

你那可爱的标准库实现能用任何类型去替换上面的unspecified,只要是可以对那个类型做这样的操作。

 

These requirements, apart from those I enumerated a moment ago, are dictated by the category of the iterator provided by the container.

容器提供的迭代器类型(前向、后向、随机等等)都满足这些条件,除了我之前列举的。

 

 

 

Categories are collections of requirements that describe the different forms of iterator, and they are the subjects of the next section. Allowing implementations to choose their iterator type permits flexibility to use different approaches in different libraries.

对迭代器的分类是针对不同的需求的,这是下一节的内容。

 

All categories satisfy the requirements defined by the iterator pattern, but, as I said, not literally. Table 1 explains how the iterators in the C++ standard library satisfy the requirements of the pattern.

所有分类都满足迭代器模式的定义,但是,正如我说,并不全是。表1展示了C++标准库是如何满足迭代器模式的。

 

Table 1. How C++ meets iterator requirements

 

Requirement

需求

C++ Manifestation  C++的实现

Move to the beginning of the range

移动到区间的开始

All standard containers, and some collections that are not strictly containers, provide a begin member function, which returns an iterator that refers to the first element in the container, if it exists.

所有的标准容器包括一些集合(不是严格意义上的容器)都提供一个begin函数,如果第一个元素存在,就返回一个指向第一个元素的迭代器。

Advance to the next element

递增到下一个元素

Using pointer semantics, the prefix and postfix forms of the ++ operator move the iterator to the next element. Some iterator categories also support --, -=, +=, +, and - operators.

就是指针的语义,不论是前缀还是后缀形式的++都可以把一个迭代器移动到下一个元素。一些迭代器也支持:---=+==-操作符。

Return the referent

返回引用

Also using pointer semantics, the dereference operator * is used to return the referent. For example, if p is an iterator, then x = *p would assign the value referred to by p to x (assuming x and the referent are compatible types).

也是使用指针的语义。操作符*用来返回引用。例如:p是一个迭代器,那么x = *p就把x赋值为p指向的元素值。

Interrogate the iterator to see if it is at the end of the range

判断一个迭代器是不是已经到了区间的结尾

Similar to the begin member function, there is an end member function on all standard containers that returns an iterator that refers to one past the end of the container. Dereferencing this iterator yields undefined behavior, but you can compare the value returned by end to the current iterator to test if you are done iterating through the range.

begin函数类似,标准容器有一个成员函数是end。它可以返回一个指向区间结尾的迭代器。这个解引用这个迭代器的行为是没有定义的,你只是可以做比较,判断当前的迭代器是不是和它相等,(代码里常见的写法:for(;iter!=vecotr.end;))

 

The definition I usually read for C++ iterators is something like,

我经常遇到的C++对迭代器的定义如下:

 

"a generalization of a pointer", or, "an abstraction of a pointer."

一个泛型的指针或者是一个抽象的指针。

 

Both of these are technically correct, but somewhat high-level.

这些都是比较高级的技术错误。

 

Here's a more concrete one: an iterator is any type that behaves like an iterator.

也可以这样:一个迭代器就是一个像迭代器行为的类型。

What this means is that any type that supports the interface described in Table 1 is an iterator, including a pointer to an element in a plain, static array. The following types are iterators:

一个迭代器就是满足表1所描述的,包括一个指向元素的指针,你也可以认为就是一种迭代器,下面的类型都可以认为是迭代器。

 

An iterator or const_iterator typedef on a standard container

一个iteratorconst_iterator可以被标准容器typedef

A pointer to an element in an array of objects

指针指向对象集里的一个元素

Your Iterator class that you will read about in the second part of this article

 

But it does not mean that these types are equivalent, nor does it mean that they all inherit from a common base class.

但是这并不意味着这些类型是一样的,也不代表他们从同一个基类继承而来。

 

It means that you can use each one of these with pointer semantics: if i is an iterator, then *i returns the object referred to by the iterator; i++ advances the iterator to the next element; and where (*i).f is valid, meaning i refers to something that has a member function or variable named f, i->f is also valid.

这意味这,你可以使用一个迭代器就像是使用一个指针一样,比如:i是一个迭代器,*i返回引用的对象,i++递增。包括(*i).f也是合法的,意思是调用一个叫f的成员函数,i->f也合法。

 已同步至 fandaozhang的微博

刚表态过的朋友 (0 人)

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册
验证问答 换一个 验证码 换一个

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部