⭐️今天我先为大家介绍STL中的list容器,我会先介绍它的一些个常见接口以及用法,然后再模拟实现它,其中list的迭代器相比前两个容器的来说更为复杂,所以我会更加详细地介绍它,这样我们就能够比较深入地了解这个容器。
⭐️博客代码已上传至gitee:https://gitee.com/byte-binxin/cpp-class-code
list的本质是一个带头的双向循环链表。
总结几点:
实例演示
void TestList1()
{
list<int> lt1;// 无参构造
list<int> lt2(10, 5);// 用n个val构造一个list对象
list<int> lt3(lt2);// 拷贝构造
list<int> lt4(lt2.begin(), lt2.end());// 用一段区间的元素构造list
cout << "lt1:";
PrintList(lt1);
cout << "lt2:";
PrintList(lt2);
cout << "lt3:";
PrintList(lt3);
cout << "lt4:";
PrintList(lt4);
}
实例演示
void TestList2()
{
list<int>lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_front(0);
lt.push_front(-1);
lt.push_front(-2);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
list<int>::reverse_iterator rit = lt.rbegin();
while (rit != lt.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
}
实例演示
void TestList3()
{
list<int>lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_front(0);
lt.push_front(-1);
lt.push_front(-2);
cout << "删除前:";
PrintList(lt);
lt.erase(lt.begin());
cout << "删除后:";
PrintList(lt);
lt.insert(lt.begin(), 0);
cout << "插入一个数据后:";
PrintList(lt);
}
迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
两种情况测试
void TestList4()
{
int arr[] = { 1,2,3,4,5 };
list<int> lt(arr, arr + sizeof(arr) / sizeof(arr[0]));
list<int>::iterator it = lt.begin();
lt.insert(it, 3);
}
void TestList4()
{
int arr[] = { 1,2,3,4,5 };
list<int> lt(arr, arr + sizeof(arr) / sizeof(arr[0]));
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
lt.erase(it);
++it;
}
}
代码运行结果如下:
总结: 插入数据不会导致迭代器失效,删除数据会导致迭代器失效。相比vector容器,vector容器插入数据是会导致迭代器失效,因为vector涉及增容问题,而list却不存在增容问题,所以迭代器指向的位置是有效的。删除数据会导致迭代器指向的位置是无效的,所以迭代器会失效。
修改后的代码如下:
void TestList4()
{
int arr[] = { 1,2,3,4,5 };
list<int> lt(arr, arr + sizeof(arr) / sizeof(arr[0]));
list<int>::iterator it = lt.begin();
//lt.insert(it, 3);
while (it != lt.end())
{
it = lt.erase(it);
}
}
list是由节点组成,所以定义一个节点的类,然后list的类中成员只需要一个头结点的指针即可。
template<class T>
struct __list_node
{
__list_node<T>* _prev;
__list_node<T>* _next;
T _data;
__list_node(const T& x = T())
:_next(nullptr)
, _prev(nullptr)
, _data(x)
{}
};
template<class T>
class list
{
typedef __list_node<T> Node;
public:
private:
Node* _head;
};
构造函数要做的任务就是开一个头结点,所以我们可以封装出一个具体的函数来实现创建头结点的这个过程。
创建头结点:
void CreatHead()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
构造函数的实现:
list()
{
CreatHead();
}
list相比vector的迭代器而言,不再是一个简单的指针,它相对而言更复杂一些,list的迭代器为了实现一些简单的功能,我们把它封装成了一个类。看下面源码实现:
我们自己来模拟实现一下简单的。
迭代器的小框架(里面有一个成员变量——节点指针)
struct __list_iterator
{
typedef __list_node<T> Node;
__list_iterator(Node* node = nullptr)
:_node(node)
{}
Node* _node;
}
由于迭代器分普通迭代器和const 迭代器,为了不造成代码冗余,我们设计出来三个模板参数,根据传入的模板参数确定是那种迭代器。
迭代器的实现
// __list_iterator -> 普通迭代器
// __list_iterator -> const迭代器
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* node = nullptr)
:_node(node)
{}
__list_iterator(const Self& l)
:_node(l._node)
{}
// *it T&
Ref operator*()
{
return _node->_data;
}
// it-> T*
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;
++(*this);
return tmp;
}
Self operator--(int)
{
Self tmp(*this);
//_node = _node->_prev;
--(*this);
return tmp;
}
Self operator+(int count)
{
Self tmp(*this);
while (count--)
{
++tmp;
}
return tmp;
}
Self operator-(int count)
{
Self tmp(*this);
while (count--)
{
--tmp;
}
return tmp;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
};
我们还要在list里面做这样一个操作(堆两种迭代器进行重命名,方便我们认识):
typedef __list_iterator<T, T&, T*> iterator;// 普通迭代器
typedef __list_iterator<T, const T&, const T*> const_iterator;// const迭代器
list内部begin()和end()的实现(普通迭代器调用前两个,const迭代器调用后两个)
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);
}
这里几个接口其实我们再之前数据结构的时候都实现过,比较简单
void push_back(const T& x)
{
Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
}
void pop_back()
{
assert(_head != _head->_next);
Node* tail = _head->_prev;
Node* prevTail = tail->_prev;
delete tail;
tail = prevTail;
tail->_next = _head;
_head->_prev = tail;
}
void push_front(const T& x)
{
Node* newnode = new Node(x);
Node* firstNode = _head->_next;
_head->_next = newnode;
newnode->_prev = _head;
newnode->_next = firstNode;
firstNode->_prev = newnode;
}
void pop_front()
{
assert(_head->_next != _head);
Node* firstNode = _head->_next;
Node* secondNode = firstNode->_next;
delete firstNode;
firstNode = nullptr;
_head->_next = secondNode;
secondNode->_prev = _head;
}
void insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
}
注意,为了提高代码的复用性,push_front和push_back都可以复用insert
void push_back(const T& x)
{
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator erase(iterator pos)
{
assert(_head->_next != _head);
assert(pos != end());
Node* node = pos._node;
Node* prev = node->_prev;
Node* next = node->_next;
delete node;
node = nullptr;
prev->_next = next;
next->_prev = prev;
return iterator(next);
}
同样地,为了提高代码的复用性,pop_front和pop_back都可以复用erase
void pop_back()
{
erase(end() - 1);
}
void pop_front()
{
erase(begin());
}
T front()
{
assert(_head->_next != _head);
return _head->_next->_data;
}
T back()
{
assert(_head->_next != _head);
return _head->_prev->_data;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
list(const list<T>& lt)
{
CreatHead();
/*const_iterator it = lt.begin();
while (it != lt.end())
{
push_back(*it);
++it;
}*/
for (auto e : lt)
push_back(e);
}
list<T>& operator=(list<T> lt)
{
if (this != <)// 防止自己给自己赋值
{
swap(lt);
}
return *this;
}
swap函数实现如下:
void swap(list<T>& lt)
{
::swap(_head, lt._head);
}
这里对比和之前数据结构中顺序表和单链表的对比是一样的,如下:
vector | list | |
---|---|---|
底层结构 | 动态顺序表,一段连续的空间 | 带头双向循环链表 |
随机访问 | 支持,时间复杂度是O(1) | 不支持,时间复杂度是O(N) |
插入和删除 | 效率低,时间复杂度是O(N) | 效率高,时间复杂度是O(1) |
空间利用率 | 底层是连续的空间,不容易造成内存碎片化 | 底层动图开辟,小结点容易造成内存碎片,空间利用率低 |
迭代器 | 原生指针 | 堆原生态指针进行封装 |
迭代器失效 | 插入和删除都会造成,所以最后每次操作后堆迭代器重新赋值 | 插入数据不会,删除数据会造成迭代器失效 |
使用场景 | 需要高存储效率,支持随机访问,不关心插入删除效率 | 大量插入删除操作,不关心随机访问 |