【本节目标】
1. list的介绍及使用
2. list的深度剖析及模拟实现
3. list与vector的对比
1. list的介绍及使用
1.1 list的介绍
1. list
是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list
的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
3. list
与
forward_list
非常相似:最主要的不同在于
forward_list
是单链表,只能朝前迭代,已让其更简单高 效。
4.
与其他的序列式容器相比
(array
,
vector
,
deque)
,
list
通常在任意位置进行插入、移除元素的执行效率 更好。
5.
与其他序列式容器相比,
list
和
forward_list
最大的缺陷是不支持任意位置的随机访问,比如:要访问
list 的第6
个元素,必须从已知的位置
(
比如头部或者尾部
)
迭代到该位置,在这段位置上迭代需要线性的时间 开销;list
还需要一些额外的空间,以保存每个节点的相关联信息
(
对于存储类型较小元素的大
list
来说这 可能是一个重要的因素)
1.2 list的使用
ist
中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已达到可扩展 的能力。以下为list
中一些
常见的重要接口。
1.2.1 list的构造
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>
#include <vector>
// list的构造
void TestList1()
{
list<int> l1; // 构造空的l1
list<int> l2(4, 100); // l2中放4个值为100的元素
list<int> l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
list<int> l4(l3); // 用l3拷贝构造l4
// 以数组为迭代器区间构造l5
int array[] = { 16,2,77,29 };
list<int> l5(array, array + sizeof(array) / sizeof(int));
// 列表格式初始化C++11
list<int> l6{ 1,2,3,4,5 };
// 用迭代器方式打印l5中的元素
list<int>::iterator it = l5.begin();
while (it != l5.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// C++11范围for的方式遍历
for (auto& e : l5)
cout << e << " ";
cout << endl;
}
1.2.2 list iterator的使用
此处,大家可暂时
将迭代器理解成一个指针,该指针指向
list
中的某个节点
。
【注意】
1.
begin
与
end
为正向迭代器,对迭代器执行
++
操作,迭代器向后移动
2.
rbegin(end)
与
rend(begin)
为反向迭代器,对迭代器执行
++
操作,迭代器向前移动
// list迭代器的使用
// 注意:遍历链表只能用迭代器和范围for
void PrintList(const list<int>& l)
{
// 注意这里调用的是list的 begin() const,返回list的const_iterator对象
for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
// *it = 10; 编译不通过
}
cout << endl;
}
void TestList2()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l(array, array + sizeof(array) / sizeof(array[0]));
// 使用正向迭代器正向list中的元素
// list<int>::iterator it = l.begin(); // C++98中语法
auto it = l.begin(); // C++11之后推荐写法
while (it != l.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// 使用反向迭代器逆向打印list中的元素
// list<int>::reverse_iterator rit = l.rbegin();
auto rit = l.rbegin();
while (rit != l.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
}
1.2.3 list capacity
1.2.4 list element access
1.2.5 list modifiers
// list插入和删除
// push_back/pop_back/push_front/pop_front
void TestList3()
{
int array[] = { 1, 2, 3 };
list<int> L(array, array + sizeof(array) / sizeof(array[0]));
// 在list的尾部插入4,头部插入0
L.push_back(4);
L.push_front(0);
PrintList(L);
// 删除list尾部节点和头部节点
L.pop_back();
L.pop_front();
PrintList(L);
}
// insert /erase
void TestList4()
{
int array1[] = { 1, 2, 3 };
list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
// 获取链表中第二个节点
auto pos = ++L.begin();
cout << *pos << endl;
// 在pos前插入值为4的元素
L.insert(pos, 4);
PrintList(L);
// 在pos前插入5个值为5的元素
L.insert(pos, 5, 5);
PrintList(L);
// 在pos前插入[v.begin(), v.end)区间中的元素
vector<int> v{ 7, 8, 9 };
L.insert(pos, v.begin(), v.end());
PrintList(L);
// 删除pos位置上的元素
L.erase(pos);
PrintList(L);
// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
L.erase(L.begin(), L.end());
PrintList(L);
}
// resize/swap/clear
void TestList5()
{
// 用数组来构造list
int array1[] = { 1, 2, 3 };
list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
PrintList(l1);
// 交换l1和l2中的元素
list<int> l2;
l1.swap(l2);
PrintList(l1);
PrintList(l2);
// 将l2中的元素清空
l2.clear();
cout << l2.size() << endl;
}
list
中还有一些操作,需要用到时大家可参阅
list
的文档说明。
1.2.6 list的迭代器失效
前面说过,此处大家可将迭代器暂时理解成类似于指针,
迭代器失效即迭代器所指向的节点的无效,即该节
点被删除了
。因为
list
的底层结构为带头结点的双向循环链表
,因此
在
list
中进行插入时是不会导致
list
的迭代
器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响
。
void TestListIterator1()
{
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())
{
// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
l.erase(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);
}
}
2. list的模拟实现
2.1 模拟实现list
要模拟实现
list
,必须要熟悉
list
的底层结构以及其接口的含义,通过上面的学习,这些内容已基本掌握,现 在我们来模拟实现list
。
#pragma once
#include <assert.h>
#include <iostream>
namespace xyl
{
template<class T>
//list节点类
struct ListNode
{
ListNode(const T& val = T())
:_pPrev(nullptr)
, _pNext(nullptr)
,_val(val)
{
}
ListNode<T>* _pPrev;
ListNode<T>* _pNext;
T _val;
};
//List迭代器类
template<class T,class Ref,class Ptr>
struct ListIterator
{
typedef ListNode<T>* PNode;
typedef ListIterator<T, Ref, Ptr> Self;
ListIterator(PNode pNode = nullptr)
:_pNode(pNode)
{
}
ListIterator(const Self & l)
:_pNode(l._pNode)
{
}
T& operator*()
{
return _pNode->_val;
}
T* operator->()
{
return &(_pNode->_val);
}
Self& operator++()
{
_pNode = _pNode->_pNext;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
_pNode = _pNode->_pNext;
return tmp;
}
Self& operator--()
{
_pNode = _pNode->_pPrev;
return *this;
}
Self& operator--(int)
{
Self tmp(*this);
_pNode = _pNode->_pPrev;
return tmp;
}
bool operator==(const Self& l)
{
return _pNode == l._pNode;
}
bool operator!=(const Self& l)
{
return _pNode != l._pNode;
}
PNode _pNode;
};
//list类
template< class T >
class list
{
typedef ListNode<T> Node;
typedef Node* PNode;
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T,const T&, const T*> const_iterator;
public:
void init_empty()
{
_head = new Node;
_head->_pPrev = _head;
_head->_pNext = _head;
size = 0;
}
list()
{
init_empty();
}
list(int n, const T& val = T())
{
init_empty();
for (int i = 0;i < n;i++)
{
push_back(val);
}
}
template <class Iterator>
list(Iterator first, Iterator last)
{
init_empty();
while (first != last)
{
push_back(*first);
first++;
}
}
list(const list<T>& l)
{
init_empty();
const_iterator it = l.begin();
while (it != l.end())
{
push_back(*it);
it++;
}
}
~list()
{
clean();
delete _head;
_head = nullptr;
}
void clean()
{
iterator it = begin();
while (it != end())
{
it=erase(it);
}
_head->_pNext = _head;
_head->_pPrev = _head;
}
list<T>& operator=(const list<T>& l)
{
list<T> tmp(l);
swap(tmp);
return *this;
}
size_t _size()
{
return size;
}
bool empty()
{
return size == 0;
}
T& front()
{
return _head->_pNext->_val;
}
T& back()
{
return _head->_pPrev->_val;
}
const T& front() const
{
return _head->_pNext->_val;
}
const T& back() const
{
return _head->_pPrev->_val;
}
void push_back(const T& val)
{
/*PNode cur= new Node(val);
PNode tail = _head->_pPrev;
cur->_pPrev = tail;
cur->_pNext = _head;
tail->_pNext = cur;
_head->_pPrev = cur;
size++;*/
insert(end(), val);
}
void push_front(const T& val)
{
insert(begin(), val);
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
iterator insert(iterator pos, const T& val)
{
PNode newnode = new Node(val);
PNode cur = pos._pNode;
PNode prev = cur->_pPrev;
prev->_pNext = newnode;
newnode->_pPrev = prev;
newnode->_pNext = cur;
cur->_pPrev = newnode;
size++;
return iterator(newnode);
}
iterator erase(iterator pos)
{
//找到删除节点
PNode del = pos._pNode;
//存储删除节点的下一位当返回值
PNode tmp = del->_pNext;
PNode next = del->_pNext;
PNode prev = del->_pPrev;
prev->_pNext = next;
next->_pPrev = prev;
delete del;
size--;
return iterator(tmp);
}
iterator begin()
{
return (_head->_pNext);
}
iterator end()
{
return (_head);
}
const_iterator begin() const
{
return (_head->_pNext);
}
const_iterator end() const
{
return (_head);
}
void swap(list<T>& l)
{
std::swap(_head, l._head);
std::swap(size, l.size);
}
PNode _head;
size_t size;
};
}
#include <iostream>
#include "list.h"
using namespace std;
void test_list1()
{
xyl::list<int> list1;
list1.push_back(1);
list1.push_back(2);
list1.push_back(3);
list1.push_back(4);
xyl::list<int>:: iterator it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
cout << list1.size;
cout << endl;
xyl::list<int> list2(10,1);
xyl::list<int>::iterator it2 = list2.begin();
while (it2 != list2.end())
{
cout << (*it2) << ' ';
++it2;
}
cout << endl;
}
void test_list2()
{
xyl::list<int> list1;
list1.push_back(1);
list1.push_back(2);
list1.push_back(3);
list1.push_back(4);
xyl::list<int>::iterator it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
xyl::list<int> list2(++list1.begin(),list1.end());
xyl::list<int>::iterator it2 = list2.begin();
while (it2 != list2.end())
{
cout << (*it2) << ' ';
++it2;
}
cout << endl;
xyl::list<int> list3(list2);
xyl::list<int>::iterator it3 = list3.begin();
while (it3 != list3.end())
{
cout << (*it3) << ' ';
++it3;
}
cout << endl;
}
void test_list3()
{
xyl::list<int> list1;
list1.push_back(1);
list1.push_back(2);
list1.push_back(3);
list1.push_back(4);
xyl::list<int>::iterator it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
xyl::list<int> list2;
list2 = list1;
xyl::list<int>::iterator it2 = list2.begin();
while (it2 != list2.end())
{
cout << (*it2) << ' ';
++it2;
}
cout << endl;
cout << list2.front() << endl;
cout << list2.back() << endl;
}
void test_list4()
{
xyl::list<int> list1;
list1.push_back(1);
list1.push_back(2);
list1.push_back(3);
list1.push_back(4);
list1.erase(list1.begin());
xyl::list<int>::iterator it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
list1.clean();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
}
void test_list5()
{
xyl::list<int> list1;
list1.push_back(1);
list1.push_back(2);
list1.push_back(3);
list1.push_back(4);
list1.insert(list1.begin(), 3);
xyl::list<int>::iterator it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
list1.pop_back();
it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
list1.pop_front();
it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
list1.push_front(5);
it1 = list1.begin();
while (it1 != list1.end())
{
cout << (*it1) << ' ';
++it1;
}
cout << endl;
}
int main()
{
test_list5();
return 0;
}
2.2 list的反向迭代器
通过前面例子知道,反向迭代器的
++
就是正向迭代器的
--
,反向迭代器的
--
就是正向迭代器的
++
,因此反向迭 代器的实现可以借助正向迭代器,即:反向迭代器内部可以包含一个正向迭代器,对正向迭代器的接口进行 包装即可。
template<class Iterator>
class ReverseListIterator
{
// 注意:此处typename的作用是明确告诉编译器,Ref是Iterator类中的类型,而不是静态成员变量
// 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量
// 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的
public:
typedef typename Iterator::Ref Ref;
typedef typename Iterator::Ptr Ptr;
typedef ReverseListIterator<Iterator> Self;
public:
//
// 构造
ReverseListIterator(Iterator it) : _it(it) {}
//
// 具有指针类似行为
Ref operator*() {
Iterator temp(_it);
--temp;
return *temp;
}
Ptr operator->() { return &(operator*()); }
//
// 迭代器支持移动
Self& operator++() {
--_it;
return *this;
}
Self operator++(int) {
Self temp(*this);
--_it;
return temp;
}
Self& operator--() {
++_it;
return *this;
}
Self operator--(int)
{
Self temp(*this);
++_it;
return temp;
}
//
// 迭代器支持比较
bool operator!=(const Self& l)const { return _it != l._it; }
bool operator==(const Self& l)const { return _it != l._it; }
Iterator _it;
};
3. list与vector的对比
vector
与
list
都是
STL
中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不 同,其主要不同如下: