list的介紹
- list是可以在常數範圍內在任意位置進行插入和刪除的序列式容器,並且該容器可以前後雙向迭代。
- list的底層是雙向鏈表結構,雙向鏈表中每個元素存儲在互不相關的獨立節點中,在節點中通過指針指向其前一個元素和後一個元素。
- list與forward_list非常相似:最主要的不同在於forward_list是單鏈表,只能朝前迭代,讓其更簡單高效。
- 與其他的序列式容器相比(vector,deque),list通常在任意位置進行插入、移除元素的執行效率更好。
- 與其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的隨機訪問,比如:要訪問list的第6個元素,必須從已知的位置(比如頭部或者尾部)迭代到該位置,在這段位置上迭代需要線性的時間開銷;list還需要一些額外的空間,以保存每個節點的相關聯信息。
- 使用 list 時必須加上頭文件
#include<list>
和using namespace std;
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 |
list<int> l1; // 構造空的l1
list<int> l2(4, 100); // l2中放4個值爲100的元素
list<int> l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左閉右開的區間構造l3
list<int> l4(l3); // 用l3拷貝構造l4
int arr[] = { 16,2,77,29 }; // 以數組爲迭代器區間構造l5
list<int> l5(arr, arr + sizeof(arr) / sizeof(arr[0]));
list迭代器的使用
函數聲明 | 接口說明 |
---|---|
begin + end | begin返回第一個元素的迭代器 + end返回最後一個元素下一個位置的迭代器 |
rbegin + rend | rbegin返回最後一個元素的reverse_iterator + rend返回第一個元素上一個位置的reverse_iterator |
#include <iostream>
#include <list>
using namespace std;
void PrintList(const list<int>& l)
{
list<int>::const_iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 使用正向迭代器正向打印list中的元素
list<int>::iterator it1 = l.begin();
while (it1 != l.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
// 使用反向迭代器逆向打印list中的元素
list<int>::reverse_iterator it2 = l.rbegin();
while (it2 != l.rend())
{
cout << *it2 << " ";
++it2;
}
cout << endl;
//使用const迭代器正向打印list中的元素
PrintList(l);
return 0;
}
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 | 在 pos 位置中插入值爲val的元素 |
erase | 刪除 pos 位置的元素 |
swap | 交換兩個list中的元素 |
clear | 清空list中的有效元素 |
#include <iostream>
#include <list>
#include <vector>
using namespace std;
void PrintList(list<int>& l)
{
for (auto& e : l)
{
cout << e << " ";
}
cout << endl;
}
//push_back、pop_back、push_front、pop_front
void TestList1()
{
int arr[] = { 1, 2, 3 };
list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 在list的尾部插入4,頭部插入0
l.push_back(4);
l.push_front(0);
PrintList(l);
// 刪除list尾部節點和頭部節點
l.pop_back();
l.pop_front();
PrintList(l);
}
//insert、erase
void TestList2()
{
int arr[] = { 1, 2, 3 };
list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 獲取鏈表中第二個節點
auto pos = ++l.begin();
cout << *pos << endl;
// 在pos前插入值爲4的元素
l.insert(pos, 4);
PrintList(l);
// 在pos前插入5個值爲5的元素
l.insert(pos, 5, 5);
PrintList(l);
// 在pos前插入[v.begin(), v.end)區間中的元素
vector<int> v{ 7, 8, 9 };
l.insert(pos, v.begin(), v.end());
PrintList(l);
// 刪除pos位置上的元素
l.erase(pos);
PrintList(l);
// 刪除list中[begin, end)區間中的元素,即刪除list中的所有元素
l.erase(l.begin(), l.end());
PrintList(l);
}
//resize、swap、clear
void TestList3()
{
int arr[] = { 1, 2, 3 };
list<int> l1(arr, arr + sizeof(arr) / sizeof(arr[0]));
PrintList(l1);
// 交換l1和l2中的元素
list<int> l2;
l1.swap(l2);
PrintList(l1);
PrintList(l2);
// 將l2中的元素清空
l2.clear();
cout << l2.size() << endl;
}
list的迭代器失效
- 和vector不同的是,list插入數據時不會引起迭代器失效,因爲每個節點之間都是相互獨立的且不需要擴容,則迭代器的指向不會被改變。
- erase數據時迭代器會失效,刪除數據後,節點被釋放,迭代器的指向非法,需要重新賦值。
list的模擬實現(造輪子)
#pragma once
#include <iostream>
#include <cassert>
using namespace std;
namespace MakeList
{
template<class T>
struct ListNode
{
ListNode* _prev;
ListNode* _next;
T _data;
ListNode(const T& data = T())
: _prev(nullptr)
, _next(nullptr)
, _data(data)
{}
};
template<class T, class Ref, class Ptr>
struct ListIterator
{
typedef ListIterator<T, Ref, Ptr> Self;
typedef ListNode<T> Node;
Node* _node;
ListIterator(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!=(Self it)
{
return _node != it._node;
}
bool operator==(Self it)
{
return _node == it._node;
}
};
template<class T>
class list
{
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;
private:
typedef ListNode<T> Node;
public:
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()
: _head(nullptr)
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
//拷貝構造
list(const list<T>& l)
: _head(nullptr)
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
for (auto e : l)
{
push_back(e);
}
}
//賦值運算符傳統寫法
//list<T>& operator=(const list<T>& l)
//{
// if (this != &l)
// {
// clear();
// for (auto e : l)
// {
// push_back(e);
// }
// }
// return *this;
//}
//賦值運算符現代寫法
list<T>& operator=(list<T> l)
{
std::swap(_head, l._head);
return *this;
}
void clear()
{
iterator it = begin();
while (it != end())
{
erase(it++);
}
}
void push_back(const T& val)
{
Node* tail = _head->_prev;
Node* new_node = new Node(val);
tail->_next = new_node;
new_node->_prev = tail;
_head->_prev = new_node;
new_node->_next = _head;
}
void pop_back()
{
assert(_head->_prev != nullptr);
Node* tail = _head->_prev;
Node* prev = tail->_prev;
delete tail;
_head->_prev = prev;
prev->_next = _head;
}
void push_front(const T& val)
{
Node* cur = _head->_next;
Node* new_node = new Node(val);
_head->_next = new_node;
new_node->_prev = _head;
new_node->_next = cur;
cur->_prev = new_node;
}
void pop_front()
{
assert(_head->_next != nullptr);
Node* cur = _head->_next;
Node* next = cur->_next;
delete cur;
_head->_next = next;
next->_prev = _head;
}
iterator insert(iterator pos, const T& val)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* new_node = new Node(val);
prev->_next = new_node;
new_node->_prev = prev;
new_node->_next = cur;
cur->_prev = new_node;
return pos;
}
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);
}
private:
Node* _head;
};
}