文章目录
模拟实现list
STL的list是前面数据结构博客中写到的带头循环双向链表,所以这里实现也是按照库里的走
forward_list是单链表
list的结构
list的结构相比vector和string稍微麻烦一点,因为list是由一个一个结点构成的,所以要先在命名空间中
- 定义结点的类并且写好结点的构造函数(缺省参数传匿名结构体)
- 定义链表的类,定义哨兵位的头结点
//list的结点类
template<class T>
struct list_node
{
list_node<T>* _prev;
list_node<T>* _next;
T _data;
//结点的构造函数
list_node(const T& x = T()) //匿名结构体
:_prev(nullptr)
, _next(nullptr)
,_data(x)
{}
};
template<class T>
class list
{
typedef list_node<T> node;
public:
typedef _list_iterator<T,T&,T*> iterator;
typedef _list_iterator<T, const T&, const T*> const_iterator;
private:
node* _head; //哨兵位头结点
};
1. list的迭代器的实现(难)
List 的迭代器
迭代器有两种实现方式,具体应根据容器底层数据结构实现:
- 原生态指针,比如:vector
- 将原生态指针进行封装,因迭代器使用形式与指针完全相同,因此在自定义的类中必须实以下方法:
- 指针可以解引用,迭代器的类中必须重载operator*()
- 指针可以通过->访问其所指空间成员,迭代器类中必须重载oprator->()
- 指针可以++向后移动,迭代器类中必须重载operator++()与operator++(int)至于operator–()/operator–(int)释放需要重载,根据具体的结构来抉择,双向链表可以向前移动,所以需要重载,如果forward_list 就不需要重载–
- 迭代器需要进行是否相等的比较,因此还需要重载operator==()与operator!=()
很明显list的结点算是一个自定义类型,无法想string和vector来用原生指针来实现,只能实现原生指针的封装形式
1. 普通迭代器
template<class T>
struct _list_iterator
{
typedef _list_node<T> node;
typedef _list_iterator<T> self;
node* _node;
_list_iterator(node*n)
:_node(n)
{}
//解引用
T& operator*()
{
return _node->_data;
}
//迭代器支持移动
self& operator++()
{
_node = _node->_next;
return *this;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}
//迭代器支持比较
bool operator!=(const self& s)
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
2. const迭代器
那么对于const迭代器来说,vector是在解引用的时候直接加上const就可以了,但list明显不能像vector那么直接
对于const的list迭代器来说,因为本身是以类的方式进行,而const实际上就代表迭代器指向的内容不可改变,也就是说只需要改变普通迭代器的解引用运算符重载就可以了,因此我们实现const有两种思路可行,一是再写一个类,只将普通迭代器运算符重载的函数换成const类型,也就是这样:多加了一个const类型的迭代器的类。
template<class T>
struct _list_const_iterator
{
typedef _list_node<T> node;
typedef _list_const_iterator<T> self;
node* _node;
_list_const_iterator(node*n)
:_node(n)
{}
//解引用
const T& operator*()
{
return _node->_data;
}
//迭代器支持移动
self& operator++()
{
_node = _node->_next;
return *this;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}
//迭代器支持比较
bool operator!=(const self& s)
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
但我们发现这种方式会产生很多的代码冗余,因为除了解引用运算符重载,别的都没有变化,因此设计者在设计这里的时候用到了多个模板参数,通过传入的类型不同,就将这个迭代器的类转化成const的和非const
总结一下实现const的迭代器的两种方法:
- 重新写一个类,不过里面只有一个函数是不一样的,会造成代码冗余
- 利用模板参数, 将一个类通过传入的类型不同能够自动演化出不同的类[推荐]
3. 真正迭代器的实现
定义一个这样的类:
struct AA
{
int _a1;
int _a2;
AA(int a1 = 0, int a2 = 0)
:_a1(a1)
, _a2(a2)
{}
};
想要通过迭代器去访问内部元素时,只能这样访问:
list<AA> lt(1,1);
list<AA>::iterator it = lt.begin();
while(it != lt.end())
{
cout <<(*it)._a1<<":"<<(*it)._a2<<endl;
++it;
}
这样显得很别扭, 我们平常用习惯的是->
访问,所以需要我们去重载->
即可
cout << it->_a1 << ":" << it->_a2 << endl;
//cout << it.operator->()->_a1 << ":" << it.operator->()->_a1 << endl; //本来的样子
代码
需要考虑如何将这样的运算符也重载进去,只需要在类中再加一个模板参数,到现在三个模板参数,已经齐了。这也是我们最终的迭代器的类:
template<class T, class Ref, class Ptr>
struct _list_iterator
{
typedef list_node<T> node;
typedef _list_iterator<T,Ref,Ptr> self;
node* _node;
_list_iterator(node*n) //结点析构交给链表
:_node(n)
{}
//解引用
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
//迭代器支持移动
self& operator++()
{
_node = _node->_next;
return *this;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}
//迭代器支持比较
bool operator!=(const self& s)
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
2. list模拟实现完整代码
namespace yj
{
//list的结点类
template<class T>
struct list_node
{
list_node<T>* _prev;
list_node<T>* _next;
T _data;
//结点的构造函数
list_node(const T& x = T()) //匿名结构体
:_prev(nullptr)
, _next(nullptr)
,_data(x)
{}
};
//1. 迭代器要么就是原生指针
//2. 迭代器要么就是自定义类型对原生指针的封装, 模拟指针的行为
//类中实现迭代器 --- 用结点的指针来构造
template<class T, class Ref, class Ptr>
struct _list_iterator
{
typedef list_node<T> node;
typedef _list_iterator<T,Ref,Ptr> self;
node* _node;
_list_iterator(node*n) //结点析构交给链表
:_node(n)
{}
//解引用
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
self& operator++()
{
_node = _node->_next;
return *this;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
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 list_node<T> node;
public:
typedef _list_iterator<T,T&,T*> iterator;
typedef _list_iterator<T, const T&, const T*> const_iterator;
iterator begin()
{
//1. 构造迭代器返回
//iterator it(_head->_next);
//return it;
//使用匿名对象返回
return iterator(_head->_next);
}
iterator end() //指向最后一个结点的下一个
{
return iterator(_head);
}
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
//空初始化(开辟哨兵位的头结点)
void empty_list()
{
_head = new node;
_head->_prev = _head;
_head->_next = _head;
}
//链表的构造函数
list()
{
empty_list();
}
//迭代器区间构造
template <class Iterator>
list(Iterator first, Iterator last)
{
empty_list(); //开辟哨兵位头结点
while (first != last)
{
push_back(*first); //push_back前提是要有哨兵位的头结点
++first;
}
}
//拷贝构造 lt2(lt1)
//传统写法:
//list(const list<T>& lt)
//{
// empty_list();
// for (auto e : lt)
// {
// push_back(e); //一个一个尾插进去
// }
//}
//现代写法:
list(const list<T>& lt)
{
empty_list(); //要初始化this的头结点
list<T> tmp(lt.begin(), lt.end());
swap(tmp);
}
void swap(list<T>& tmp)
{
std::swap(_head, tmp._head);
}
//赋值重载 //lt1 = lt3
//不能传引用,传引用lt就是lt3,导致lt3会发生改变,lt应该是lt3的拷贝
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
~list() //释放掉哨兵位的头结点
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
//erase(it++); //也可以,erase的是返回迭代器对象(it的拷贝)
}
}
//可以自己写或者复用inert
void push_back(const T& x)
{
//node* new_node = new node(x);
//node* tail = _head->_prev;
//tail->_next = new_node;
//new_node->_prev = tail;
//new_node->_next = _head;
//_head->_prev = new_node;
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(end());
--end();
}
iterator insert(iterator pos, const T &x)
{
node* cur = pos._node; //在pos前插入
node* prev = cur->_prev;
node* new_node = new node(x);
prev->_next = new_node;
new_node->_next = cur;
new_node->_prev = prev;
cur->_prev = new_node;
return iterator(pos);
}
iterator erase(iterator pos)
{
assert(pos != end());
node* prev = pos._node->_prev;
node* next = pos._node->_next;
prev->_next = next;
next->_prev = prev;
delete pos._node;
return iterator(next);
}
private:
node* _head; //哨兵位头结点
};
}
3. list的迭代器失效
因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
void TestListIterator()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l(array, array + sizeof(array) / sizeof(array[0]));
auto it = l.begin();
while (it != l.end())
{
l.erase(it++); //或it = l.erase(it);
}
}
4. list与vector的对比
vector | list | |
---|---|---|
底层结构 | 动态顺序表,一段连续空间 | 带头结点的双向循环链表 |
随机访问 | 支持随机访问,访问某个元素效率O(1) | 不支持随机访问,访问某个元素效率O(N) |
插入和删除 | 任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低 | 任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1) |
空间利用率 | 底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高 | 底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低 |
迭代器 | 原生态指针 | 对原生态指针(节点指针)进行封装 |
迭代器失效 | 在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效 | 插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响 |
使用场景 | 需要高效存储,支持随机访问,不关心插入删除效率 | 大量插入和删除操作,不关心随机访问 |