list的介绍
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向带头链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
总览
#include<iostream>
#include<assert.h>
#include<algorithm>
using namespace std;
namespace l
{
//节点结构体
template<class T>
struct __list_node
{
__list_node<T>* _next;
__list_node<T>* _prev;
T _data;
__list_node(const T& x = T())
:_next(nullptr)
, _prev(nullptr)
, _data(x)
{}
};
//封装迭代器
template<class T,class Ref,class Ptr>
struct __list_iterator
{
//萃取 源码中拷贝
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef ptrdiff_t difference_type;
typedef __list_iterator<T,Ref,Ptr> self;
typedef __list_node<T> Node;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
Ref operator*();
Ptr operator->();
self& operator++();
self operator++(int);
self& operator--();
self operator--(int);
bool operator!=(const self& it) const;
bool operator==(const self& it) const;
};
//模拟list类
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;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
list();
template<class InputIterator>
list(InputIterator first,InputIterator last);
list(const list<T>& lt);
list<T>& operator=(list<T> lt);
~list();
void clear();
void push_back(const T& x);
void push_front(const T& x);
void pop_back();
void pop_front();
iterator insert(iterator pos, const T& x);
iterator erase(iterator pos);
size_t size();
bool empty();
private:
Node* _head;
};
list迭代器
迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
容器迭代器分类:
1.使用功能的角度:(正向、反向)+const
2.容器底层结构的角度:单向、双向、随机
单向:如单链表迭代器、哈希表迭代器,可以进行++
双向:如双链表迭代器、map迭代器,可以++、–
随机迭代器:如string、vector、deque迭代器、map迭代器,可以++、–、+、-
迭代器一般分为:
1.原生指针(天然迭代器)。底层物理结构是连续数组空间 如string/vector
2.原生指针(不是天然迭代器)。用一个类去封装指针,通过重载运算符,行为像指针一样 如list/map/set
//迭代器 用一个类把节点的指针封装起来
//iterator - <T,T&,T*>
//const_iterator - <T,const T&,const T*>
template<class T,class Ref,class Ptr>
struct __list_iterator
{
//萃取 源码中拷贝
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef ptrdiff_t difference_type;
typedef __list_iterator<T,Ref,Ptr> self;
typedef __list_node<T> Node;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
//拷贝构造和赋值重载 析构函数 默认生成就可以
//*it->it.operator*()
//*it=10
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
//++it->it.operator++()
self& operator++()
{
_node = _node->_next;
return *this;
}
//it++ -> it.operator++(0)
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;
}
};
list的使用及模拟
构造、析构函数
构造函数(conatructor) | 接口说明 |
---|---|
list() | 构造空的list |
list(size_type n,const value_type& val=value_type()) | 构造的list中包含n个值为val的元素 |
list(const list& x) | 拷贝构造 |
list(InputIterator first,InputIterator last) | 用[first,last)区间中的元素构造list |
list()
{
//带头双向循环
//_head=new Node(T());
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//lt2(lt1) 传统写法
/*list(const list<T>& lt)
{
_head = new newnode;
_head->_next = _head;
_head->_prev = _head;
for (const auto& e : lt)
{
push_back(e);
}
}*/
template<class InputIterator>
list(InputIterator first,InputIterator last)
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
while (first != last)
{
push_back(*first);
++first;
}
}
//lt2(lt1) 现代写法
list(const list<T>& lt)
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
list<T> tmp = list(lt.begin(), lt.end());
std::swap(_head, tmp._head);
}
lt1=lt2 传统写法
//list<T>& operator=(const list<T>& lt)
//{
// if (this != lt)
// {
// clear();
// for (const auto& e : lt)
// {
// push_back(e);
// }
// }
// return *this;
//}
//lt1=lt2 现代写法
list<T>& operator=(list<T> lt)
{
swap(_head, lt._head);
return *this;
}
析构函数:
~list()
{
clear();
delete _head;
_head = nullptr;
}
list iterator
函数声明 | 接口说明 |
---|---|
begin+end | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
rbegin+rend | 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置 |
【注意】
- begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
- rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
typedef __list_iterator<T,T&,T*> 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);
}
list capacity
函数声明 | 接口说明 |
---|---|
empty | 检测list是否为空,是返回true,否则返回false |
size | 返回list中有效节点的个数 |
size_t size()
{
size_t n = 0;
iterator it = begin();
while (it != end())
{
++it;
++n;
}
return n;
}
bool empty()
{
return begin() == end();
}
list element access
函数声明 | 接口说明 |
---|---|
front | 返回list的第一个节点中值的引用 |
back | 返回list的最后一个节点中值的引用 |
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
list modifiers
函数声明 | 接口说明 |
---|---|
push_front | 在list首元素前插入值为val的元素 |
pop_front | 删除list中第一个元素 |
push_back | 在list尾部插入值为val的元素 |
pop_back | 删除list中最后一个元素 |
insert | 在list position 位置中插入值为val的元素 |
erase | 删除list position位置的元素 |
clear | 清空list中的有效元素 |
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);
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
iterator insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
//iterator ret(newnode);
//return ret;
//匿名对象
return iterator(newnode);
//return newnode; 隐式构造
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
delete cur;
prev->_next = next;
next->_prev = prev;
return iterator(next);
}
测试
void print_list(const list<int>& l)
{
list<int>::const_iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
```cpp
void test_list1()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
print_list(l1);
l::list<int>::iterator pos = std::find(l1.begin(), l1.end(), 2);
if (pos != l1.end())
{
l1.insert(pos, 100);
}
print_list(l1);
}
struct TreeNode
{
struct TreeNode* _left;
struct TreeNode* _right;
int _val;
TreeNode(int val=-1)
:_left(nullptr)
,_right(nullptr)
,_val(val)
{}
};
void test_list2()
{
list<TreeNode> lt;
lt.push_back(TreeNode(1));
lt.push_back(TreeNode(2));
lt.push_back(TreeNode(3));
lt.push_back(TreeNode(4));
list<TreeNode>::iterator it = lt.begin();
while (it != lt.end())
{
printf("val:%d,left:%p,right:%p\n", (*it)._val, (*it)._left, (*it)._right);
printf("val:%d,left:%p,right:%p\n", it->_val, it->_left, it->_right);
++it;
}
cout << endl;
}
注意,it->_val即it.operator->(),返回TreeNode*指针。本来这个调用应该是it->->_val,这样写可读性太差,所以编译器进行了特殊处理,省略了一个->,保持程序可读性
void test_list3()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
print_list(l1);
l1.clear();
print_list(l1);
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
print_list(l1);
}
void test_list4()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
print_list(l1);
cout << endl;
list<int> l2(l1);
print_list(l2);
cout << endl;
list<int> l3(l1.begin(), l1.end());
string s("hello");
print_list(l3);
list<int> l4(s.begin(), s.end());
cout << endl;
print_list(l4);
}