目录
list的介绍
list的模拟实现
定义节点
有关遍历的重载运算符
list的操作实现
(1)构造函数
(2)拷贝构造函数
(3)赋值运算符重载函数=
(4)析构函数和clear成员函数
(5)尾插/头插和尾删/头删
(6)size成员函数
(7)在任意位置插入 (insert)
(8)任意位置删除(erase)
(9)迭代器
完整代码展示
vector和list的比较
1.排序
(1)list和vector排序
(2)list copy vector sort copy list sort和list
2.总结
list的介绍
(1)list类其实就是链表,但是它是双向链表。在数据结构中我们了解过双向链表的特点。下面我们回忆一下。
1.节点中具有两个指针。一个指针指向该节点的前一个节点,另一个指针指向该节点的下一个节点。
2.存在哨兵位。初始化的时候节点里的下一个节点和上一个节点都指向自己。
(2)STL中list的底层结构
list的模拟实现
定义节点
我们先定义双向链表的节点并初始化。
template <class T>
struct list_node
{
list_node* _next;
list_node* _prev;
T _data;
list_node(const T& x = T())
:_next(nullptr)
, _prev(nullptr)
, _data(x)
{
}
};
有关遍历的重载运算符
list容器有迭代器,那么就可以进行遍历,因此我们要可以++,--等运算符重载。而且在插入删除操作中我们常常需要 ‘.’ '->'对链表进行遍历。因为普通迭代器和const迭代器中只有operator*和operator->的返回值有区别,所以我们就在模板上多增加了两个模板参数。
代码如下:
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)
:_node(node)
{
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
//C++规定后缀调用需要有一个int型参数作为区分前缀与后缀调用的区别
Self& operator++()//前置++,先++再取值
{
_node=_node->_next;
return *this;
}
Self operator++(int)//后置++,先取值再++
{
Self tmp(*this);//取值
_node = _node->_next;
return tmp;//返回被取的值
}
Self operator--()
{
_node = _node->_prev;
return *this;
}
Self operator--(int)
{
Self tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator==(const Self& lt)
{
return _node == lt._node;//结构体变量用「.」来访问成员,而结构体指针用「->」来访问。
}
bool operator!=(const Self& lt)
{
return _node != lt._node;
}
};
list的操作实现
(1)构造函数
创建节点并把节点中的指针全部指向自己。
list()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
}
(2)拷贝构造函数
先构造a1,再把lt中的资源尾插给a1。
void empty_Init()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
}
//a1(a2),a1是新建的
list(const list<T>& lt)
{
empty_Init();
for (auto& e : lt)
{
push_back(e);
}
}
//initializer_list<T>
list(std::initializer_list<T> lt)
{
empty_Init();
for (auto& e : lt)
{
push_back(e);
}
}
两种拷贝构造的区别:
(3)赋值运算符重载函数=
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
//lt1=lt
list<T>& operator=(list<T>& lt)
{
swap(lt);
return *this;
}
看到这个代码我们就会想为什么运算符重载中的形参不加const呢?如果加了const就像 void swap(*this,const list<T>& y)一样,这样是会报错的,两边类型不同,swap函数是一个函数模板,只有一个模板参数,那么有人会说把这个改成类型相同的不就行了。但是我们知道const list<T>& lt中const修饰list<T>
类型,则lt
引用的对象(即 list<T>
)是常量对象,不能通过 lt
修改它的内容。它的值在初始化后就不能改变,而在swap函数中需要交换它们的资源,那么lt就需要改变。
(4)析构函数和clear成员函数
clear的作用只是清理链表的节点,只剩下哨兵位,并不会释放空间。
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
(5)尾插/头插和尾删/头删
void push_back(const T&x)
{
insert(end(), x);
}
void push_front(const T&x)
{
insert(begin(), x);
}
void pop_back()
{
erase(--end());//end()是哨兵位
}
void pop_front()
{
erase(begin());
}
(6)size成员函数
size_t size()const
{
return _size;
}
(7)在任意位置插入 (insert)
typedef list_iterator<T,T&,T*> iterator;
void insert(iterator pos, const T& x)
{
node* newnode = new node(x);
node*pre = pos._node;
node* prev = pre->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = pre;
pre->_prev = newnode;
_size++;
}
注意:pos的类型是list_iterator<T,T&,T*>,这个类中的成员变量只有_node,而_node的类型才是list_node,类型为list_node才有节点的成员变量。所以我们要node*pre = pos._node,而不能直接使用pos。
(8)任意位置删除(erase)
list中的erase也会有迭代器失效,所以我们需要返回下一个迭代器。
typedef list_iterator<T,T&,T*> iterator;
iterator erase(iterator pos)
{
assert(pos != end());
node* pre = pos._node;
node* prev = pre->_prev;
node* next = pre->_next;
delete pre;
prev->_next = next;
next->_prev = prev;
_size--;
return iterator(next);
}
(9)迭代器
iterator begin()
{
iterator it(_head->_next);//调用了list_iterator类模板的构造函数
return it;
}
iterator end()
{
iterator it(_head);
return it;
}
const_iterator begin()const
{
const_iterator it(_head->_next);
return it;
}
const_iterator end()const
{
const_iterator it(_head);
return it;
}
完整代码展示
#include<assert.h>
namespace slm
{
//创建节点
template <class T>
struct list_node
{
list_node* _next;
list_node* _prev;
T _data;
list_node(const T& x = T())
:_next(nullptr)
, _prev(nullptr)
, _data(x)
{
}
};
//实现运算符重载
template<class T,class Ref,class Ptr>
//template<class T,class T&,class T*>
struct list_iterator
{
typedef list_node<T> Node;
typedef list_iterator<T, Ref, Ptr> Self;
Node* _node;
list_iterator(Node* node)
:_node(node)
{
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
//C++规定后缀调用需要有一个int型参数作为区分前缀与后缀调用的区别
Self& operator++()//前置++,先++再取值
{
_node=_node->_next;
return *this;
}
Self operator++(int)//后置++,先取值再++
{
Self tmp(*this);//取值
_node = _node->_next;
return tmp;//返回被取的值
}
Self operator--()
{
_node = _node->_prev;
return *this;
}
Self operator--(int)
{
Self tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator==(const Self& lt)
{
return _node == lt._node;//结构体变量用「.」来访问成员,而结构体指针用「->」来访问。
}
bool operator!=(const Self& lt)
{
return _node != lt._node;
}
};
template<class T>
class list
{
typedef list_node<T> node;
typedef list_iterator<T,T&,T*> iterator;
typedef list_iterator<T,const T&,const T*> const_iterator;
public:
iterator begin()
{
iterator it(_head->_next);
return it;
}
iterator end()
{
iterator it(_head);
return it;
}
const_iterator begin()const
{
const_iterator it(_head->_next);
return it;
}
const_iterator end()const
{
const_iterator it(_head);
return it;
}
void empty_Init()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
}
list()
{
empty_Init();
}
//a1(a2)
list(const list<T>& lt)
{
empty_Init();
for (auto& e : lt)
{
push_back(e);
}
}
list(std::initializer_list<T> lt)
{
empty_Init();
for (auto& e : lt)
{
push_back(e);
}
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
//lt1=lt
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);
}
}
size_t size()const
{
return _size;
}
void push_back(const T&x)
{
insert(end(), x);
}
//在pos前插入
void insert(iterator pos, const T& x)
{
node* newnode = new node(x);
node*pre = pos._node;
node* prev = pre->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = pre;
pre->_prev = newnode;
_size++;
}
iterator erase(iterator pos)
{
assert(pos != end());
node* pre = pos._node;
node* prev = pre->_prev;
node* next = pre->_next;
delete pre;
prev->_next = next;
next->_prev = prev;
_size--;
return iterator(next);
}
void push_front(const T&x)
{
insert(begin(), x);
}
void pop_back()
{
erase(--end());//end()是哨兵位
}
void pop_front()
{
erase(begin());
}
private:
node* _head;
size_t _size;
};
}
vector和list的比较
1.排序
(1)list和vector排序
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
void test_op1()
{
srand(time(0));
const int N = 1000000;
list<int> lt1;
vector<int> v;
for (int i = 0; i < N; ++i)
{
auto e = rand() + i;
lt1.push_back(e);
v.push_back(e);
}
int begin1 = clock();
// vector排序
sort(v.begin(), v.end());
int end1 = clock();
//list排序
int begin2 = clock();
lt1.sort();
int end2 = clock();
printf("vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
int main()
{
test_op1();
return 0;
}
从结果中我们发现list排序比vector排序快了两倍多。
(2)list copy vector sort copy list sort和list
那如果我们先把list类资源拷贝构造给vector排序,排完序后又拷贝回list类,那结果会是如何呢?
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
void test_op2()
{
srand(time(0));
const int N = 10000;
list<int> lt1;
list<int> lt2;
for (int i = 0; i < N; ++i)
{
auto e = rand() + i;
lt1.push_back(e);
lt2.push_back(e);
}
int begin1 = clock();
// 拷贝vector
vector<int> v(lt2.begin(), lt2.end());
// 排序
sort(v.begin(), v.end());
// 拷贝回lt2
lt2.assign(v.begin(), v.end());
int end1 = clock();
int begin2 = clock();
lt1.sort();
int end2 = clock();
printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
int main()
{
test_op2();
return 0;
}
我们发现上面list先拷贝构造成vector类在排序,排完序后再拷贝回list的效率还是比直接list排序慢。
2.总结
vector | list | |
底 层 结 构 | 动态顺序表,一段连续空间 | 带头结点的双向循环链表 |
随 机 访 问 | 支持随机访问,访问某个元素效率O(1) | 不支持随机访问,访问某个元 素效率O(N) |
插 入 和 删 除 | 任意位置插入和删除效率低,需要搬移元素,时间 复杂度为O(N),插入时有可能需要增容,增容: 开辟新空间,拷贝元素,释放旧空间,导致效率更 低 | 任意位置插入和删除效率高, 不需要搬移元素,时间复杂度 为O(1) |
空 间 利 用 率 | 底层为连续空间,不容易造成内存碎片,空间利用 率高,缓存利用率高 | 底层节点动态开辟,小节点容 易造成内存碎片,空间利用率 低,缓存利用率低 |
迭 代 器 | 原生态指针 | 对原生态指针(节点指针)进行 封装 |
迭 代 器 失 效 | 在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效. 删除时,当前迭代器需要重新赋值否则会失效 | 插入元素不会导致迭代器失 效,删除元素时,只会导致当 前迭代器失效,其他迭代器不 受影响 |
使 用 场 景 | 需要高效存储,支持随机访问,不关心插入删除效率 | 大量插入和删除操作,不关心 随机访问 |