• <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 閱讀(1448) 評論(0)  編輯 收藏 引用
            精品免费久久久久国产一区| 国产精品亚洲综合久久| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久这里只精品99re66| 久久久久AV综合网成人 | 久久天天躁狠狠躁夜夜不卡| 午夜精品久久久久久久久| 国产成人无码精品久久久免费 | 婷婷久久综合九色综合绿巨人| 亚洲中文字幕无码一久久区| 99精品久久久久久久婷婷| 久久人人爽人人爽人人片AV高清| 国产一级持黄大片99久久| 奇米影视7777久久精品人人爽| 青青草原综合久久| 久久久久久久亚洲Av无码| 香蕉99久久国产综合精品宅男自| 国产一区二区三区久久| 久久综合狠狠综合久久| 久久人做人爽一区二区三区| 午夜精品久久影院蜜桃| 九九久久精品无码专区| 韩国三级中文字幕hd久久精品 | 四虎国产精品免费久久久| 久久久久高潮毛片免费全部播放| 国内精品九九久久精品| 偷窥少妇久久久久久久久| 日日狠狠久久偷偷色综合免费| 国产三级精品久久| 久久精品成人免费观看97| 国产午夜精品久久久久九九| 久久av免费天堂小草播放| 亚洲伊人久久大香线蕉苏妲己| 热久久国产精品| 久久久精品人妻无码专区不卡| 国产亚州精品女人久久久久久| 国产精品伦理久久久久久| 久久久久国产精品三级网| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久久久久极精品久久久| 精品伊人久久久|