1.首先要了解到vs底层的list链表是带头双向循环的链表。
所以首先就要看成员变量

那么就说明我们还需要构造一个Node的结构体,(typedef一下就好了,名字不影响)

现在就可以完成间的push_back函数了。

1.list的iterator
我们之前模拟vector和string的时候就直接typedef T* iterator

但是这个list是双向的迭代器,这个可以在C++的官网上可以查询的。

其他类型也是可以查询的:

因为链表的指针注定是不可以简单的++就可以完成的,vector是用数组存储的,所以指针都是连续的,所以我们就必须想别的方法,C++牛逼的就是如果我们把iterator也封装成一个类/结构体,那么我们就可以干我们想的任何事情,因为他可以运算符重载,所以我们外边看着和vector一样的++,但是底层是完全不同的。这才是C++祖师爷强的地方,外面都是vector<>::iterator/list<>::iterator, 但是底层是天差地别。

2.list的const_iterator
有些人的想法是typedef const _list_iterator<T> const_iterator,但是使用的时候,Q::list<int>::const_iterator it = l.begin(); ------> 相当于 Q::list<int>:: const _list_iterator it = l.begin()
那么_list_iterator 这个类是不发改变的,但是const迭代器期望指向内容是不能修改的,这样设计的迭代器本身不能修改。必然是有_node = _node->_next。所以这不能和vector一样无脑的typedef的。
更有有说那么久重新设计一个_const_list_iterator的类就好了嘛。但是那就太过冗余了,因为里面只有部分的成员函数不一样,所以我们就不需要再设计一个类了,我们先看看他们两者类有什么区别,才能更好的统一。

这里只有返回值是不一样的,所以为什么不能将返回值再用一个模板来统一规划一下呢?

所以就变成了:

3._list_iterator类的再次优化

看一眼这个就知道要重载运算符的-> , 那就实现呗。


但是他实现的很奇怪:
但是如果是const类型怎么办?因为它的返回值是const T*, 所以我们就有必要再加一个类模板参数了: