一、list的底层原理
STL list 容器的底层是用双向链表实现的,甚至一些 STL 版本中(比如 SGI STL),list 容器的底层实现使用的是双向循环链表。
如图 1 所示,使用链表存储数据,并不会将它们存储到一整块连续的内存空间中。恰恰相反,各元素占用的存储空间(又称为节点)是独立的、分散的,它们之间的线性关系通过指针(图 1 以箭头表示)来维持。
所以STL list的结点(node)结构:
tempalte <class T>
struct _list_node{
typedef void* void_pointer;
void_pointer prev; //型别为void*。其实可设为_list_node<T>*
void_pointer next;
T data;
}
list的数据结构:
template<class T,class Alloc = alloc> //缺省使用alloc为配置器
class list{
protected:
typedef _list_node<T> list_node;
public:
typedef list_node* link_type;
protected:
link_type node;
...
}
二、list的迭代器
list不能够再像vector一样以普通指针作为迭代器,因为其节点不保证在储存空间中连续存在。list 迭代器必须有能力指向list 的节点,并有能力进行正确的递增、递减、取值、成员存取等操作。所谓“list 迭代器正确的递增、递减、取值、成员取用”操作是指,递增时指向下一个结点,递减时指向上一个结点,取值时取的是节点的数据值,成员取用时是取用的是节点的成员。
list 还有一个重要的性质:插入(insert)和结合(splice)都不会造成原有的list迭代器失效。这在vector 是不成立的,因为vector的插入操作可能造成记忆体重新配置,导致原有的迭代器全部失效。甚至list 的元素删除操作(erase),也只有“指向被删除元素”的那个迭代器失效,其它迭代器不受影响。
1.STL库中的结构
如下图SGI STL的源码,STL对迭代器进行了封装:
而封装的结构如下:
可以发现并没有增加过多的元素,还是对指针进行了封装,不过在这个类中重载了一些函数,以使其具有迭代器的性质。
2.迭代器的具体实现
以下是STL中list的迭代器的实现:
- iterator的++和--操作是通过访问prev和next指针实现的
- iterator是用节点来进行构造的
- operator->的调用会被编译器优化
template<class T, class Ref, class Ptr>
struct __list_iterator {
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef __list_iterator<T, Ref, Ptr> self;
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef __list_node<T>* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
link_type node;
__list_iterator(link_type x) : node(x) {}
__list_iterator() {}
__list_iterator(const iterator& x) : node(x.node) {}
bool operator==(const self& x) const { return node == x.node; }
bool operator!=(const self& x) const { return node != x.node; }
reference operator*() const { return (*node).data; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
self& operator++() {
node = (link_type)((*node).next);
return *this;
}
self operator++(int) {
self tmp = *this;
++*this;
return tmp;
}
self& operator--() {
node = (link_type)((*node).prev);
return *this;
}
self operator--(int) {
self tmp = *this;
--*this;
return tmp;
}
};
对于operator-> :
3.反向迭代器
我们先看STL的设计:
我们发现源码是通过正向迭代器来初始化反向迭代器的,所以它们是对称的结构。而此时的解引用就要进行特殊处理,所以发现反向迭代器的解引用是先--再解引用的。
三、模拟实现list
这里我们只实现了常用的增删查改操作函数。
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace myList
{
template<class T>
struct list_node
{
list_node<T>* _next;
list_node<T>* _prev;
T _data;
list_node(const T& x)
:_prev(nullptr)
, _next(nullptr)
, _data(x)
{}
};
template<class T, class Ref, class Ptr>
struct _list_iterator
{
typedef list_node<T> node;
typedef _list_iterator<T, Ref, Ptr> Self;
node* _pnode;
_list_iterator(node* p)
:_pnode(p)
{}
Ptr operator->() { return &_pnode->_data; }
Ref operator*() { return _pnode->_data; }
Self& operator++() { _pnode = _pnode->_next; return *this; }
Self& operator++(int) { Self tmp(*this); _pnode = _pnode->_next; return tmp; }
Self& operator--() { _pnode = _pnode->_prev; return *this; }
Self& operator--(int) { Self tmp(*this); _pnode = _pnode->_prev; return tmp; }
bool operator!=(const Self& it)const { return _pnode != it._pnode; }
bool operator == (const Self& it)const { return _pnode == it._pnode; }
};
template<class T>
class list
{
public:
typedef list_node<T> node;
typedef _list_iterator<T, T&, T*>iterator;
typedef _list_iterator<T, const T&, const T*>const_iterator;
private:
node* _head;
size_t _size;
public:
iterator begin() { 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); }
list() { initialize(); }
void swap(list<T>& it) { std::swap(_head, it._head); std::swap(_size, it._size); }
list<T>& operator=(list<T> it) { swap(it); return *this; }
size_t size()const { return _size; }
bool empty()const { return _size == 0; }
~list() { clear(); delete _head; _head = nullptr; }
void push_back(const T& x) { insert(end(), x); }
void push_front(const T& x) { insert(begin(), x); }
void pop_back() { erase(--end()); }
void pop_front() { erase(begin()); }
void initialize()
{
_head = new node(T());
_head->_next = _head;
_head->_prev = _head;
_size = 0;
}
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
initialize();
while (first != last)
{
push_back(*first);
first++;
}
}
list(const list<T>& it)
{
initialize();
list<T> tmp(it.begin(), it.end());
swap(tmp);
}
void clear()
{
iterator it = begin();
while (it != end()) it = erase(it);
}
iterator erase(iterator pos)
{
assert(pos != end());
node* prev = pos._pnode->_prev;
node* next = pos._pnode->_next;
prev->_next = next;
next->_prev = prev;
delete pos._pnode;
--_size;
return iterator(next);
}
iterator insert(iterator pos, const T& x)
{
node* newnode = new node(x);
node* cur = pos._pnode;
node* prev = cur->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
};
}