第二章 STL
基本概念
1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list的底层是双向带头循环链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
list的使用与vector差不多,一样的方法名
list的节点结构
template<class T>
struct __list_node
{
__list_node* _prev; // 也可以写成这样 __list_node<T>* _prev;
__list_node* _next;
T _data;
// 构造函数
__list_node(const T& x = T())
:_prev(nullptr)
,_next(nullptr)
,_data(x)
{}
};
list类封装
template<class T>
class list
// 带头双向循环链表
{
typedef __list_node<T> Node; // 将节点重命名
public:
...... 各种方法
private:
Node* _head;
};
迭代器实现
list的迭代器,它是内嵌类型的,是在某个类。并且它是行为是像指针一样的!在上层中,每种迭代器的使用方法几乎一样,但是其底层实现的方法压根不一样!
// 迭代器的实现
// __list_iterator <T, T&, T* -> iterator
// __list_iterator < T,const T&,const T*> -> cosnt_iterator
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)
{}
// *it
/*T& operator*()
{
return _node->_data;
}*/
Ref operator*()
{
return _node->_data;
}
/*T* operator ->()
{
return &_node->_data;
}*/
Ptr operator ->()
{
return &_node->_data;
}
// ++it
// 返回的是迭代器
Self& operator++()
{
_node = _node->_next;
return *this;
}
// --it
Self& operator--()
{
_node = _node->_prev;
return *this;
}
// it++
Self operator++(int)
{
Self tmp(*this);
_node = _node->_next; // ++(*this) 效果一样
return tmp;
}
// it--
Self operator--(int)
{
Self tmp(*this);
_node = _node->_prev; // --(*this) 效果一样
return tmp;
}
/*it != end()*/
bool operator!=(const Self& end)
{
return _node != end._node;
}
bool operator==(const Self& end)
{
return _node == end._node;
}
};
list类中
//typedef __list_iterator<T> iterator;
typedef __list_iterator<T, T&, T*> iterator;
// 实现const_iterator
typedef __list_iterator<T,const T&,const T*> const_iterator;
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);
}
迭代器类中的 operator->
返回的是T的地址,比如T是一个日期的数据,那就是返回Date*,但当我们去调用这个函数并且取其数据的时候,按道理来说,因该会有两个箭头:
Date类:
struct Date
{
int _year = 0;
int _month = 1;
int _day = 1;
};
hek::list<hek::Date> ll;
hek::list<hek::Date> l1;
l1.push_back(hek::Date());
l1.push_back(hek::Date());
hek::list<hek::Date>::iterator it = l1.begin();
while (it != l1.end())
{
// 这么写不需要重载
//cout << (*it)._year << '-' << (*it)._month << '-' << (*it)._day;
// 需要重载 ->
// 这里原本应该是 it->->_year,但是为了可读性,编译器特殊处理了
// ( it-> ) 代表 ( it.operator->() ) Date* 类型
cout << it->_year << '-' << it->_month << '-' << it->_day;
cout << endl;
++it;
}
但实际上我们在写的时候却不是这样,我们只需要写一个箭头即可,因为编译器为了可读性,做了特殊的处理,省略掉了一个箭头。
此时,就完成了我们模拟实现list中迭代器
迭代器失效问题
vector的迭代器失效是在insert和erase,即插入和删除数据都有可能导致迭代器失效,因为位置发生了变化。
list的迭代器失效是在erase,即删除节点的时候会导致迭代器失效,因为迭代器指向的节点被删除空间也被释放了。
list的默认成员函数
list()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
// l2(l1)
list(const list<T>& lt)
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
const_iterator it = lt.begin();
while (it != lt.end())
{
//this->push_back(*it);
push_back(*it);
it++;
}
/*for (auto& i : lt)
{
push_front(i); // 倒着插
}*/
}
// l2 = l1
//list<T>& operator=(const list<T>& lt)
//{
// clear();
// const_iterator it = lt.begin();
// while (it != lt.end())
// {
// //this->push_back(*it);
// push_back(*it);
// it++;
// }
// return *this;
//}
list<T>& operator=(list<T> lt)
{
swap(_head, lt._head);
return *this;
}
void clear()
{
iterator it = begin();
while (it != end())
{
erase(it++);
}
}
~list()
{
clear(); // 先删掉所有数据 不然会内存泄露
delete[] _head;
_head = nullptr;
}
list的增删改查
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
void insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
// prev newnode cur
Node* newnode = new Node(x);
prev->_next = newnode;
newnode->_next = cur;
newnode->_prev = prev;
cur->_prev = newnode;
}
// erase -> 擦除
void erase(iterator pos)
{
// 头结点不能删
assert(pos != end());
Node* del = pos._node;
Node* prev = del->_prev;
Node* cur = del->_next;
prev->_next = cur;
cur->_prev = prev;
delete del;
}
总结
vector
优点:
- 下标的随机访问
- 尾插尾删效率高
- CPU高速缓存命中率高,因为物理空间连续
缺点:
- 随机插入或删除数据效率低
- 扩容有消耗 ,还会存在一定的空间浪费
list
优点:
- 按需申请空间,无需扩容
- 任意位置插入或删除,时间复杂度是O(1)
缺点:
- 不支持下标随机访问
- CPU高速缓冲命中率低
补充:list自带的函数
sort
之前的vector类,用到的是算法库的排序sort,但在官方文档中发现list自带一个排序函数:
由于list是链表结构,而算法库中的排序的底层是快速排序,不能实现链表的排序,因此设计了一个list自带的排序,通过前面的学习,list排序有纯粹的暴力插入排序,也有更好的归并排序,而这个list的sort的底层就是归并排序。
unique
去重函数,调用unique的前提是数据必须有序。