cpp: Iterator Pattern

 C++已经是个多重泛型编程语言(multiparadigm programming lauguage),一个同时支持过程形式(procedural)、面向对象形式(object-oriented)、函数形式(functional)、范型形式(generic)、元编程形式(metaprogramming)的语言。 

 

human flow, Material flow, Capital or Money flow,Reverse flow and information flow

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*****************************************************************//**
 * \file   GoldIterator.h
 * \brief  迭代器模式 Iterator Pattern
 * 2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GOLDITERATOR_H
#define GOLDITERATOR_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
 
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuIteratorPattern
{
     
    /// <summary>
    /// 抽象迭代器类模板
    /// </summary>
    /// <typeparam name="T"></typeparam>
    template <typename T>
    class GoldIterator
    {
    public:
        /// <summary>
        /// 指向容器中第一个元素
        /// </summary>
        virtual void First() = 0;  
        /// <summary>
        /// 指向下一个元素
        /// </summary>
        virtual void Next() = 0;     
        /// <summary>
        /// 是否遍历完
        /// </summary>
        /// <returns></returns>
        virtual bool IsDone() = 0;  
        /// <summary>
        /// 获取当前的元素
        /// </summary>
        /// <returns></returns>
        virtual T& CurrentItem() = 0;
        /// <summary>
        /// 做父类时析构函数应该为虚函数
        /// </summary>
        virtual ~GoldIterator() {}        
 
 
 
    };
 
}
 
#endif
 
 
/*****************************************************************//**
 * \file   GoldCotainer.h
 * \brief  迭代器模式 Iterator Pattern
 *  2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GOLDCOTAINER_H
#define GOLDCOTAINER_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "GoldIterator.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuIteratorPattern
{
 
 
    /// <summary>
    /// 抽象容器类模板
    /// </summary>
    /// <typeparam name="T"></typeparam>
    template <typename T>
    class GoldCotainer
    {
 
    public:
        /// <summary>
        /// 创建迭代器
        /// </summary>
        /// <returns></returns>
        virtual GoldIterator<T>* CreateIterator() = 0;
        /// <summary>
        /// 获取当前元素
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        virtual T& getItem(int index) = 0; 
        /// <summary>
        /// 容器中元素数量
        /// </summary>
        /// <returns></returns>
        virtual int getSize() = 0; 
        /// <summary>
        /// 做父类时析构函数应该为虚函数
        /// </summary>
        virtual ~GoldCotainer() {}
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   GoldVectorIterator.h
 * \brief  迭代器模式 Iterator Pattern
 *  2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GOLDVECTORITERATOR_H
#define GOLDVECTORITERATOR_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "GoldIterator.h"
#include "GoldCotainer.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuIteratorPattern
{
 
    /// <summary>
    /// 具体迭代器类模板
    /// </summary>
    /// <typeparam name="T"></typeparam>
    template <typename T>
    class GoldVectorIterator :public GoldIterator<T>
    {
    public:
        /// <summary>
        ///
        /// </summary>
        /// <param name="tmpc"></param>
        GoldVectorIterator(GoldCotainer<T>* tmpc) :myVector(tmpc)
        {
            mCurrent = 0;
        }
        /// <summary>
        /// 容器(数组)中的第一个元素下标为0
        /// </summary>
        virtual void First()
        {
            mCurrent = 0;
        }
        /// <summary>
        /// 下标+1,意味着数组中的下一个元素
        /// </summary>
        virtual void Next()
        {
 
            mCurrent++;
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        virtual bool IsDone()
        {
            if (mCurrent >= myVector->getSize())
            {
                return true;
            }
            return false;
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        virtual T& CurrentItem()
        {
            return myVector->getItem(mCurrent);
        }
    private:
        /// <summary>
        ///
        /// </summary>
        GoldCotainer<T>* myVector;
        /// <summary>
        /// 记录数组的当前下标(迭代器在当前容器中的位置)
        /// </summary>
        int mCurrent; 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   GoldVector.h
 * \brief  迭代器模式 Iterator Pattern
 *  2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GOLDVECTOR_H
#define GOLDVECTOR_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "GoldIterator.h"
#include "GoldCotainer.h"
#include "GoldVectorIterator.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuIteratorPattern
{
 
 
    /// <summary>
    /// 具体容器类模板
    /// </summary>
    template <typename T>
    class GoldVector:public GoldCotainer<T>
    {
    public:
         
         
        GoldVector()
        {
            //将数组中元素进行初始化
            for (int i = 0; i < 10; ++i)
            {
                mElem[i] = i;
            }
        }
         
 
     
        virtual GoldIterator<T>* CreateIterator()
        {
            //工厂模式,注意实参传递进去的是该容器的指针this
            return new GoldVectorIterator<T>(this); //要考虑在哪里释放的问题
        }
 
         
     
        virtual T& getItem(int index)
        {
            return mElem[index];
        }
         
         
 
        /// <summary>
        /// 为简化代码,返回固定数字
        /// </summary>
        virtual int getSize()
        {
            return 10;
        }
    private:
        /// <summary>
        /// 为了简化代码,将容器实现为固定装入10个元素的数组
        /// </summary>
        T mElem[10];
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   GeovinDu.h
 * \brief  迭代器模式 Iterator Pattern
 *  2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GEOVINDU_H
#define GEOVINDU_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "GoldIterator.h"
#include "GoldCotainer.h"
#include "GoldVectorIterator.h"
#include "GoldVector.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuIteratorPattern
{
 
    /// <summary>
    ///
    /// </summary>
    class GeovinDu
    {
 
    private:
 
    public:
 
        /// <summary>
        /// 迭代器模式示例
        /// </summary>
        void displaySimple();
 
 
    };
 
}
 
#endif
 
 
/*****************************************************************//**
 * \file   GeovinDu.cpp
 * \brief  迭代器模式 Iterator Pattern
 *  2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#include "GeovinDu.h"
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuIteratorPattern
{
 
    /// <summary>
    ///
    /// </summary>
    void GeovinDu::displaySimple()
    {
            /**/
            std::vector<int> msgVector;
            msgVector.push_back(1); //末尾插入1,vector中内容:1
            msgVector.push_back(2); //开头插入2,vector中内容:1,2
            msgVector.push_back(3); //末尾插入3,vector中内容:1,2,3
            for (std::vector<int>::iterator pos = msgVector.begin(); pos != msgVector.end(); ++pos)
            {
                cout << *pos << endl;
            }
            cout << "-------------------" << endl;
            std::list<int>  msgList;
            msgList.push_back(1); //末尾插入1,list中内容:1
            msgList.push_front(2);//开头插入2,list中内容:2,1
            msgList.push_back(3); //末尾插入3,list中内容:2,1,3
            for (std::list<int>::iterator pos = msgList.begin(); pos != msgList.end(); ++pos)
            {
                cout << *pos << endl;
            }
            cout << "-------------------" << endl;
 
            /**/
            DuIteratorPattern::GoldCotainer<int>* dupcontainer = new DuIteratorPattern::GoldVector<int>();
            DuIteratorPattern::GoldIterator<int>* duiter = dupcontainer->CreateIterator();
            //遍历容器中的元素
            for (duiter->First(); !duiter->IsDone(); duiter->Next()) //多态机制的遍历,效率上不好,尽量考虑栈机制
            {
                cout <<"迭代器:"<< duiter->CurrentItem() << endl;
            }
 
            cout << "-------------------" << endl;
     
     
 
            DuIteratorPattern::GoldCotainer<int>* pcontainer = new DuIteratorPattern::GoldVector<int>();
            DuIteratorPattern::GoldVectorIterator<int> iter(pcontainer);
            //遍历容器中的元素 
            for (iter.First(); !iter.IsDone(); iter.Next())//非多态机制,可以明显改善程序性能
            {
                cout <<"迭代器:"<< iter.CurrentItem() << endl;
            }
 
 
            //释放资源
            delete duiter;
            delete dupcontainer;
 
 
 
            //释放资源
            delete pcontainer;
 
 
    }
 
 
}

  

调用:

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
/*****************************************************************//**
 * \file   ConsoleDuIteratorPattern.cpp
 * \brief  迭代器模式 Iterator Pattern
 *  2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
// ConsoleDuIteratorPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 2023年5月12日 涂聚文 Geovin Du Visual Studio 2022 edit.
#define _UNICODE
 
 
#include <iostream>
#include <vector>
#include <list>
 
#include "GeovinDu.h"
#ifdef _DEBUG   //只在Debug(调试)模式下
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) //重新定义new运算符
#define new DEBUG_NEW
#endif
#endif
using namespace std;
using namespace DuIteratorPattern;
 
 
int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出时检测内存泄漏并显示到“输出”窗口
 
    std::cout << "Hello World!涂聚文 Geovin Du\n";
    GeovinDu geovin;
    geovin.displaySimple();
 
    system("pause");
    return 0;
 
}
 
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
 
// 入门使用技巧:
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#define UNICODE

  

输出:

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
Hello World!涂聚文 Geovin Du
1
2
3
-------------------
2
1
3
-------------------
迭代器:0
迭代器:1
迭代器:2
迭代器:3
迭代器:4
迭代器:5
迭代器:6
迭代器:7
迭代器:8
迭代器:9
-------------------
迭代器:0
迭代器:1
迭代器:2
迭代器:3
迭代器:4
迭代器:5
迭代器:6
迭代器:7
迭代器:8
迭代器:9
请按任意键继续. . .

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示