• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            emptysoul

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團(tuán)隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            迭代器模式(Iterator)提供一種方法可以訪問聚合對象,而不用暴露這個對象的內(nèi)部表示。結(jié)構(gòu)圖為:


            我們實現(xiàn)一個存儲INT的棧結(jié)構(gòu),用迭器來訪問這個結(jié)構(gòu)中的數(shù)據(jù),結(jié)構(gòu)圖為:


            實現(xiàn)代碼:
            //IntStack.h
            class IntStack
            {
            public:
                IntStack();
                
            virtual ~IntStack();

                
            void Push(int);
                
            int Pop();
                friend 
            class IntStackIterator;
            private:
                
            enum { SIZE = 100 }; 
                
            int m_nStack[SIZE];
                
            int m_nTop;
            };

            //IntStack.cpp
            #include "stdafx.h"
            #include 
            "IntStack.h"
            #include 
            <iostream>

            using namespace std;

            IntStack::IntStack()
            {
                m_nTop 
            = 0;
            }

            IntStack::
            ~IntStack()
            {

            }

            void IntStack::Push(int nData)
            {
                
            if(m_nTop < SIZE)
                {
                    m_nStack[m_nTop
            ++= nData;
                }
                
            else
                {
                    cout 
            << "超出容量范圍。" << endl;
                }
            }

            int IntStack::Pop()
            {
                
            if(m_nTop > 0)
                {
                    
            return m_nStack[--m_nTop];
                }

                
            return 0;
            }

            //Iterator.h
            class Iterator  
            {
            public:
                Iterator();
                
            virtual ~Iterator();

                
            virtual int operator++() = 0;
                
            virtual int operator++(int= 0;
            };

            //Iterator.cpp
            #include "stdafx.h"
            #include 
            "Iterator.h"

            Iterator::Iterator()
            {

            }

            Iterator::
            ~Iterator()
            {

            }

            //IntStack.h
            #include "Iterator.h"

            class IntStack;
            class IntStackIterator : public Iterator
            {
            public:
                IntStackIterator(IntStack
            *);
                
            virtual ~IntStackIterator();

                
            int operator++();
                
            int operator++(int);
            private:
                IntStack
            * m_pStack;
                
            int m_nIndex;
            };

            //IntStackIterator.cpp
            #include "stdafx.h"
            #include 
            "IntStackIterator.h"
            #include 
            "IntStack.h"

            IntStackIterator::IntStackIterator(IntStack
            * pStack)
            {
                m_pStack 
            = pStack;
                m_nIndex 
            = 0;
            }

            IntStackIterator::
            ~IntStackIterator()
            {
                
            if(m_pStack != NULL)
                {
                    delete m_pStack;
                    m_pStack 
            = NULL;
                }
            }

            int IntStackIterator::operator++()
            {
                
            if(m_nIndex < m_pStack->m_nTop)
                {
                    
            return m_pStack->m_nStack[++m_nIndex];
                }

                
            return 0;
            }

            int IntStackIterator::operator++(int)
            {
                
            if(m_nIndex < m_pStack->m_nTop)
                {
                    
            return m_pStack->m_nStack[m_nIndex++];
                }

                
            return 0;
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "Iterator.h"
            #include 
            "IntStackIterator.h"
            #include 
            "IntStack.h"
            #include 
            <iostream>

            using namespace std;

            int main(int argc, char* argv[])
            {
                IntStack
            * pStack = new IntStack;
                
            for(int i = 0; i < 20++i)
                {
                    pStack
            ->Push(i);
                }

                Iterator
            * pIt = new IntStackIterator(pStack);
                
            for(i = 0; i < 20++i)
                {
                    cout 
            << (*pIt)++ << endl;
                }

                
            return 0;
            }
            posted on 2009-02-15 13:30 emptysoul 閱讀(1448) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国内精品伊人久久久久| 国产成人无码精品久久久性色| 亚洲午夜久久久影院| 久久国语露脸国产精品电影 | 久久精品国产一区二区三区日韩| 99久久国产热无码精品免费久久久久| 久久久久国产一区二区三区| 亚洲国产日韩欧美久久| 国产精品久久久福利| 久久综合色区| 韩国三级中文字幕hd久久精品 | 亚洲国产成人久久综合一区77 | 国产91色综合久久免费分享| 久久精品夜色噜噜亚洲A∨| 人妻无码久久一区二区三区免费| 日本道色综合久久影院| 久久99国产精品二区不卡| 日本欧美国产精品第一页久久| 久久久久成人精品无码中文字幕 | 久久久久久久人妻无码中文字幕爆| 精品久久久久久| 久久精品中文闷骚内射| 亚洲国产精品无码久久九九| 久久本道久久综合伊人| 99久久免费国产精精品| 男女久久久国产一区二区三区| 怡红院日本一道日本久久| 色综合久久无码五十路人妻| 无码8090精品久久一区| 国产精品综合久久第一页| 日韩精品国产自在久久现线拍 | 精品免费tv久久久久久久| 亚洲色大成网站WWW久久九九| 久久精品国产亚洲5555| 国产日韩久久久精品影院首页| 国产人久久人人人人爽| 欧美一区二区三区久久综| 亚洲国产精品18久久久久久| 亚洲欧美成人综合久久久| 亚洲精品tv久久久久久久久| 亚洲综合日韩久久成人AV|