2021.11.24

今日学习内容:迭代器模式

信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从小到大和从大到小两种次序输出学生信息。

c++源代码:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream> 
#include<string>
#include <sstream>
#include <vector> 
using namespace std;
 
/* object可以是任意类型的变量 */
typedef int object;
 
class Iterator
{
public:
    virtual object begin() = 0;
    virtual void   next() = 0;
    virtual object end() = 0;
    virtual object current() = 0;
    virtual bool   IsDone() = 0;
};
 
class ConcreteAggregate
{
private:
    vector<object> _objects;
 
public:
    void AddObject(object obj)
    {
        _objects.push_back(obj);
    }
 
    object& operator[](int index)
    {
        return _objects[index];
    }
    int size()
    {
        return _objects.size();
    }
};
 
class ConcreteIterator :public Iterator
{
public:
    ConcreteAggregate *agg;
    int _index;
public:
    ConcreteIterator(ConcreteAggregate *agg)
    {
        this->agg = agg;
        _index = 0;
    }
    virtual object begin()
    {
        return (*agg)[0];
    }
    virtual void next()
    {
        _index++;
    }
    virtual void preious()
    {
        _index--;
    }
 
    virtual object end()
    {
        _index = agg->size();
        return (*agg)[_index - 1];
    }
 
    virtual object current()
    {
        return (*agg)[_index];
    }
 
    virtual bool IsDone()
    {
        return (_index == agg->size());
    }
    virtual bool IsFirst()
    {
        return (_index == 0);
    }
};
 
int main()
{
    ConcreteAggregate *objects = new ConcreteAggregate();
    cout << "信1305班同学:" << endl;
    object a = 20130001;
    object b = 20130002;
    object c = 20130003;
    object d = 20130004;
    object e = 20130005;
    object f = 20130006;
    object g = 20130007;
 
    objects->AddObject(a);
    objects->AddObject(b);
    objects->AddObject(c);
    objects->AddObject(d);
    objects->AddObject(e);
    objects->AddObject(f);
    objects->AddObject(g);
 
 
    ConcreteIterator *iter = new ConcreteIterator(objects);
    ConcreteIterator *iter1 = new ConcreteIterator(objects);
 
    object tmp_begin = iter->begin();
    cout << "从小到大输出:" << endl;
    while (!iter->IsDone())
    {
        cout << iter->current() << endl;
        iter->next();
    }
    cout << endl;
 
    object tmp_begin1 = iter1->end();
    cout << "从大到小输出:" << endl;
    while (!iter1->IsFirst())
    {
        iter1->preious();
        cout << iter1->current() << endl;
    }
    cout << endl;
 
    delete objects;
    delete iter;
 
    system("pause");
    return 0;
}

  

实现截图如下:

posted on 2021-11-24 13:25  只爱敲代码  阅读(48)  评论(0编辑  收藏  举报

导航