抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

数据结构:迭代器

模式类型

数据结构

迭代器模式

将特定的数据结构封装在内部,在外部提供统一的接口,来实现与数据结构无关的访问。

数据结构内部变化各异,但希望在不暴露内部结构的同时,让外部客户“透明遍历”访问其中的元素。

使用场景

  • C++ STL 迭代器

举例

面向对象的迭代器

运行时多态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
template<typename T>
class Iterator {
public:
virtual void first() = 0;
virtual void next() = 0;
virtual T& current() = 0;
};

template<typename T>
class MyCollection {
public:
Iterator<T> createIterator() {
return (new CollectionIterator<T>(this))
}
};

template<typenmae T>
class CollectionIterator : public Iterator<T> {
MyCollection<T> mc;

public:
CollectionIterator(const MyCollection<T> &c): mc(c) {

}

void first() override {

}
void next() override {

}
T& current() override {

}
};

void MyAlgorithm() {
MyCollection<int> mc;
Iterator<int> iter = mc.createIterator();
for (iter.first(); ...) {
// ...
}
}