一个简单的iterator的代码

None.gif template < typename T >
None.gif
class  ListItem
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif T value() 
const
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return _value;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif ListItem 
*next() const
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return _next;
ExpandedSubBlockEnd.gif }

InBlock.gif
protected:
InBlock.gif
private:
InBlock.gif T _value;
InBlock.gif ListItem 
*_next;
ExpandedBlockEnd.gif}
;
None.giftemplate
< typename T >
None.gif
class  List
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif
protected:
InBlock.gif
private:
InBlock.gif ListItem
<T> *begin;
ExpandedBlockEnd.gif}
;
None.gif
None.giftemplate
< typename Item >
None.gif
class  ListIter  
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif ListIter();
ExpandedSubBlockStart.gifContractedSubBlock.gif 
virtual ~ListIter()dot.gif{};
InBlock.gif
InBlock.gif Item 
*ptr;
ExpandedSubBlockStart.gifContractedSubBlock.gif ListIter(Item 
*= 0) : ptr(p)dot.gif{}
InBlock.gif
InBlock.gif Item
& operator*() const 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return *ptr;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif Item 
*operator->() const
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return ptr;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
//pre increment operator
InBlock.gif
 ListIter& operator++()
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  ptr 
= ptr->next();
InBlock.gif  
return *this;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
//post increment operator
InBlock.gif
 ListIter operator++(int)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  ListIter tmp 
= *this;
InBlock.gif  
++*this;
InBlock.gif  
return tmp;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
bool operator==(const ListIter& i) const
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return ptr == i.ptr;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
bool operator!=(const ListIter& i) const
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
return ptr != i.ptr;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}
;
None.gif

转载于:https://www.cnblogs.com/hunter_gio/archive/2005/05/13/154974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值