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

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

我的博客

迭代器模式 (接上一篇)

已有 1418 次阅读2011-6-13 11:04 |系统分类:IT技术|

The Iterator Pattern

迭代器模式

Iterators are not unique to C++. The concept of an iterator is something that allows two parties--generally the consumer of some data structure or "client code", and the implementer of the data structure, or "library code"--to communicate without concern for the other's internal details.

迭代器不是C++独有的。这个概念包含两个方面:一个是使用数据结构的代码(“客户”),一个是数据结构的实现(最典型的就是类)。他们相互交流是不需要知道对方实现细节的。

 

This principle of intentional ignorance is what lets a collection of elements (in any language) expose those elements to the outside world without revealing the details of the collection's internal implementation, i.e. whether it is a hash table, linked list, tree, or some other sort of data structure.

这种忽略内部实现的理论(应该也就是黑盒)使得任何元素类型的集合暴露仅仅他们的数据而没有暴露内部实现。比如:我不知道他的实现是:哈希表、链表、树、或者是其它。

 

Probably the best definition of the iterator pattern is in Design Patterns, by Erich Gamma, et al, (Addison-Wesley). The authors provide a short description of the intent of iterators:

或许对迭代器最好的定义就是《设计模式》一书里,作者对迭代器有一段简短的说明:

 

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

提供一种访问“聚合对象”的数据而且不需要知道其底层实现的方法。

 

The use cases they reference use iterators to access elements in one of these "aggregate objects," a la the C++ standard containers vector, list, map, and so on, and

in fact, the sample implementation they give is in C++. But most modern languages provide iterators in some form: Java has an Iterator class, and C# has enumerators.

这种例子就是C++里对vectorlistmap的访问,事实上,其它很多语言也对迭代器有实现,比如JAVAC#

 

A great deal of programming has to do with manipulating sequences of elements,

程序里会大量的操作序列化的元素,

 

which is why, despite its simplicity, the notion of an iterator is so broadly applicable.

这就是为什么,尽管它简单,迭代器这个概念的适用范围是如此的广泛。

 

 

 

 

You usually do the same sorts of things to a sequence of elements regardless of its type: iterating through every element, searching, sorting, inserting, deleting, and so on.

你经常排序一个序列的元素而且不关心它的类型:迭代器遍历每个元素,查找,排序,插入,删除等等。

 

Without a common interface (whether an iterator or something else), you would have to do the same things to different data structures in different ways. This would be a sad state of affairs; luckily we have iterators.

没有通用的接口(迭代器或者别的),你需要在不同的数据结构上做相同的事情。这将是悲崔的事情,还好我们有迭代器,于是就不悲崔了。

 

The iterator pattern defines a handful of simple requirements. An iterator should allow its consumers to:

迭代器模式定义了少量的需求,一个迭代器应该允许他的使用者:

Move to the beginning of the range of elements

移动到元素集合的开始

Advance to the next element

递增到下一个元素

Return the value referred to, often called the referent

返回元素的引用,

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

判断是否已经到了集合的结尾

 

If all of these requirements are met, then consumers of an iterator will be able to traverse a range of elements in some aggregate object with a minimum of effort.

如果上述要求都达到,这个迭代器将能用最小的代价遍历元素。

As you will see, C++ iterators provided by the standard library satisfy all of these requirements, though not exactly as they are outlined in Design Patterns. With that in mind, let's discuss what a C++ iterator is.

C++标准库的迭代器虽然并不完全像《设计模式》描述的那样,但是它满足上述要求。下面我们看一下C++的迭代器。

 

 已同步至 fandaozhang的微博

评论 (0 个评论)

facelist

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

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

返回顶部