• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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


            我們實現一個存儲INT的棧結構,用迭器來訪問這個結構中的數據,結構圖為:


            實現代碼:
            //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 閱讀(1447) 評論(0)  編輯 收藏 引用
            蜜桃麻豆www久久国产精品| 国内精品久久久久久久亚洲| 久久亚洲AV成人无码软件| 久久经典免费视频| 精品久久久无码21p发布| 久久精品黄AA片一区二区三区| 久久精品国产亚洲AV电影| 国产 亚洲 欧美 另类 久久| 精品国产乱码久久久久软件| 久久精品国产免费一区| 色综合久久夜色精品国产| 91精品国产高清91久久久久久| 久久免费小视频| 亚洲精品无码成人片久久| 久久九九青青国产精品| 99久久国产精品免费一区二区| 日韩一区二区久久久久久| 亚洲午夜久久久久久久久电影网 | 99久久国产综合精品成人影院| 无码精品久久一区二区三区| 97精品久久天干天天天按摩| 国产精品久久久久久久人人看 | 中文字幕无码久久人妻| 久久成人影院精品777| 精品国产日韩久久亚洲| 久久久WWW成人免费毛片| 97久久国产亚洲精品超碰热| 99久久99久久精品国产片果冻| 久久无码国产| 久久国产精品免费一区二区三区| .精品久久久麻豆国产精品| 久久久www免费人成精品| 一级女性全黄久久生活片免费 | 久久综合丁香激情久久| 久久99精品国产麻豆| 国产亚洲综合久久系列| 亚洲精品第一综合99久久| 亚洲а∨天堂久久精品| 久久久无码精品亚洲日韩京东传媒| 久久免费99精品国产自在现线 | 97精品国产91久久久久久|