博主首页:
有趣的中国人
专栏首页:
C++专栏
1. list简介
2. list模拟实现
2.1 list类的相关成员变量
template<class T>
struct ListNode
{
ListNode<T>* _prev;
ListNode<T>* _next;
T _val;
ListNode(const T& x = T())
:_prev(nullptr)
,_next(nullptr)
,_val(x)
{}
};
template<class T>
class list
{
public:
typedef ListNode<T> Node;
list()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
}
private:
Node* _head;
};
2.2 尾插
尾插太简单了,直接上代码:
void push_back(const T& x)
{
Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
}
2.3 迭代器
下面的代码是迭代器正常的使用方法,我们需要用运算符重载来实现这些功能:
void list_test1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
2.3.1 迭代器类的成员变量
struct ListNodeIterator
{
typedef ListNode<T> Node;
Node* _node;
ListNodeIterator(Node* node)
:_node(node)
{}
};
2.3.2 迭代器类的实现
template<class T>
struct ListNodeIterator
{
typedef ListNode<T> Node;
typedef ListNodeIterator<T> Self;
Node* _node;
ListNodeIterator(Node* node)
:_node(node)
{}
T& 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;
}
};
2.3.3 insert 和 erase
void insert(iterator pos, const T& x)
{
Node* newnode = new Node(x);
Node* cur = pos._node;
Node* prev = cur->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
}
void erase(iterator pos)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
cur = nullptr;
}
这里需要注意,在erase的时候由于迭代器指向的空间被释放了,会导致迭代器失效的问题,之前的文章讲过相关的内容,因此我们需要更新iterator,指向被删除的位置的下一个位置即可,代码如下:
iterator erase(iterator pos)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
cur = nullptr;
return next;
}
2.3.4 begin 和 end
typedef ListNodeIterator<T> iterator;
iterator begin()
{
return iterator(_head->_next);
// 单参数类型的构造函数支持隐式类型转换,以下依法也可以:
// return _head->_next;
}
iterator end()
{
return iterator(_head);
// return _head;
}
2.3.5 insert 和 erase的复用
void push_back(const T& x)
{
/*Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;*/
insert(end(), x);
}
void pop_back()
{
erase(--end());
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_front()
{
erase(begin());
}
测试代码:
void list_test1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
lt.pop_back();
lt.pop_back();
it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
lt.push_front(100);
lt.push_front(200);
lt.push_front(300);
it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
lt.pop_front();
lt.pop_front();
it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
2.3.5 operator->的重载
- 先看一下这段代码:
void list_test2()
{
struct A
{
int _a1;
int _a2;
A(int a1 = 0, int a2 = 0)
:_a1(a1)
,_a2(a2)
{}
};
list<A> lt;
A a(1, 2);
lt.push_back(a);
lt.push_back(A(3, 4));
lt.push_back({ 5,6 });
list<A>::iterator it = lt.begin();
while (it != lt.end())
{
// 主要看这里
cout << (*it)._a1 << " " << (*it)._a2 << " ";
cout << endl;
++it;
}
}
A* aa;
(*aa)._a1;
// 上面的方法很别扭,我们正常用指针都是用->访问的,所以我们如何实现->的重载呢?
aa->_a1;
- 实现方法如下:
T* operator->()
{
return &_node->_val;
}
`
void list_test2()
{
struct A
{
int _a1;
int _a2;
A(int a1 = 0, int a2 = 0)
:_a1(a1)
,_a2(a2)
{}
};
/*A* aa;
(*aa)._a1;
aa->_a1;*/
list<A> lt;
A a(1, 2);
lt.push_back(a);
lt.push_back(A(3, 4));
lt.push_back({ 5,6 });
list<A>::iterator it = lt.begin();
while (it != lt.end())
{
cout << (*it)._a1 << " " << (*it)._a2 << " ";
cout << it.operator->()->_a1 << " " << it.operator->()->_a2 << " ";
//cout << it->->_a1 << " " << it->->_a2 << " ";
cout << it->_a1 << " " << it->_a2 << " ";
cout << endl;
++it;
}
}
2.4 const迭代器
- 我们先看以下这段代码:
void Print(const list<int>& lt)
{
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void list_test3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(1);
lt.push_back(8);
lt.push_back(6);
lt.push_back(0);
Print(lt);
}
- 很明显,会报错,提示不能从
const
转换为非const
:
- 很多人可能会想着在
begin
和end
后加上const
进行修饰,但其实也不行,这样虽然传入的值是const
类型,但是返回的值不是const
类型,就会导致返回的值能被修改,但是要求是返回的值是const
类型,所以这种想法是不行的,下面是错误的示范:
iterator begin() const
{
return iterator(_head->_next);
// 单参数类型的构造函数支持隐式类型转换,以下写法也可以:
// return _head->_next;
}
iterator end() const
{
return iterator(_head);
// return _head;
}
void Print(const list<int>& lt)
{
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
// 这里可以修改
*it += 10;
cout << *it << " ";
++it;
}
cout << endl;
}
void list_test3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(1);
lt.push_back(8);
lt.push_back(6);
lt.push_back(0);
Print(lt);
}
- 那应该如何解决呢?在此之前,我们需要了解一下这段代码:
const iterator begin()
{
return iterator(_head->_next);
// 单参数类型的构造函数支持隐式类型转换,以下写法也可以:
// return _head->_next;
}
const iterator end()
{
return iterator(_head);
// return _head;
}
- 当我们在返回值前加上
const
,代表返回的迭代器不能被修改,例如不能进行++it
; - 但是我们是想着迭代器
指向的内容
不能被修改,因此这种方法是不可行的。 - 可以类比一下这段代码:
// const修饰的是解引用之后的内容
const int* a;
// const修饰的是指针本身
int* const a;
- 解决方法其实很简单,之前说过既然不满足要求,那我们就自己造轮子,自己写一个类;
- 这个类其实也很简单,就把
ListNodeIterator
这个类中的两个运算符重载函数的返回值改变一下就可以了,一个是*
,另一个是->
:
template<class T>
struct ListNodeConstIterator
{
typedef ListNode<T> Node;
typedef ListNodeConstIterator<T> Self;
Node* _node;
ListNodeConstIterator(Node* node)
:_node(node)
{}
const T& operator*()
{
return _node->_val;
}
const T* 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;
}
};
在list
类中加入这两个函数:
const_iterator begin() const
{
return const_iterator(_head->_next);
// 单参数类型的构造函数支持隐式类型转换,以下写法也可以:
// return _head->_next;
}
const_iterator end() const
{
return const_iterator(_head);
// return _head;
}
- 这时候就不能修改了
2.5 模板的作用
template<class T, class Ref, class Ptr>
struct ListNodeIterator
{
typedef ListNode<T> Node;
typedef ListNodeIterator<T, Ref, Ptr> Self;
Node* _node;
ListNodeIterator(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)
{
return _node != it._node;
}
};
template<class T>
class list
{
public:
typedef ListNode<T> Node;
typedef ListNodeIterator<T, T&, T*> iterator;
typedef ListNodeIterator<T,const T&, const T*> const_iterator;
iterator begin()
{
return iterator(_head->_next);
// 单参数类型的构造函数支持隐式类型转换,以下写法也可以:
// return _head->_next;
}
iterator end()
{
return iterator(_head);
// return _head;
}
const_iterator begin() const
{
return const_iterator(_head->_next);
// 单参数类型的构造函数支持隐式类型转换,以下写法也可以:
// return _head->_next;
}
const_iterator end() const
{
return const_iterator(_head);
// return _head;
}
// ......
};
3. 拷贝构造、赋值重载、析构
3.1 析构函数
- 析构函数释放掉空间即可,记住更新一下迭代器。
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
3.2 拷贝构造
- 拷贝构造新建一个头节点,然后尾插。
void empty_init()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
}
list(const list<T>& lt)
{
empty_init();
for (auto& e : lt)
{
push_back(e);
}
}
3.3 赋值重载
- 赋值重载现代写法,之前讲过类似的方法:
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
}
list<int>& operator=(list<T> lt)
{
swap(lt);
return *this;
}