迭代器实现代码



#include <iostream>
using namespace std;


template <class T>
class my_iterator{
//friend class my_iterator;
public:
my_iterator(T *value=NULL){_value=value;}
//~my_iterator()//该不该加?
//{
//}


T& operator*() const{return *_value;}
T* operator->() const{return _value;}//用于返回内容

//++(当iterator移动的时候,移动的是其内封装的值)
my_iterator& operator++()
{
_value=_value->next;
return *this;
}


bool operator==(my_iterator& other){return _value==other._value;}
bool operator!=(my_iterator& other){return _value!=other._value;}


protected:
T *_value;
};


template <class T>
class my_node{
public:
my_node():next(NULL){}
my_node(T& v):value(v),next(NULL){}


T value;
my_node *next;
};


template <class T>
class my_list{
public:
//定义iterator
typedef my_iterator< my_node<T> > my_iterator;


public:
my_list():_size(0),_pnode(NULL){}

//获得元素个数
int size(){return _size;}


//清空
void clear()
{
my_node<T>* p;
while(_pnode)
{
p=_pnode;
_pnode=_pnode->next;
delete p;
}
_size=0;
}


//获得首元素
my_iterator& begin()
{
my_iterator *pit=new my_iterator(_pnode);
return *pit;
}


//获得尾元素
my_iterator& end()
{
my_node<T>* p=_pnode;
while(p) p=p->next;
my_iterator *pit=new my_iterator(p);
return *pit;
}


//添加元素
void push_back(T value)//后插
{
my_node<T> *p=new my_node<T>(value);
if(_pnode){
my_node<T> *pt=_pnode;
while(pt) pt=pt->next;
pt->next=p;
}else{
_pnode=p;
}
_size++;
}
void push_front(T value)//前插
{
my_node<T> *p=new my_node<T>(value);
if(_pnode){
p->next=_pnode;
_pnode=p;
}else{
_pnode=p;
}
_size++;
}


//删除
T pop_back()//后取
{
my_node<T> *p=_pnode;
my_node<T> *tail;
T value;
if(!p) throw("NULL");
else if(!p->next)
{
T value=p->value;
delete p;
_size--;
_pnode=NULL;
return value;
}
while(p && p->next && p->next->next) p=p->next;
value=p->next->value;
delete p->next;
p->next=NULL;
_size--;
return value;
}
T pop_front()//前取
{
my_node<T> *p=_pnode;
T value;
if(!p) throw("Exception: the list is NULL!");
_pnode=_pnode->next;
value=p->value;
delete p;
_size--;
return value;
}

//


protected:
my_node<T> *_pnode;
int _size;

};


//重载输出
template <class T>
ostream& operator<<(ostream& ost,my_list<T> lst)
{
my_list<T>::my_iterator it;
if(lst.size()>0){
for(it=lst.begin();it!=lst.end();it++)
{
cout<<it->value<<endl;
}
}else{
cout<<"NULL"<<endl;
}

return ost;
}


//===================自定义find算法
template <class T,class Value>
T my_find(T begin,T end,Value value)
{
T it;
for(it=begin;it!=end;it++)
{
if(it->value==value) break;
}


return it; 
}


int main(int argc, char* argv[])
{
//测试int
my_list<int> lst;
lst.push_front(1);
lst.push_front(2);
lst.push_front(3);
lst.push_front(4);
lst.push_front(5);
cout<<lst<<endl;
lst.pop_front();
lst.pop_back();
cout<<lst<<endl;
lst.clear();
cout<<lst<<endl;
try{
lst.pop_front();//将发生异常
}catch (char *e) {
cout<<e<<endl;
}
//测试double
my_list<double> lst1;
lst1.push_front(1.0);
lst1.push_front(2.2);
lst1.push_front(3.3);
lst1.push_front(4.0);
lst1.push_front(5.7344);
cout<<lst1<<endl;
lst1.pop_front();
lst1.pop_back();
cout<<lst1<<endl;
lst1.clear();
cout<<lst1<<endl;
try{
lst1.pop_front();//将发生异常
}catch (char *e) {
cout<<e<<endl;
}

//测试find函数
int i;
for(i=0;i<20;i++)
{
lst.push_front(i);
lst1.push_front(i+0.5);
}
my_list<int>::my_iterator it1;
my_list<double>::my_iterator it2;
it1=my_find<my_list<int>::my_iterator,int>(lst.begin(),lst.end(),5);
it2=my_find<my_list<double>::my_iterator,double>(lst1.begin(),lst1.end(),5.5);


cout<<it1->value<<endl;
cout<<it2->value<<endl;

return 0;

}



转自  http://bbs.csdn.net/topics/190136277

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值