目录
list的介绍及使用
list的介绍
list的文档及介绍
list的使用
list的构造
构造函数( (constructor)) | 接口说明 |
---|---|
list (size_type n, const value_type& val =value_type()) | 构造的list中包含n个值为val的元素 |
list() | 构造空的list |
list (const list& x) | 拷贝构造函数 |
list (InputIterator first, InputIterator last) | 用[first, last)区间中的元素构造list |
list<int> lt1;// 构造空的l1
list<int> lt2(5,10);// l2中放5个值为10的元素
list<int> lt3(lt2.begin(), lt2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
list<int> lt4(lt2); // 用l2拷贝构造l4
list iterator的使用
函数声明 | 接口说明 |
---|---|
begin +end | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
rbegin+ rend | 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置 |
int arr[5] = { 1,2,3,4,5 };
list<int> lt1(arr, arr + 5);//以数组为迭代器区间构造lt1
list<int>::iterator it = lt1.begin();//正向迭代器
while (it != lt1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
list<int>::reverse_iterator rit = lt1.rbegin();//反向迭代器
while (rit != lt1.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
list capacity
函数声明 | 接口说明 |
---|---|
empty | 检测list是否为空,是返回true,否则返回false |
size | 返回list中有效节点的个数 |
list element access
函数声明 | 接口说明 |
---|---|
front | 返回list的第一个节点中值的引用 |
back | 返回list的最后一个节点中值的引用 |
list modifiers
函数声明 | 接口说明 |
---|---|
push_front | 在list首元素前插入值为val的元素 |
pop_front | 删除list中第一个元素 |
push_back | 在list尾部插入值为val的元素 |
pop_back | 删除list中最后一个元素 |
insert | 在list position 位置中插入值为val的元素 |
erase | 删除list position位置的元素 |
swap | 交换两个list中的元素 |
clear | 清空list中的有效元素 |
mylist.push_back(1);//在链表尾部插入一个1
mylist.push_front(0);//在链表头部插入一个0
mylist.pop_back();//在链表的尾部删除数据
mylist.pop_front();//在链表的头部删除数据
list<int> mylist(5,10);
list<int>::iterator it = mylist.begin();
mylist.insert(it,20);
mylist.insert(it,5,30);
list<int> lt(5, 100);
mylist.insert(it,lt.begin(),lt.end());
list的迭代器失效
int arr[] = { 1,2,3,4,5 };
list<int> lt(arr, arr + 5);
auto it = lt.begin();
while (it != lt.end())
{
lt.erase(it);
it++;// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给
//其赋值
}
改正
int arr[] = { 1,2,3,4,5 };
list<int> lt(arr, arr + 5);
auto it = lt.begin();
while (it != lt.end())
{
lt.erase(it++); // it = lt.erase(it);
}
list的模拟实现
#include<assert.h>
namespace zx
{
//定义节点
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)
{}
};
//定义迭代器
//因为list的底层不是连续的物理空间,无法使用原生指针作为迭代器,需要用类去封装一下,来模拟指针的行为
template<class T,class Ptr,class Ref>
//定义3个模板参数
struct list_iterator//这里也可以是class,因为后续会多次使用类里面的变量,因此struct会更好一些
{
typedef list_node<T> Node;
typedef list_iterator<T,Ptr,Ref> 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& node)const
{
return _node!= node._node;
}
bool operator==(const Self& node)const
{
return _node == node._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;
iterator begin()
{
return _head->_next;
}
iterator end()
{
return _head;
}
const_iterator begin()const
{
return _head->_next;
}
const_iterator end()const
{
return _head;
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
_size = 0;
}
list()
{
empty_init();
}
list(const list<T>& lt)
{
empty_init();
for (auto& e : lt)
{
push_back(e);
}
}
list(initializer_list<T>& v)
{
empty_init();
for (auto& e : v)
{
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;
}
void clear()
{
auto it = begin();
while (it != end())
{
it = erase(it);
}
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void push_back(const T& x)
{
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator insert(iterator pos, const T& x)
{
Node* newnode = new Node(x);
Node* prev = pos._node->_prev;
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = pos._node;
pos._node->_prev = newnode;
++_size;
return newnode;
}
iterator erase(iterator pos)
//这里需要有返回值来接受,不正确的使用erase,会使迭代器失效,出现野指针
{
assert(pos != end());
//prev pos next
Node* prev = pos._node->_prev;
Node* next = pos._node->_next;
prev->_next = next;
next->_prev = prev;
delete pos._node;
--_size;
return next;
}
void pop_back()
{
erase(--end());//复用之前的函数
}
void pop_front()
{
erase(begin());
}
size_t size()const
{
return _size;
}
bool empty()const
{
return _size == 0;
}
private:
Node* _head;
size_t _size;//用于记录数据个数,后续需要的时候,就不用遍历链表了
};
}
list与vector的对比
vector:
list: