1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问
list的使用
构造函数
构造函数 | 接口说明 |
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 |
迭代器
函数声明 | 接口说明 |
begin+end | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
rbegin+rend | 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置 |
list容量
函数声明 | 接口说明 |
empty | 检测list是否为空,是返回true,否则返回false |
size | 返回list中有效节点的个数 |
list元素访问
函数声明 | 接口说明 |
front | 返回list的第一个节点中值的引用 |
back | 返回list的最后一个节点中值的引用 |
list修改
函数声明 | 接口说明 |
push_front | 在list首元素前插入值为val的元素 |
pop_front | 删除list中第一个元素 |
push_back | 在list尾部插入值为val的元素 |
pop_back | 删除list中最后一个元素 |
insert | 在list position 位置中插入值为val的元素 |
erase | 删除list position位置的元素 |
clear | 清空list中的有效元素 |
list的迭代器失效
迭代器失效即迭代器所指向的节点的无效。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。下一次使用迭代器之前必须先给迭代器赋值。
list的模拟实现
#pragma once
#include<iostream>
using namespace std;
namespace hzp {
//节点类
template<class T>
struct ListNode {
ListNode<T>* _pre;
ListNode<T>* _next;
T _data;
ListNode(const T& data = T())
:_pre(nullptr)
,_next(nullptr)
,_data(data)
{}
};
//list迭代器类
template<class T, class Ref, class Ptr>
struct list_Iterator {
typedef ListNode<T> Node;
typedef list_Iterator<T, Ref, Ptr> self;
Node* _node;
list_Iterator(Node* x)
:_node(x)
{}
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->_pre;
return *this;
}
//后置--
self operator--(int) {
self tmp(*this);
_node = _node->_pre;
return tmp;
}
bool operator!=(const self& lt) const {
return _node != lt._node;
}
bool operator=(const self& lt) const {
return _node == lt._node;
}
};
//list类
template<class T>
class list {
typedef ListNode<T> Node;
public:
typedef list_Iterator<T, T&, T*> iterator;
typedef list_Iterator<T, const T&, const T*> const_iterator;
iterator begin() {
return iterator(phead->_next);
}
iterator end() {
return iterator(phead);
}
const_iterator begin() const {
return const_iterator(phead->_next);
}
const_iterator end() const {
return const_iterator(phead);
}
list() {
phead = new Node;
phead->_pre = phead;
phead->_next = phead;
}
list(int n, const T& val = T()) {
phead = new Node;
phead->_next = phead;
phead->_pre = phead;
for (int i = 0; i < n; i++) {
push_back(val);
}
}
template<class InputIterator>
list(InputIterator first, InputIterator last) {
phead = new Node;
phead->_next = phead;
phead->_pre = phead;
while (first != last) {
push_back(*first);
first++;
}
}
//拷贝构造
list(const list<T>& lt) {
phead = new Node;
phead->_next = phead;
phead->_pre = phead;
list<T> tmp(lt.begin(), lt.end());
swap(phead, tmp.phead);
}
//赋值重载
list<T>& operator=(const list<T>& lt) {
swap(phead, lt.phead);
return *this;
}
size_t size() const {
const_iterator it = begin();
size_t count = 0;
while (it != end()) {
it++;
count++;
}
return count;
}
bool empty() const {
return !(phead->_next);
}
T& front() {
return phead->_next->_data;
}
const T& front() const {
return phead->_next->_data;
}
T& back() {
return phead->_pre->_data;
}
const T& back() const {
return phead->_pre->_data;
}
void push_back(const T& x) {
Node* newNode = new Node(x);
Node* tail = phead->_pre;
phead->_pre = newNode;
newNode->_next = phead;
tail->_next = newNode;
newNode->_pre = tail;
}
void pop_back() {
erase(end()--);
}
void push_front(const T& x) {
insert(begin(), x);
}
void pop_front() {
erase(begin());
}
iterator insert(iterator pos, const T& val) {
Node* cur = pos._node;
Node* pre = cur->_pre;
Node* newNode = new Node(val);
pre->_next = newNode;
newNode->_next = cur;
cur->_pre = newNode;
newNode->_pre = pre;
return iterator(newNode);
}
iterator erase(iterator pos) {
Node* pre = pos._node->_pre;
Node* next = pos._node->_next;
delete pos._node;
pre->_next = next;
next->_pre = pre;
return iterator(next);
}
void clear() {
iterator it = begin();
while (it != end()) {
erase(it++);
}
}
~list() {
clear();
delete phead;
phead = nullptr;
}
private:
Node* phead;
};
void test1() {
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
list<int> lt1(5, 3);
/*list<int>::iterator it = lt1.begin();
while (it != lt1.end()) {
cout << *it << " ";
it++;
}
cout << endl;
cout << lt.front() << endl;
cout << lt.back() << endl;*/
cout << lt.size() << endl;
cout << lt.empty() << endl;
}
}