总述

带头双向循环链表
Alloc 是空间配置器,内存池。以后会讲空间配置器跟这里是如何配合的
没有 resize、reserve:不涉及扩容,不用提前开空间
遍历不能用下标+[ ],因为不是连续空间了,只能用迭代器。且 list 的迭代器没有重载 +,因为代价太大了
insert 以后,迭代器不失效;erase 以后,节点都没了,迭代器失效
#include <list>
#include <iostream>
using namespace std;
void test_list1()
{
list<int> lt;
lt.push_back(1); lt.push_back(2);
lt.push_back(3); lt.push_back(4);
lt.push_front(10); lt.push_front(20);
/*list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;*/
for (auto e : lt)
{
cout << e << " ";
}
cout << endl; // 20 10 1 2 3 4
// 第5个位置插入数据
//v.insert(v.begin()+5, 100); 错!空间不连续
auto it = lt.begin();
for (size_t i = 0; i < 5; i++)
{
++it;
}
lt.insert(it, 100);
for (auto e : lt) { cout << e << " "; }
cout << endl; // 20 10 1 2 3 100 4
////////////////////////////////////////////
it = find(lt.begin(), lt.end(), 3);
if (it != lt.end()) // find没找到返回后一个迭代器
{
lt.insert(it, 30);
// insert以后,it不失效
*it *= 100;
}
for (auto e : lt) { cout << e << " "; }
cout << endl; // 20 10 1 2 30 300 100 4
it = find(lt.begin(), lt.end(), 2);
if (it != lt.end())
{
lt.erase(it);
//erase以后,it失效了
// *it *= 100; 放开就报错
}
for (auto e : lt) { cout << e << " "; }
cout << endl; // 20 10 1 30 300 100 4
// 删除list中的所有偶数
it = lt.begin();
while (it != lt.end())
{
if (*it % 2 == 0)
{
it = lt.erase(it);
}
else
{
++it;
}
}
for (auto e : lt) { cout << e << " "; }
cout << endl; // 1
}
int main()
{
test_list1();
return 0;
}
相关操作

merge:2个链表归并。要先有序
unique:去重。要先有序
remove:find + erase。
splice:转移。把一个链表节点取下来,插入到另一个。

void test_list2()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_front(10);
lt.push_front(20);
for (auto e : lt) { cout << e << " "; }
cout << endl;
reverse(lt.begin(), lt.end()); // 算法库
for (auto e : lt) { cout << e << " "; }
cout << endl;
lt.reverse(); // list自己的
for (auto e : lt) { cout << e << " "; }
cout << endl;
//sort(lt.begin(), lt.end()); // 算法库
//报错。sort是快排,要三数取中选位置,list无法适应这个场景
for (auto e : lt) { cout << e << " "; }
cout << endl;
lt.sort(); // list自己的
for (auto e : lt) { cout << e << " "; }
cout << endl;
}
迭代器
算法要作用到数据上面去,数据在容器里一般被封装为私有,不能直接访问
迭代器提供统一方式,让算法去处理容器中的数据,还不用知道容器底层是怎么实现的
把公共的算法提取出来
迭代器从功能、性质角度会分类,由容器底层结构决定
单向:++ forward_list/unordered_xxx
双向:++/-- list/map/set
随机:++/--/+/- vector/string/deque
上面容器能用的算法,下面容器也一定能用

都是函数模板,但名字很有讲究,名字暗示要传什么迭代器、适合什么容器

容器能不能用某个算法,要看容器的迭代器是哪一种,能不能适应算法
算法库里的 sort 要随机迭代器,所以 list 调就报错
所以 list 自己的 sort 就有意义了。但唯一意义是方便,数据量小可以用
数据量大不能用 list 排序,要用 vector 排序;vector 的效率 >> list 的效率
排序,要先把数据放在合适的容器再排序,而不是让 sort 算法兼容各个容器
如果兼容了反而误导了用户,用户在不知情的情况下会认为 vector、list 的 sort 没太大差别,导致效率下降
比如 vector 不直接提供头插头删,就是为了防止用户想大量头插头删时误用vector,导致效率下降
偶尔头插头删用 vector 没问题,大量头插头删就应该用 list
Release下:vector 的排序用算法的 sort(快排);list 的排序用 list 的 sort(归并)
void test_op()
{
srand(time(0));
const int N = 100000;
vector<int> v;
v.reserve(N);
list<int> lt;
for (int i = 0; i < N; ++i)
{
auto e = rand();
v.push_back(e);
lt.push_back(e);
}
int begin1 = clock();
sort(v.begin(), v.end());
int end1 = clock();
int begin2 = clock();
lt.sort();
int end2 = clock();
printf("vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}

拷贝到 vector 排序,排完以后再拷贝回来:↓
void test_op()
{
srand(time(0));
const int N = 5000000;
list<int> lt1;
list<int> lt2;
for (int i = 0; i < N; ++i)
{
auto e = rand();
lt1.push_back(e);
lt2.push_back(e);
}
int begin1 = clock();
vector<int> v;
v.reserve(N);
for (auto e : lt1) // 先拷贝到vector
{
v.push_back(e);
}
sort(v.begin(), v.end()); // 排序
size_t i = 0;
for (auto& e : lt1) // 拷贝回去
{
e = v[i++];
}
int end1 = clock();
int begin2 = clock();
lt2.sort();
int end2 = clock();
printf("vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
模拟实现
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef Node* iterator; // 错

数组天生可以做迭代器,因为迭代器就是按数组指针的方式设计的。it 解引用拿到数据;++到下一个位置
不能让 Node* 充当 iterator,it 解引用得到的是节点,但想要的是数据;且++到不了下一个节点位置
库里用 __list_iterator类 封装节点的指针。相当于用一个节点的指针构造了一个迭代器
库里的迭代器是如何构造的?
迭代器是自定义类型,这个自定义类型的成员是一个节点的指针
其实这里本身想要的就是节点的指针,节点的指针就可以做迭代器。但由于底层的差异,像原生指针那样使用会报错,内置类型不符合我们的行为。
通过节点的指针可以找到下一个位置,达到 ++it 的效果;
通过节点的指针拿到 _val,达到 *it 的效果;
C++可以用一个类去封装这个内置类型,再重载运算符,改变原生指针 it 的行为:
*it 调用 operator* 函数;++it 调用 operator++ 函数
最终达成的效果:不论底层多么复杂,都可以和 vector 这种原生指针同一种用法
(上面说 vector 迭代器是原生指针只是为了理解 list 迭代器,任何容器的迭代器底层不一定是原生指针,不同平台会再封装。但所有容器最底层都是指针)
I 版代码
namespace qtw
{
template<class T>
struct list_node
{
list_node<T>* _next;
list_node<T>* _prev;
T _val;
list_node(const T& val = T())
:_next(nullptr)
, _prev(nullptr)
, _val(val)
{}
};
template<class T>
struct __list_iterator
{
typedef list_node<T> Node;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
T& operator*()
{
return _node->_val;
} // 出了作用域,节点还在,引用返回可读可写
__list_iterator<T>& operator++()
{
_node = _node->_next;
return *this;
} // 自定义类型优先调用前置++
__list_iterator<T> operator++(int)
{
__list_iterator<T> tmp(*this);
_node = _node->_next;
return tmp;
}
bool operator!=(const __list_iterator<T>& it)
{
return _node != it._node;
}
bool operator==(const __list_iterator<T>& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef list_node<T> Node;
// 这个Node不是给用户用的,是给类内部用的
public:
typedef __list_iterator<T> iterator;
// 迭代器是对外用的
iterator begin()
{
//return _head->_next;
return iterator(_head->_next);
}
iterator end()
{
return _head;
//return iterator(_head);
}
list()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
}
~list()
{
//...
}
void push_back(const T& x)
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
}
private:
Node* _head;
};
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
(*it) += 1;
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
}
问题1
iterator begin()
{
//return _head->_next;
return iterator(_head->_next);
}
iterator end()
{
return _head;
//return iterator(_head);
}
begin 为什么明明返回的是迭代器,却能返回节点的指针?
因为单参数的构造函数支持隐式类型转换。2种写法都是生成匿名对象
问题2
bool operator!=(const __list_iterator<T>& it)
{
return _node != it._node;
}
///////////////////
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
operator!= 不加 const 会报错,为什么加 const ?
调了 end(),是传值返回,返回的是 _head 的拷贝,是临时对象,常性
问题3
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
第一行代码:调用 begin,返回值拷贝构造给 it
我们没写拷贝构造,默认生成的拷贝构造对内置类型完成浅拷贝。
没问题,要的就是浅拷贝
begin 返回指向1这个节点的迭代器,我们就希望 it 里面节点的指针也是指向1这个节点的迭代器
2个对象指向同一节点,没有析构2次,没有崩溃。
迭代器没写析构函数,因为节点不属于迭代器,节点应该由 list 释放
把节点给迭代器,不是让迭代器管理节点的释放……,只是借助这个节点访问容器
问题4:如何实现const迭代器?
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef __list_iterator<T> iterator;
// 这样设计const迭代器是不行的,因为const迭代器期望指向内容不能修改
// 迭代器都要能++,要不然怎么遍历呢? 这样设计是迭代器本身不能修改
typedef const __list_iterator<T> const_iterator; // 错
// const T* ptr1;
// T* const ptr2;
private:
Node* _head;
};
迭代器 与 const迭代器 唯一区别解引用操作 *it:↓
const T& operator*() // 返回 const T 的别名
{
return _node->_val;
}
T& operator*() // 返回 T 的别名
{
return _node->_val;
}
可以这样实现:↓
template<class T>
struct __list_iterator
{
typedef list_node<T> Node;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
T& operator*() // 除了这,其他地方和 const迭代器一样
{
return _node->_val;
}
__list_iterator<T>& operator++()
{
_node = _node->_next;
return *this;
}
__list_iterator<T> operator++(int)
{
__list_iterator<T> tmp(*this);
_node = _node->_next;
return tmp;
}
bool operator!=(const __list_iterator<T>& it)
{
return _node != it._node;
}
bool operator==(const __list_iterator<T>& it)
{
return _node == it._node;
}
};
template<class T>
struct __list_const_iterator
{
typedef list_node<T> Node;
Node* _node;
__list_const_iterator(Node* node)
:_node(node)
{}
const T& operator*() // 除了这,其他地方和普通迭代器一样
{
return _node->_val;
}
__list_const_iterator<T>& operator++()
{
_node = _node->_next;
return *this;
}
__list_const_iterator<T> operator++(int)
{
__list_const_iterator<T> tmp(*this);
_node = _node->_next;
return tmp;
}
bool operator!=(const __list_const_iterator<T>& it)
{
return _node != it._node;
}
bool operator==(const __list_const_iterator<T>& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef __list_iterator<T> iterator;
typedef __list_const_iterator<T> const_iterator;
iterator begin()
{
//return _head->_next;
return iterator(_head->_next);
}
iterator end()
{
return _head;
//return iterator(_head);
}
const_iterator begin() const
{
//return _head->_next;
return const_iterator(_head->_next);
}
const_iterator end() const
{
return _head;
//return const_iterator(_head);
}
private:
Node* _head;
};
这么设计太冗余了,2个类只有 const T& operator*() 这里返回值不一样,我们通过一个类型控制这个返回值就行 ==> 增加一个模板参数
库里只设计了一个类,控制了3个模板参数去搞:↓
template<class T, class Ref, class Ptr>
struct __list_iterator {
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
typedef __list_iterator<T, Ref, Ptr> self;
};
template <class T, class Alloc = alloc>
class list {
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
};
II 版代码
我们本质上还是控制了2个类型。一个模板给不同的参数就是不同的类型
//typedef __list_iterator<T, T&> iterator;
//typedef __list_iterator<T, const T&> const_iterator;
template<class T, class Ref>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T, Ref> self;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_val;
}
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& it)
{
return _node != it._node;
}
bool operator==(const self& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef __list_iterator<T, T&> iterator;
typedef __list_iterator<T, const T&> const_iterator;
iterator begin()
{
//return _head->_next;
return iterator(_head->_next);
}
iterator end()
{
return _head;
//return iterator(_head);
}
const_iterator begin() const
{
//return _head->_next;
return const_iterator(_head->_next);
}
const_iterator end() const
{
return _head;
//return const_iterator(_head);
}
private:
Node* _head;
};
库里重载了3个模板参数,还重载了一个运算符 ->
// 库里
reference operator*() const { return (*node).data; }
pointer operator->() const { return &(operator*()); }
-> 的返回值是模板参数 Ptr,
运算符重载是可以显示调用的,所以可以调 operator*(),返回结果是 data 这个数据取地址
Ref operator*()
{
return _node->_val;
}
T* operator->()
{
return &_node->_val;
}
指针除了 *解引用,取指针指向的对象
还有 -> 解引用,如果是结构体的指针就要用 ->
迭代器又是模拟指针的行为,什么时候模拟结构体指针?链表数据是自定义类型:↓
struct A
{
A(int a1 = 0, int a2 = 0)
:_a1(a1)
, _a2(a2)
{}
int _a1;
int _a2;
};
void test_list2()
{
list<A> lt;
lt.push_back(A(1, 1));
lt.push_back(A(2, 2));
lt.push_back(A(3, 3));
lt.push_back(A(4, 4));
list<A>::iterator it = lt.begin();
while (it != lt.end()) // 访问自定义类型数据
{
cout << *it << " "; // 流插入报错
++it;
}
cout << endl;
}
*it 解引用出来的数据是 T,这里是 A,是自定义类型。要求用户自己重载流插入,才能支持
如果 A 是私有的,只能去重载流插入。但现在是公有的,可以直接访问
cout << (*it)._a1 << " " << (*it)._a2 << endl;
![]()
此时,迭代器模拟的是结构体指针,可不可以这样写?
cout << it->_a1 << " " << it->_a2 << endl;
it 是自定义类型的指针,且上面重载了 ->,所以这样写没问题
但把 T* operator->() 屏蔽调代码会报错:“qtw::__list_iterator<T,T &>”类型没有重载成员“operator ->”

严格来说 it->->_a2 才符合语法
由于运算符重载要可读性,编译器省略了一个 ->,编译器自己知道有
const 迭代器这里要返回 const T *
const T* operator->()
{
return &_node->_val;
}
所以要加第三个模板参数
III 版代码
namespace qtw
{
template<class T>
struct list_node
{
list_node<T>* _next;
list_node<T>* _prev;
T _val;
list_node(const T& val = T())
:_next(nullptr)
, _prev(nullptr)
, _val(val)
{}
};
//typedef __list_iterator<T, T&, T*> iterator;
//typedef __list_iterator<T, const T&, const T*> const_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)
{}
Ref operator*()
{
return _node->_val;
}
Ptr operator->()
{
return &_node->_val;
}
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& it) const
{
return _node != it._node;
}
bool operator==(const self& it) const
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef list_node<T> Node; // 这个Node不是给用户用的,是给类内部用的
public:
//typedef __list_iterator<T> iterator; // 迭代器是对外用的
//typedef __list_const_iterator<T> const_iterator; // 这么设计太冗余了
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
iterator begin()
{
//return _head->_next;
return iterator(_head->_next);
}
iterator end()
{
return _head;
//return iterator(_head);
}
const_iterator begin() const
{
//return _head->_next;
return const_iterator(_head->_next);
}
const_iterator end() const
{
return _head;
//return const_iterator(_head);
}
list()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
}
~list()
{
//...
}
void push_back(const T& x)
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
}
private:
Node* _head;
};
}
模拟实现最终代码

类模板在类里,类名可以代替类型。但我们不要这么写
namespace qtw
{
template<class T>
struct list_node
{
list_node<T>* _next;
list_node<T>* _prev;
T _val;
list_node(const T& val = T())
:_next(nullptr)
, _prev(nullptr)
, _val(val)
{}
};
//typedef __list_iterator<T, T&, T*> iterator;
//typedef __list_iterator<T, const T&, const T*> const_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)
{}
Ref operator*()
{
return _node->_val;
}
Ptr operator->()
{
return &_node->_val;
}
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& it) const
{
return _node != it._node;
}
bool operator==(const self& it) const
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef list_node<T> Node; // 这个Node不是给用户用的,是给类内部用的
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
iterator begin()
{
//return _head->_next;
return iterator(_head->_next);
}
iterator end()
{
return _head;
//return iterator(_head);
}
const_iterator begin() const
{
//return _head->_next;
return const_iterator(_head->_next);
}
const_iterator end() const
{
return _head;
//return const_iterator(_head);
}
void empty_init()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
_size = 0;
}
list()
{
empty_init();
}
//list(const list& lt)
list(const list<T>& lt)
{
empty_init();
// 自定义类型比较大的时候,范围for一定加引用
for (auto& e : lt)
{
push_back(e);
}
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
//list& operator=(list 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 = 0;
}
void push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(--end());
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
// pos位置之前插入
iterator insert(iterator pos, const T& x)
{
Node* newnode = new Node(x);
Node* cur = pos._node;
Node* prev = cur->_prev;
prev->_next = newnode;
newnode->_next = cur;
newnode->_prev = prev;
cur->_prev = newnode;
++_size;
return newnode;
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
--_size;
return next;
}
size_t size()
{
/*size_t sz;
iterator it = begin();
while (it != end())
{
++sz;
++it;
}
return sz;*/
return _size;
}
private:
Node* _head;
size_t _size;
};
}
本篇的分享就到这里了,感谢观看,如果对你有帮助,别忘了点赞+收藏+关注。
小编会以自己学习过程中遇到的问题为素材,持续为您推送文章

8303

被折叠的 条评论
为什么被折叠?



