/*****************************************************************//** * \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; } }
調用:
/*****************************************************************//** * \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
輸出:
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 請按任意鍵繼續. . .