• <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 閱讀(1446) 評論(0)  編輯 收藏 引用
            欧美喷潮久久久XXXXx| 久久午夜无码鲁丝片秋霞| 久久国内免费视频| 久久狠狠爱亚洲综合影院| 精品久久久久久无码专区不卡| 久久99国产精品久久99| 久久精品夜色噜噜亚洲A∨| 久久综合给合综合久久| 久久久久女人精品毛片| 麻豆久久| av午夜福利一片免费看久久| 久久九九有精品国产23百花影院| 久久精品无码一区二区app| 超级碰碰碰碰97久久久久| 精品久久777| 久久精品国产久精国产果冻传媒| 精品999久久久久久中文字幕| 亚洲国产精品综合久久网络| 精品一区二区久久久久久久网站| 亚洲国产成人精品女人久久久 | 亚洲精品无码久久毛片| 久久精品亚洲一区二区三区浴池 | 久久久久一本毛久久久| 人妻无码αv中文字幕久久琪琪布| 久久久久久亚洲精品不卡| 99久久久精品| 久久棈精品久久久久久噜噜| 久久精品国产99国产精品| 9久久9久久精品| 久久久精品人妻一区二区三区四| 午夜精品久久久内射近拍高清| 99热精品久久只有精品| 久久综合狠狠综合久久激情 | 99久久国产主播综合精品| 97久久香蕉国产线看观看| 久久亚洲AV成人出白浆无码国产| 久久夜色撩人精品国产小说| 无码精品久久一区二区三区 | 国产国产成人精品久久| 99久久99久久久精品齐齐| 国内精品伊人久久久久av一坑|