STL——list
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
来说这可能是一个重要的因素)
2、list用法

list的接口有很多,具体如下

具体用法可以查看 https://legacy.cplusplus.com/reference/list/list/?kw=list
下面我介绍一些常用的接口的用法
2.1 list的构造
|
构造函数(constructor) | 接口说明 |
list (size_type n, const value_type& val = value_type()) | 构造的 |
list() | 构造空的 |
list (const list& x) | 拷贝构造函数 |
list (InputIterator first, InputIterator last) | 用 |
// list的构造void TestList1(){ list l1; // 构造空的l1 list l2(4, 100); // l2中放4个值为100的元素 list l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3 list l4(l3); // 用l3拷贝构造l4 // 以数组为迭代器区间构造l5 int array[] = { 16,2,77,29 }; list l5(array, array + sizeof(array) / sizeof(int)); // 列表格式初始化C++11 list l6{ 1,2,3,4,5 }; // 用迭代器方式打印l5中的元素 list::iterator it = l5.begin(); while (it != l5.end()) { cout << *it << " "; ++it; } cout << endl; // C++11范围for的方式遍历 for (auto& e : l5) cout << e << " "; cout << endl;}2.2list iterator的使用
迭代器底层是使用指针实现的,所以,我们可以把迭代器当成一个指针,指向list的某个结点。所有的容器的迭代器都被重命名为iterator
|
函数声明 | 接口说明 |
begin | 返回第一个元素的迭代器 |
rbegin rend | 返回第一个元素的 reverse_iterator, |
【注意】
1.
begin
与
end
为正向迭代器,对迭代器执行
++
操作,迭代器向后移动
2.
rbegin(end)
与
rend(begin)
为反向迭代器,对迭代器执行
++
操作,迭代器向前移动
//iterator
void PrintList(const list& l)
{
for (list::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
// *it = 10; 这里是const_iterator ,it指向的内容不能被修改,所以编译不通过
}
cout << endl;
}
void TestList2()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list l(array, array + sizeof(array) / sizeof(array[0]));
// 使用正向迭代器正向list中的元素
// list::iterator it = l.begin();
auto it = l.begin();
while (it != l.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// 使用反向迭代器逆向打印list中的元素
// list::reverse_iterator rit = l.rbegin();
auto rit = l.rbegin();
while (rit != l.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
}
2.3list modifiers
|
函数声明 | 接口说明 |
push_front | 在 |
pop_front | 删除 |
push_back | 在 |
pop_back | 删除 |
insert | 在 |
erase | 删除 |
swap | 交换两个 |
clear | 清空 |
void TestList1(){ int array[] = { 1, 2, 3 }; list 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 TestList2(){ int array1[] = { 1, 2, 3 }; list 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 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/clearvoid TestList3(){ // 用数组来构造list int array1[] = { 1, 2, 3 }; list l1(array1, array1 + sizeof(array1) / sizeof(array1[0])); PrintList(l1); // 交换l1和l2中的元素 list l2; l1.swap(l2); PrintList(l1); PrintList(l2); // 将l2中的元素清空 l2.clear(); cout << l2.size() << endl;}2.4list的迭代器失效
前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节
点被删除了
。因为
list
的底层结构为带头结点的双向循环链表
,因此
在
list
中进行插入时是不会导致
list
的迭代
器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
//这是一个模拟List的clear的实现
//这是错误写法
void clear()
{
iterator it = begin();
while (it != end())
{
erase(it);
it++;
}
}
//这段代码之所以错误,主要是因为,it指向的那个结点已经被删除了,所以it再++是找不到后面节点的位置的。
///
//正确写法
void clear()
{
iterator it = begin();
while (it != end())
{
it=erase(it);//删除这个结点,返回这个结点的下一个位置给it。
}
}
3、list的模拟实现
3、1node类模板
#include
#include
using namespace std;
namespace A
{
//用命名区间A把自己实现的list圈起来,防止与库里面的混淆
........
}
首先先构造出结点的类模板list_node,因为struct默认所有成员都是public,我们需要在类外面使用list_node,所以这里使用struct。定义三个成员变量 _next下一个节点,_pre上一个结点和数据data。
template
struct list_node
{
typedef list_node Node;
Node* _next;
Node* _pre;
T _data;
list_node(const T& val=T())
:_next(nullptr)
,_pre(nullptr)
,_data(val)
{
}
};
3、2迭代器类模板
l链表的物理结构并不是连续的,它不像string、vector的结构,对list进行++时找不到它的下一个结点的。所以我们必须自己模拟出它的迭代器。
定义一个迭代器类模板,在里面服用list_node类,定义出迭代器的成员变量_node。在里面实现出我们需要用的运算符
template
struct __list_iterator
{
typedef list_node Node;
typedef __list_iterator self;
__list_iterator(Node* node)
:_node(node)
{
}
Node* _node;
self& operator++()
{
_node = _node->_next;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self& operator--()
{
_node = _node->_pre;
return *this;
}
self operator--(int)
{
self tmp(*this);
_node = _node->pre;
return tmp;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator != (const self& s)
{
return _node != s._node;
}
};
3、3list类模板
实现出list的迭代器,我们就可以正式来模拟list接口了。
首先定义一个哨兵位结点,这也是list类模板唯一的成员变量。在此我们复用list_node类模板和迭代器类模板,然后我们实现list的各个接口
template
class list
{
typedef list_node Node;
public:
typedef __list_iterator iterator;
typedef __list_iterator const_iterator;
void init()
{
_head = new Node;
_head->_next = _head;
_head->_pre = _head;
}
list()
{
init();
}
void clear()
{
iterator it = begin();
while (it != end())
{
it=erase(it);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void swap(list& lt)
{
std::swap(_head, lt._head);
}
list(const list& lt)//构造
{
_head = new Node;
_head->_next = _head;
_head->_pre = _head;
for (auto e : lt)
{
push_back(e);
}
}
list& operator=(list lt)
{
swap(lt);
return *this;
}
void push_back(const T& x)
{
Node* tail = _head->_pre;
Node* newnode = new Node(x);
tail->_next = newnode;
newnode->_pre = tail;
newnode->_next = _head;
_head->_pre = newnode;
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator insert(iterator pos,const T& x)
{
Node* newnode = new Node(x);
Node* cur = pos._node;
Node* pre = cur->_pre;
pre->_next = newnode;
newnode->_pre = pre;
newnode->_next = cur;
cur->_pre = newnode;
return newnode;
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* pre = cur->_pre;
Node* next = cur->_next;
pre->_next=next;
next->_pre = pre;
delete cur;
return next;
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
iterator begin()
{
return _head->_next;
}
iterator end()
{
return _head;
}
const_iterator begin() const
{
return _head->_next;
}
const_iterator end() const
{
return _head;
}
private:
Node* _head;
};
上面就是list的常见接口的模拟实现,有些并不常见的我没有在此写出原来,如果以后见到的时候大家查一下List的文档就可以了,使用方法都很简单。
4、list的优缺点
带头结点的双向循环链表 ,list这个容器常用于适合大量插入删除数据的场景,由于它是一个个结点链接,所以它移动节点会很方便,并不需要挪动数据,头插头删,或者任意位置插入删除都很高效。但是它的缺点也很明显:不支持随机访问,访问某个元素效率O(N),底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低。大家使用的时候注意能否适合使用List。
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/0deeef8607.html
