其实一种循环结构就是一种迭代操作。
在STL中,容器的迭代器被作为容器元素对象或者I/O流中的对象的位置指示器,因此可以把它理解为面向对象的指针–一种泛型指针或通用指针。不依赖于元素的真实类型。迭代器是为了降低容器和泛型算法至今联系而设计的,泛型算法的参数不是容器。是迭代器。
容器的迭代器的作用就类似一个游标,它屏蔽了底层存储空间的不连续性,在上层使容器元素有一种连续的假象。
接下来简单实现下list迭代器
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
template<class T>
struct _ListNode
{
_ListNode<T>* _prev;
_ListNode<T>* _next;
T _data;
_ListNode(const T& data)
:_data(data)
, _prev(NULL)
, _next(NULL)
{}
};
template<class T,class Ref,class Ptr>
//ref为引用,ptr为指针类型
struct _ListIterator
{
typedef _ListNode<T> Node;
typedef _ListIterator<T, Ref, Ptr> self;
typedef _ListIterator<T, Ref, Ptr> Iterator;
typedef Ref Reference;
typedef Ptr Pointer;
Node* _node;
_ListIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
self& operator++()
{
_node = _node->_next;
return *this;
}
self operator++(int)//后置++
{
Node* temp = _node;
_node = _node->_next;
return temp;
}
self& operator--()
{
_node= _node->_prev;
return *this;
}
self& operator--(int)//后置--
{
Node* temp = _node;
_node = _node->_prev;
return temp;
}
bool operator==(const self& s)
{
return _node == s._node;
}
bool `
perator!=(const self& s)
{
return _node != s._node;
}
};
//ReserveIterator迭代器就可以移正向迭代器为模板。再次基础上是反向迭代器。
template <class Iterator>
struct ReserveIterator
{
ReserveIterator(Iterator it)
:_it(it)
{}
typename Iterator::Reference operator*()
{
return *_it;
}
typename Iterator::Pointer operator->()
{
return operator->();
}
ReserveIterator operator++()
{
--_it;
return *this;
}
ReserveIterator operator++(int)
{
ReserveIterator temp = _it;
--_it;
return temp;
}
ReserveIterator operator--()
{
++it;
return *this;
}
ReserveIterator operator--(int)
{
ReserveIterator temp = _it;
++_it;
return temp;
}
bool operator!=(ReserveIterator<Iterator>& rit)
{
return _it != rit._it;
}
Iterator _it;
};
//list迭代器的使用实现常规操作
template<class T>
class List
{
typedef _ListNode<T> Node;
public:
typedef _ListIterator<T, T&, T*> Iterator;
typedef _ListIterator<T, const T&, const T*> ConstIterator;
typedef ReserveIterator<Iterator> ReserveIterator;
//typedef ReserveIterator<ConstIterator> ConstReserveIterator;
List()
{
_head = new Node(T());
_head->_next = _head;
_head->_prev = _head;
}
void clear()
{
Iterator it = Begin();
while (it!= End())
{
Node* del = it._node;
it++;
delete del;
}
}
~List()
{
clear();
delete _head;
_head = NULL;
}
Iterator End()
{
return Iterator(_head);
}
ConstIterator End() const
{
return ConstIterator(_head);
}
ReserveIterator REnd()
{
return ReserveIterator(End());
}
/*ConstReserveIterator REnd() const
{
return ConstReserveIterator(End());
}*/
ReserveIterator RBegin()
{
return ReserveIterator(--End());
}
/*ConstReserveIterator RBegin() const
{
return ConstReserveIterator(End());
}*/
Iterator Begin()
{
return Iterator(_head->_next);
}
ConstIterator Begin() const
{
return ConstIterator(_head->_next);
}
Node* Buynode(const T& x)
{
Node* newnode = new Node(x);
return newnode;
}
void Insert(Iterator pos, const T& x)//在pos位置前插入
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* temp = Buynode(x);
temp->_prev = prev;
prev->_next = temp;
temp->_next = cur;
cur->_prev = temp;
}
void Erase(Iterator& pos)
{
assert(pos != _head);
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
}
void PushBack(const T& data)
{/*
Node* temp = Buynode(data);
Node* tail = _head->_prev;
temp->_prev = tail;
tail->_next = temp;
temp->_next = _head;
_head->_prev = temp;*/
Insert(End(), data);
}
void PushFront(const T& data)
{
Insert(Begin(), data);
}
void PopBack()
{
Erase(--End());
}
void PopFront()
{
Erase(Begin());
}
void PrintList()
{
Iterator it = Begin();
while (it != End())
{
cout << *it << " ";
it++;
}
cout << endl;
}
protected:
Node* _head;
};
void TestList()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
//l.PrintList();
///*l.PopBack();
//l.PopBack();
//l.PopBack();
//l.PopBack();*/
//l.PushFront(8);
//l.PushFront(0);
//l.PrintList();
//l.PopFront();
//l.PopFront();
//l.PopFront();
//l.PrintList();
List<int>::ReserveIterator it = l.RBegin();
while (it != l.REnd())
{
cout << *it << " ";
it++;
}
cout << endl;
}