迭代器实现
来源:互联网 发布:免费手机监听卧底软件 编辑:程序博客网 时间:2024/06/06 12:22
#include <iostream>
using namespace std;
//T为要装载的对象
template<typename T>
class ListItem
{
private:
T _value;
ListItem* _next;
public:
T value() const
{
return _value;
}
ListItem *next()const
{
return _next;
}
void setValue(T v)
{
_value = v;
}
void setNext(ListItem *pNext)
{
_next = pNext;
}
};
//容器类(以链表的形式实现)
template<typename T>
class List
{
public:
class ListIter
{
public:
ListItem<T> *ptr;
ListIter(ListItem<T> *p = 0) : ptr(p){};
ListItem<T> &operator*()const
{
return *ptr;
}
ListItem<T> *operator->() const
{
return ptr;
}
ListIter &operator++()
{
ptr = ptr->next();
return *this;
}
ListIter &operator++(int)
{
ListIter tmp = *this;
++*this;
return tmp;
}
bool operator==(const ListIter& rth) const
{
return ptr = rth.ptr;
}
bool operator!=(const ListIter& rth) const
{
return ptr != rth.ptr;
}
};
private:
long _size;
ListItem<T> *_front;
ListItem<T> *_end;
public:
List() : _front(NULL), _end(NULL){}
~List(){}
ListItem<T> *begin() const
{
return _front;
}
ListItem<T> *end()
{
return _end->next();
}
void insert_front(T value)
{
ListItem<T> *item = new ListItem<T>;
item->setValue(value);
item->setNext(NULL);
if(_front == NULL)
{
_front = _end = item;
}
else
{
item->setNext(_front);
_front = item;
}
}
void insert_end(T value)
{
ListItem<T> *item = new ListItem<T>;
item->setValue(value);
item->setNext(NULL);
if(_front == NULL)
{
_front = _end = item;
}
else
{
_end->setNext(item);
_end = item;
}
}
void display(ostream &os = cout)
{
ListItem<T> *tmp = _front;
while(tmp != _end->next())
{
cout<<tmp->value()<<" ";
tmp = tmp->next();
}
cout<<endl;
}
};
int main()
{
List<int> s;
for(int i = 0; i < 5; ++i)
{
s.insert_front(i);
s.insert_end(i+2);
}
s.display();
List<int>::ListIter it;
for(it = s.begin(); it != s.end(); it++)
{
printf("%d/n", *it);
}
return 0;
}
- 实现迭代器
- 迭代器实现
- 迭代器实现
- BinaryTree 迭代器实现 非递归实现
- 迭代器的实现
- 迭代器的实现
- C#迭代器的实现
- 迭代器的实现
- 文本文件迭代器的实现
- C#中实现迭代器
- C++实现迭代器模式
- 实现一个跨步迭代器
- 自己实现 STL 迭代器
- 迭代器模式lua实现
- 迭代器实现代码
- 迭代器模式----C++实现
- C#中实现迭代器
- 迭代器实现二分法搜索
- SQL语句截取时间,只显示年月日
- cygwin shell报错: syntax error near unexpected token `$'{/r''
- 人体器官抗衰老方:樱桃汁可防止心脏早衰
- Spring web应用classes目录绝对路径
- 蜘蛛牌 hdu 1584
- 迭代器实现
- Java中的hashCode()和equals()
- 图文并茂方法的总结
- 飞信消息管理器2010 V2.0
- 基于mirrorrr搭建的自己的在线代理(http://zfqproxy.appspot.com/)
- 解决Sybase ASE中的dbisql (interactive sql)和Sybase SQL Anywhere中的dbisql相互打架问题
- 快速刷屏(动态规划)
- 教你玩 Robocode(2) —— 使用Eclipse开发Robot
- [转]强烈推荐:240多个jQuery插件