本文介紹了list的常用介面的使用,并對其進行了模擬實作,包括list迭代器的實作,
目錄
一、list的介紹
二、list的常用介面的使用
1. list的構造
2. list iterator的使用
3.list capacity的使用
4.list element access
5.list modifiers
6. list的迭代器失效
三、list與vector的對比
四、list的模擬實作
一、list的介紹
list 容器,又稱雙向鏈表容器,即該容器的底層是以雙向鏈表的形式實作的,這意味著,list 容器中的元素可以分散存盤在記憶體空間里,而不是必須存盤在一整塊連續的記憶體空間中,結構如圖,

list是可以在常數范圍內在任意位置進行插入和洗掉的序列式容器,并且該容器可以前后雙向迭代,
list與forward_list非常相似:最主要的不同在于forward_list是單鏈表,只能朝前迭代,已讓其更簡單高效,
與其他的序列式容器相比(array,vector,deque),list通常在任意位置進行插入、移除元素的執行效率更好,最大的缺陷是不支持任意位置的隨機訪問,必須從已知的位置(比如頭部或者尾部)迭代到該位置,在這段位置上迭代需要線性的時間;list還需要一些額外的空間,以保存每個節點的相關聯資訊,
二、list的常用介面的使用
1. list的構造
| 建構式( (constructor)) | 介面說明 |
|---|---|
| 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 |
#include <iostream>
#include <list>
int main ()
{
std::list<int> a; // 構造空的a
std::list<int> b (4,1); // b中放4個值為100的元素
std::list<int> c (b.begin(), b.end()); // 用b的[begin(), end())左閉右開的區間構造c
std::list<int> d (c); // 用c拷貝構造d
// 以陣列為迭代器區間構造e
int array[] = {16,2,77,29};
std::list<int> e (array, array + sizeof(array) / sizeof(int) );
// 用迭代器方式列印e中的元素
for(std::list<int>::iterator it = e.begin(); it != e.end(); it++)
std::cout << *it << " ";
std::cout<<endl;
// C++11范圍for的方式遍歷
for(auto& e : e)
std::cout<< e << " ";
std::cout<<endl;
return 0;
}
2. list iterator的使用
| 函式宣告 | 介面說明 |
|---|---|
| begin + end | 回傳第一個元素的迭代器+回傳最后一個元素下一個位置的迭代器 |
| rbegin + rend | 回傳第一個元素的reverse_iterator,即end位置,回傳最后一個元素下一個位置的reverse_iterator,即begin位置 |
begin與end為正向迭代器,對迭代器執行++操作,迭代器向后移動,
rbegin(end)與rend(begin)為反向迭代器,對迭代器執行++操作,迭代器向前移動,
#include <iostream>
using namespace std;
#include <list>
void print_list(const list<int>& l)
{
// 注意這里呼叫的是list的 begin() const,回傳list的const_iterator物件
for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
// *it = 10; 編譯不通過
}
cout << endl;
}
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
list<int> l(array, array + sizeof(array) / sizeof(array[0]));
// 使用正向迭代器正向list中的元素
for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
cout << *it << " ";
cout << endl;
// 使用反向迭代器逆向列印list中的元素
for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)
cout << *it << " ";
cout << endl;
return 0;
}
3.list capacity的使用
| 函式宣告 | 介面說明 |
|---|---|
| empty | 檢測list是否為空,是回傳true,否則回傳false |
| size | 回傳list中有效節點的個數 |
4.list element access
| 函式宣告 | 介面說明 |
|---|---|
| front | 回傳list的第一個節點中值的參考 |
| back | 回傳list的最后一個節點中值的參考 |
5.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中的有效元素 |
6. list的迭代器失效
此處可將迭代器暫時理解成類似于指標,迭代器失效即迭代器所指向的節點的無效,即該節點被洗掉了,因為list的底層結構為帶頭結點的雙向回圈鏈表,因此在list中進行插入時是不會導致list的迭代器失效的,只有在洗掉時才會失效,并且失效的只是指向被洗掉節點的迭代器,其他迭代器不會受到影響,
三、list與vector的對比
| vector | list | |
|---|---|---|
| 底 層 結 構 | 動態順序表,一段連續空間 | 帶頭結點的雙向回圈鏈表 |
| 隨 機 訪 問 | 支持隨機訪問,訪問某個元素效率O(1) | 不支持隨機訪問,訪問某個元素效率O(N) |
| 插 入 和 刪 除 | 任意位置插入和洗掉效率低,需要搬移元素,時間復雜度為O(N),插入時有可能需要增容,增容:開辟新空間,拷貝元素,釋放舊空間,導致效率更低 | |
| 空 間 利 用 率 | 底層為連續空間,不容易造成記憶體碎片,空間利用率高,快取利用率高 | 底層節點動態開辟,小節點容易造成記憶體碎片,空間利用率低,快取利用率低 |
| 迭 代 器 | 原生態指標 | 對原生態指標(節點指標)進行封裝 |
| 迭 代 器 失 效 | 在插入元素時,要給所有的迭代器重新賦值,因為插入元素有可能會導致重新擴容,致使原來迭代器失效,洗掉時,當前迭代器需要重新賦值否則會失效 | 插入元素不會導致迭代器失效,洗掉元素時,只會導致當前迭代器失效,其他迭代器不受影響 |
| 使 用 場 景 | 需要高效存盤,支持隨機訪問,不關心插入洗掉效率 | 大量插入和洗掉操作,不關心隨機訪問 |
四、list的模擬實作
list的模擬實作十分有趣,這里需要注意,list本身和list的節點是不同的結構,所以需要分開設計,成員都是只需淺拷貝,所以拷貝構造,析構 ,多載 = 都可以使用默認,
list iterator也需要單獨設計,因為原生指標已經無法滿足迭代器需求,所以需要封裝,讓它像一個指標一樣完成訪問操作,
通過template <class T, class Ref, class Ptr>區別T&,*T,
#include <iostream>
#include <list>
#include <assert.h>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
namespace Zht
{
template <class T>
struct _list_node //list本身和list的節點是不同的結構,所以需要分開設計,這里是list節點的結構
{
T val; //資料
_list_node<T>* _next; //下一個節點指標
_list_node<T>* _prev; //上一個
_list_node(const T& val = T()) //構造節點
:val(val) //傳的參
,_prev(nullptr)
,_next(nullptr)
{
}
};
template <class T, class Ref, class Ptr>
struct _list_iterator
{
typedef _list_node<T> node;
typedef _list_iterator<T, Ref, Ptr> self;
node* _pnode; //迭代器本質上是指標
_list_iterator(node* pnode) //建構式
:_pnode(pnode)
{
}
//這里,成員都是只需淺拷貝,所以拷貝構造,析構 ,多載 = 都可以使用默認
Ref operator*() //多載*,通過Ref靈活的調整const和普通,
{
return _pnode->val;
}
bool operator!=(const self& s) const
{
return _pnode != s._pnode;
}
bool operator==(const self& s) const
{
return _pnode == s._pnode;
}
self& operator++() //++就是指向下一個節點
{
_pnode = _pnode->_next;
return *this;
}
self operator++(int) //C++規定后綴呼叫需要有一個int型引數作為區分前綴與后綴呼叫的區別
{
self tmp (*this);
++*this;
return tmp; //*this++后++
}
self& operator--()
{
_pnode = _pnode->_prev;
return *this;
}
self operator--(int)
{
self tmp (*this);
--*this;
return tmp;
}
Ptr operator->()
{
return &(operator*());
}
};
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;
list() //建構式,構造哨兵位
{
_head = new node; //開一個節點
_head->_next = _head; //初始化節點
_head->_prev = _head;
}
template <class Iterator>
list(Iterator first, Iterator last)
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
while(first != last)
{
push_back(*first);
first++;
}
}
list(const list<T>& It)
{
_head = new node; //構造哨兵節點
_head->_next = _head;
_head->_prev = _head;
for(const auto& e : It) //逐個尾插
{
push_back(e);
}
}
list<T>& operator=(list<T> It)
{
swap(_head, It._head);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
iterator begin()
{
return iterator(_head->_next);
}
const_iterator begin() const //呼叫const的迭代器,回傳一個用_head->next構造的迭代器物件
{
return const_iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
const_iterator end() const
{
return const_iterator(_head);
}
void push_back(const T& x) //只有哨兵位的也可以通用
{
/* node* newnode = new node(x); //創建新節點
node* tail = _head->_prev; //當前的最后一個節點
tail->_next = newnode;
newnode->_next = _head;
newnode->_prev = tail;
_head->_prev = newnode;*/
insert(iterator(end()), x); //前一個就是尾
}
void push_front(const T& x)
{
insert(iterator(begin),x);
}
void pop_back()
{
erase(iterator(--end()));
}
void pop_front()
{
erase(iterator(begin()));
}
iterator insert(iterator pos, const T& val) //pos位置前插入
{
node* newnode = new node(val);
node* tail = pos._pnode;
newnode->_next = tail;
newnode->_prev = tail->_prev;
newnode->_prev->_next = newnode; //還要讓前一個指向自己
pos._pnode->_prev = newnode;
return iterator(newnode); //需要回傳迭代器;
}
iterator erase(iterator pos) //洗掉pos位置
{
assert(pos._pnode);
assert(pos != end());
node* tail = pos._pnode;
node* ret = tail->_next;
tail->_prev->_next = tail->_next;
tail->_next->_prev = tail->_prev;
delete tail;
return iterator(ret); //回傳下一個
}
bool empty()
{
return begin() == end();
}
size_t size()
{
size_t sz = 0;
iterator it = begin();
while(it != end())
{
sz++;
it++;
}
return sz;
}
void clear()
{
iterator it = begin();
while(it != end())
{
erase(it);
it++;
}
}
private:
node* _head;
};
void PrintList(const list<int>& It)
{
list<int>::const_iterator it = It.begin();
while(it != It.end())
{
cout << *it <<endl;
++it;
}
cout << endl;
}
void test1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
*it += 1;
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
PrintList(lt);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302885.html
標籤:其他
上一篇:【Linux 初學篇】(1)目錄結構、遠程登錄、vim 和 vi、用戶管理
下一篇:VulnHub-FOG
