迭代器 可以看成是一个指针,重载了指针操作的符号
#include <iostream>
using namespace std;
struct Node
{
int nValue;
Node* pNext;
};
class Iterator // 迭代器
{
public:
Node* pTemp;
public:
Node* operator++(int a) //右++
{
Node* bj = pTemp;
pTemp = pTemp->pNext;
return bj;
}
Node* operator++(int a) //左++
{
pTemp = pTemp->pNext;
return pTemp;
}
int operator*()
{
return pTemp->nValue;
}
bool operator!=(Node* pNode)
{
if(pTemp != pNode)
return true;
else
return false;
}
Node* operator->()
{
return pTemp;
}
};
int main()
{
Node* pTemp = 0;
while(pTemp != 0)
{
cout << pTemp->nValue << endl;
pTemp = pTemp->pNext;
}
Iterator ite;
ite++;
*ite;
while(ite != 0)
{
cout << *ite << endl;
ite++;
}
cout << ite->nValue << endl;
system("pause");
return 0;
}