list的介紹
詳細請看(https://cplusplus.com/reference/list/list/?kw=list)
1.list是一個可以在常數范圍內在任意位置,進行插入和刪除的序列式容器,并且此容器可以前后雙向迭代。
2.list的底層實質是一個雙向鏈表結構,雙向鏈表里每個元素的存放都互不相關,在節點中可以通過指針來指向前一個元素和后一個元素
3.相對于vector等序列式容器,list在任意位置上的插入、刪除元素的效率會更高。
4.但是list與其他序列式容器相比,最大缺陷是不支持任意位置的隨機訪問,必須要從已知位置迭代到當前的位置,只有這樣才可以進行數據的讀取。
簡要使用
建立及其數據的尾插
void test_list1()
{list<int> l1;l1.push_back(1);l1.push_back(2);l1.push_back(3);l1.push_back(4);l1.push_back(5);list<int>::iterator it = l1.begin();//讀取需要用迭代器讀取,不能用下標while (it != l1.end()){cout << *it << " ";++it;}cout << endl;for (auto e : l1){cout << e << " ";}cout << endl;
}
排序
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);l1.reverse();//進行了逆序
l1.sort();//默認升序//降序
greater<int> gt;
l1.sort(gt);//傳入這個即可l1.sort(greater<int>());//也可以用匿名函數//升序限定范圍
sort(v.begin(), v.end());
數據的拷貝
lt2.assign(v.begin(), v.end());//粘貼
去重函數
list<int> L;
L.push_back(1);
L.push_back(4);
L.push_back(3);
L.push_back(3);L.unique();
//但在注意的是,去重函數本質是用一個雙指針進行刪除,連續相同的會留下一個,若是多個重復數據,若不是連//續的,那么結果還是會出現重復的元素。
//——所以需要先進行排序sort
分割函數
list<int> l1, l2;
for (int i = 1; i <= 4; i++)
{l1.push_back(i);
}
for (int i = 5; i <= 8; i++)
{l2.push_back(i);
}auto it = l1.begin();
l1.splice(it, l2);//將l2中的數據,全部插入到l1的it處
list的模擬實現
現在復現list類的簡要底層代碼——實現的結構體邏輯和用C實現相似。
namespace bit
{template<class T>struct list_node//節點的結構,并且對節點進行初始化{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& x = T())
//給缺省值,由于不知道是什么類型,所以用模板名T進行位置類型變量的初始化(作為缺省)->T():_data(x),_next(nullptr),_prev(nullptr){}};template<class T>class list//鏈表的結構,需要一個頭結點即可{typedef list_node<T> Node;public:private:Node* _head;};
}
構造函數
void empty_init()
{_head=new Node;_head->_next=_head;_head->_prev=_head;
}list()
{empty_init();
}
尾插
void push_back(const T& x)
{Node*tail=_head->_prev;Node* newnode=new Node(x);//建立一個包含x的節點tail->_next=newnode;newnode->_prev=tail;newnode->_next=_head;_head->_prev=newnode;++_size;
}
節點迭代器
倘若我們有以下的代碼
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);list<int>::iterator it = l1.begin();
while (it != l1.end())
{cout << *it << " ";++it;
}
這樣子會顯示報錯?這是為什么?——結構體存放的是結點,解引用出來的是一整個結構體
而且節點存放的空間不是連續存放的,所以需要寫一個結構體,進行對于' 節點的指針 '的封裝。
template<class T>
struct _list_iterator
{ typedef _list_iterator<T> self;typedef list_node<T> Node;Node* _node;//結構體里存放的就是節點
}
迭代器的構造
_list_iterator(Node* node)//此迭代器的本質也就是用節點的指針:_node(node)
{}
節點指針++
self& operator()
{_node=_node->next;return *this;
}
解引用獲取數據
T& operator*()//解引用也要進行一個函數的封裝,要的是這個數據,所以用T
{return _node->_data;
}
指針的比較
bool operator!=(const self& s)//結點之間比較,所以用迭代器的結構體名稱
{return _node != s._node;
}
list類
有了迭代器之后,對于list類作補充
template<class T>
class list
{
public:typedef _list_iterator<T> iterator;iterator begin(){return _head->_next;}iterator end(){return _head;}}
插入
iterator insert(iterator pos, const T& val)//由于插入是利用節點之間的連接進行的,且需要用迭代器
{Node* cur=pos._node;Node* newnode=new Node(val);Node* prev=cur->_prev;prev->_next=newnode;newnode->_prev=prev;newnode->_next=cur;cur->_prev=newnode;++_size;return iterator(newnode);//insert插入函數的結果會返回插入節點的位置
}
刪除
iterator erase(iterator pos)
{Node* cur=pos._node;Node* prev=cur->_prev;Node* next=cur->_next;delete cur;prev->_next=next;next->_prev=prev;--_size;return iterator(next);//erase要返回下一個元素的指針
}
有了insert和erase后,可以方便地實現其他函數
void push_front(const T& x)//頭插
{insert(begin(), x);
}
void pops_front(const T& x)//頭刪
{erase(begin());
}
void pops_back(const T& x)//尾刪
{erase(end());
}
清理函數
void clear()
{iterator it=begin();while(it!=end()){it=erase(it);//erase會返回一個指向下一個位置的地址,用erase可以減少代碼量}
}
拷貝重載/賦值拷貝
void swap(list<T>& lt)
{std::swap(_head, lt._head);std::swap(_size, lt._size);
}list<int>& operator=(list<int>& lt)
{swap(lt);return *this;
}list(const list<T>& lt)
{empty_init();for (auto e : lt){push_back(e);//直接用push_back進行數據的插入即可,不需要再作拷貝結點}
}
析構函數
~list()
{clear();delete _head;_head = nullptr;
}
完善節點指針的結構體
template<class T>//用一個迭代器的結構體進行結點的封裝 ++
//struct 默認公有,但是class類需要做些聲明
struct _list_iterator
{typedef list_node<T> Node;typedef _list_iterator<T> self;Node* _node;//創造一個結點_list_iterator(Node* node)//用一個結點的指針就能夠造出一個迭代器:_node(node)//傳入begin,就會有對應位置的一個初始化結點出來{}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;}T& operator*()//解引用也要進行一個函數的封裝,要的是這個數據,所以用T{return _node->_data;}T* operator->(){return &_node->_data;}bool operator!=(const self& s)//結點之間比較,所以用迭代器的結構體名稱{return _node != s._node;}bool operator==(const self& s)//結點之間比較,所以用迭代器的結構體名稱{return _node == s._node;}
};
const迭代器
我們知道,若無const修飾,那么就可以進行' 讀寫 ',若有const修飾,那么就只能進行' 讀 '。
倘若用const修飾迭代器呢?(const iterator)——err,這樣子會是迭代器本身不能修改,那么應該怎么表示?
——const_iterator 重新定義的一個類型,這樣本身可以修改,指向的內容不能修改。
那么可以在迭代器基礎上進行修改,成為const迭代器
template<class T>//用一個迭代器的結構體進行結點的封裝 ++
//struct 默認公有,但是class類需要做些聲明
struct _list_const_iterator
{typedef list_node<T> Node;typedef _list_const_iterator<T> self;Node* _node;_list_const_iterator(Node* node):_node(node){}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;}//加上const修改后,迭代器里的內容就不能被修改const T& operator*()//解引用也要進行一個函數的封裝,要的是這個數據,所以用T{return _node->_data;}const T* operator->(){return &_node->_data;}bool operator!=(const self& s)//結點之間比較,所以用迭代器的結構體名稱{return _node != s._node;}bool operator==(const self& s)//結點之間比較,所以用迭代器的結構體名稱{return _node == s._node;}
};
現在有了兩個迭代器,但是這兩個迭代器高度相似,僅有兩個成員函數的返回值類型不同,那么有什么方法可以像模板那樣子實現類型的簡化呢?——同一個類模板,實例化參數不同,就是完全不同的類型。
改進
兩個類歸為一類
template<class T,class Ref,class Ptr>
struct _list_iterator
{typedef list_node<T> Node;typedef _list_iterator<T,Ref,Ptr> self;//模板中的類型T,可以用Ref和Ptr來分別取代//其中,T& 又可以被Ref代替 T* 可以被Ptr代替Node* _node;//創造一個結點_list_iterator(Node* node)//用一個結點的指針就能夠造出一個迭代器:_node(node)//傳入begin,就會有對應位置的一個初始化結點出來{}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;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}bool operator!=(const self& s)//結點之間比較,所以用迭代器的結構體名稱{return _node != s._node;}bool operator==(const self& s)//結點之間比較,所以用迭代器的結構體名稱{return _node == s._node;}
};
那么對于list類,要通過兩個模板參數進行相關的控制
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 iterator(_head->_next);//兩種寫法}iterator end(){return _head;}const_iterator begin()const{return const_iterator(_head->_next);}const_iterator end()const{return _head;}
....//
}