一:list:
list 简单点说就是c++标准库(STL)的一种容器,可以实现插入和删除的操作,实质和双向链表相同,但这个双向链表时带有头结点的双向链表。
二:迭代器
迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。然而迭代器有很多不同的能力,它可以把抽象容器和通用算法有机的统一起来。
我们来看下面一段代码:
.cpp文件
#include<iostream>
#include<list>
//#include"List.h"
using namespace std;
void TestList()
{
list<int> l;
l .push_back(1);
l.push_back(2);
l.push_back(3);
l.push_back(4);
//【)
list<int>::iterator it = l.begin();
while (it!= l.end())
{
cout << *it <<" ";
++it;
}
cout << endl;
}
迭代器通过对操作符*,++,--,->的操作可以实现C/C++操作链表时指针接口完成的相同的功能。简单点讲迭代器就是复杂指针。
我们发现在库中并没有很多的接口函数来实现,而是通过迭代器的来实现。接下来我们模拟实现再来仔细观察迭代器是如何完成这些操作的。
三:模拟实现:
.h文件
#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&x = T())
: _prev(NULL)
, _next(NULL)
, _data(x)
{}
};
template < class T, class Ref, class Ptr> //模板T,T&,T*;
//定义一个迭代器指针
struct __ListIterator
{
typedef __ListNode<T> Node; //重命名
typedef __ListIterator<T,Ref,Ptr>Self; //自定义类型
Node* _node;
__ListIterator(Node*node)
:_node(node)
{}
Ref operator*() //取节点的值并返回其引用
{
return _node->_data;
}
//前置++
Self& operator++() //运算符的重载(遍历链表)
{
_node = _node->_next;
return *this;
}
//后置++
Self &operator++(int)
{
Self tmp(*this);
operator++();
return tmp;
}
bool operator!=(const Self &s)
{
return _node != s._node;
}
bool operator==(const Self &s)
{
return _node == s._node;
}
};
//链表
template<class T>
class List
{
typedef __ListNode <T> Node;
public:
typedef __ListIterator<T, T&, T*> Iterator;
List()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//尾插
void PushBack(const T&x)
{
Node*tmp = new Node(x);
Node*tail = _head->_prev;
tail->_next = tmp;
tmp->_prev = tail;
tmp->_next = _head;
_head->_prev = tmp;
}
//找到第一个数据
Iterator Begin()
{
return Iterator(_head->_next);
}
//找到最后一个数据
Iterator End()
{
return Iterator(_head);
}
protected:
Node *_head;
};
void TestList1()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
List<int>::Iterator it1 = l.Begin();
while (it1 != l.End())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
}
四:反向迭代器
.cpp文件
#include<iostream>
#include<list>
using namespace std;
void TestList()
{
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.push_back(4);
//【)
list<int>::reverse_iterator it = l.rbegin();
while (it!= l.rend())
{
cout << *it << " ";
++it;
}
cout << endl;
}
int main()
{
TestList();
system("pause");
return 0;
}
前面我们大概理解迭代器相当于一个节点指针,通过指针_next访问下一个节点,指针_prev访问前一个节点,既然迭代器可以通过运算符的操作实现,那么反向的呢?
五:模拟实现:
.h文件
#pragma once
//定义一个节点结构体
#include<iostream>
using namespace std;
template<class T>
struct __ListNode
{
__ListNode *_prev;
__ListNode *_next;
T _data;
__ListNode(const T&x)
:_prev(NULL)
,_next(NULL)
,_data(x)
{}
};
//定义一个迭代器节点指针
template<class T,class Ref,class Ptr>
struct __ListIterator
{
typedef __ListNode<T> Node;//重命名
typedef __ListIterator <T,Ref,Ptr> Self;//自定义
typedef __ListIterator <T, Ref, Ptr> Iterator;//重命名
typedef Ref Reference;//模板T&
typedef Ptr Pointer;//模板T*
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)
{
Iterator tmp(*this);
_node = _node->_next;
return tmp;
}
//后置--
Self&operator--()
{
_node = _node->_prev;
return *this;
}
//前置--
Self&operator--(int)
{
Iterator tmp
_node = _node->_prev;
return *this;
}
bool operator !=(const Self&s)
{
return _node != (s._node);
}
bool operator ==(const Self&s)
{
return _node == (s._node);
}
};
//定义一个反向迭代器
template<class Iterator>
//template <class T>
struct ReverseIterator
{
ReverseIterator(Iterator iter)
:it(iter)
{}
typename Iterator::Reference operator*()
{
return *it;
}
typename Iterator::Pointer operator->()
{
return &(operator*());
}
ReverseIterator operator++()//--it;
{
--it;//
return *this;
}
ReverseIterator operator--()//++it;
{
++it;
return *this;
}
bool operator!=(const ReverseIterator <Iterator>&Rit)
{
return it != Rit.it;
}
Iterator it;
};
//链表
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 ReverseIterator<Iterator> ReverseIterator;//重命名
//typedef ConstreverseIterator<ConstIterator> ConstreverseIterator;
//构造一个头结点
List()
{
_head = new Node(T());
_head->_next = _head;
_head->_prev = _head;
}
void PushBack(const T&x)
{
Node *tmp = new Node(x);
Node *tail = _head->_prev;
tail->_next = tmp;
tmp->_prev = tail;
tmp->_next = _head;
_head->_prev = tmp;
}
//迭代器的begin()和end()
Iterator End()
{
return Iterator(_head);
}
Iterator Begin()
{
return Iterator(_head->_next);
}
ConstIterator Begin() const
{
return ConstIterator(_head->_next);
}
ConstIterator End() const
{
return ConstIterator(_head);
}
//反向迭代器的rbegin()和rend()
ReverseIterator Rbegin()
{
return ReverseIterator(--End());
}
ReverseIterator Rend()
{
return ReverseIterator(End());
}
//析构函数
~List()
{
Clear();
delete _head;
_head = NULL;
}
void Clear()
{
Iterator it = Begin();
while (it!=End())
{
Node *del = it._node;
++it;
delete del;
}
}
void Print(const List<int>&l)
{
List<int>::ConstIterator it = l.Begin();
while (it != l.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);
List<int>::ReverseIterator it = l.Rbegin();
while (it != l.Rend())
{
cout << *it << " ";
++it;
}
cout << endl;
l.Print(l);
}