1.list 接口使用
1.1构造

1.2iterator的使用
可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。

1.3 capacity

1.4 element access

1.5 modifiers

2.list的迭代器失效
迭代器失效即迭代器所指向的节点的无 效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入 时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭 代器,其他迭代器不会受到影响。
3.list模拟实现
cpp
#pragma once
namespace my_list
{
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{
}
};
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;
}
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> //使用三个模板参数就就可以不再另开一个函数
//struct _list_const_iterator
//{
// typedef list_node<T> Node;
// Node* _node;
//
// _list_const_iterator(Node* node)
// :_node(node)
// {
// }
//
// const T& operator*()
// {
// return _node->_data;
// }
//
// _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;
// }
// _list_const_iterator<T>& operator--()
// {
// _node = _node->_prev;
// return *this;
// }
// _list_const_iterator<T> operator--(int)
// {
// _list_const_iterator<T> tmp(*this);
// _node = _node->_prev;
// return tmp;
// }
//
// bool operator!=(const _list_const_iterator<T>& it) const
// {
// return _node != it._node;
// }
// bool operator==(const _list_const_iterator<T>& it) const
// {
// return _node == it._node;
// }
//};
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef _list_iterator<T, T&, T*> iterator;
typedef _list_iterator<T, const T&, const T*> const_iterator;
// 不能实现,因为const迭代器本身,迭代器无法++
// typedef const __list_iterator<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);
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
list()
{
empty_init();
}
list(const list<T>& lt)
{
empty_init();
for (const auto& e : lt)
{
push_back(e);
}
}
list(std::initializer_list<T> il)
{
empty_init();
for (const auto& e : il)
{
push_back(e);
}
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
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);
}
}
void push_back(const T& x)
{
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator insert(iterator pos, const T& val)
{
Node* cur = pos._node;
Node* newnode = new Node(val);
Node* prev = cur->_prev;
prev->_next = newnode;
newnode->_next = cur;
newnode->_prev = prev;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
iterator erase(iterator pos)
{
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() const
{
return _size;
}
private:
Node* _head;
size_t _size = 0;
};
}
打印list:
cpp
template<class T>
void print(const list<T>& lt)
{
// 类模板未实例化,不能去类模板中找后面的东西
// 编译器就分不清const_iterator是嵌套内类,还是静态成员变量
// typename告诉编译器,我确认过了这里是类型
//typename list<T>::const_iterator it = lt.begin();
auto it = lt.begin();//使用auto就可以避免上面问题
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
4.list与vector的对比
