关闭

迭代器模式

228人阅读 评论(0) 收藏 举报

#include "stdafx.h"   
#include "windows.h"   
#include<stdlib.h>    
#include<crtdbg.h>    
#include <iostream>   
#include <time.h>   
#include <vector>   
#include <mmsystem.h>   
#include "math.h"   
#include "Windows.h"   
#include <queue>   
#include <list>   
using namespace std;  

class Test  
{  
public:  
    void AddNum(int nNum)  
    {  
        m_vElement.push_back(nNum);  
    }  
private:  
    vector<int> m_vElement;  
public:
	class Iterator1
	{
	public:
		Iterator1(Test *p)
		{
			m_pTest=p;
			m_nCurrPos=0;
		}
	public:
		bool NotEnd()
		{
			return m_nCurrPos!=m_pTest->m_vElement.size();
		}
		void Next()
		{
			++m_nCurrPos;
		}
		int GetElement()
		{
			return (m_pTest->m_vElement)[m_nCurrPos];
		}
	private:
		int			m_nCurrPos;
		Test		*m_pTest;
	};
	Test::Iterator1 GetIterator1()
	{
		return Test::Iterator1(this);
	};
};  

int _tmain(int argc, _TCHAR* argv[])  
{  
    Test ts;  
    ts.AddNum(1);  
    ts.AddNum(3);  
    ts.AddNum(2);  
    ts.AddNum(6);  
    ts.AddNum(8);  
    ts.AddNum(4);  
  
	Test::Iterator1 it=ts.GetIterator1();
	while(it.NotEnd())
	{
		cout<<it.GetElement()<<endl;
		it.Next();
	}
  
    return 0;  
}  


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:122545次
    • 积分:2212
    • 等级:
    • 排名:第17641名
    • 原创:90篇
    • 转载:3篇
    • 译文:0篇
    • 评论:8条