C++List容器常用函數(shù)接口刨析
一、基本結(jié)構(gòu)
由源代碼可知,list容器是有一個(gè)帶頭雙向循環(huán)鏈表實(shí)現(xiàn),所以我們模擬實(shí)現(xiàn)也需要實(shí)現(xiàn)一個(gè)帶頭雙向循環(huán)鏈表的數(shù)據(jù)結(jié)構(gòu)。
template<class T> struct list_node { list_node<T>* _next; list_node<T>* _prev; T _data; list_node(const T& val = T())//用一個(gè)匿名對象來做缺省參數(shù) :_next(nullptr) , _prev(nullptr) , _data(val) {} }; template<class T> class list { public: typedef list_node<T> Node; private: Node* _head; };
二、list的迭代器的構(gòu)造
list的迭代器與vector的迭代器不一樣,list的迭代器是一個(gè)自定義類型的對象,成員變量包含一個(gè)指向節(jié)點(diǎn)的指針。
template<class T, class Ref, class Ptr> struct __list_iterator { typedef list_node<T> Node; typedef __list_iterator<T, Ref, Ptr> self; Node* _node; __list_iterator(Node* node) :_node(node) {} // 析構(gòu)函數(shù) -- 節(jié)點(diǎn)不屬于迭代器,不需要迭代器釋放 // 拷貝構(gòu)造和賦值重載 -- 默認(rèn)生成的淺拷貝就可以 // *it Ref operator*() { return _node->_data; } Ptr operator->() { //return &(operator*()); return &_node->_data; } self& operator++() { _node = _node->_next; return *this; } self operator++(int) { self tmp(*this);//拷貝構(gòu)造 _node = _node->_next; return tmp;//因?yàn)閠mp出了作用域就不在了,所以不可以引用返回 } self& operator--() { _node = _node->_prev; return *this; } self operator--(int) { self tmp(*this); _node = _node->_prev; return tmp; }
??????即用一個(gè)自定義類型封裝,通過運(yùn)算符的重載使迭代器實(shí)現(xiàn)像指針一樣的操作行為。
三、迭代器的實(shí)現(xiàn)
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_iterator<T, const T&, const T*> begin() const { // list_node<int>* return __list_iterator<T, const T&, const T*>(_head->_next); //構(gòu)造一個(gè)匿名對象 } const_iterator end() const { return const_iterator(_head); } iterator begin() { return iterator(_head->_next);//構(gòu)造一個(gè)匿名對象來返回 //return _head->_next;//也可以,因?yàn)閱螀?shù)構(gòu)造函數(shù)支持隱式類型轉(zhuǎn)換。 //iterator it = _head->_next 隱式調(diào)用 } iterator end() { return iterator(_head); } }
四、insert,erase
// 插入在pos位置之前 iterator insert(iterator pos, const T& x)//pos是一個(gè)迭代器對象 { Node* newNode = new Node(x); Node* cur = pos._node; Node* prev = cur->_prev; // prev newnode cur prev->_next = newNode; newNode->_prev = prev; newNode->_next = cur; cur->_prev = newNode; return iterator(newNode); } iterator erase(iterator pos) { assert(pos != end()); Node* cur = pos._node; Node* prev = cur->_prev; Node* next = cur->_next; // prev next prev->_next = next; next->_prev = prev; delete cur; return iterator(next); }
五、push_back,push_front,pop_back,pop_front
void push_back(const T& x) { //Node* tail = _head->_prev; //Node* newnode = new Node(x); _head tail newnode //tail->_next = newnode; //newnode->_prev = tail; //newnode->_next = _head; //_head->_prev = newnode; insert(end(), x); } void push_front(const T& x) { insert(begin(), x); } void pop_back() { erase(--end()); //這里不可以用end()-1吧,因?yàn)槲膊康髟谖矂h后是需要變得 } void pop_front() { erase(begin()); }
??這里均復(fù)用了insert和erase函數(shù)。
六、構(gòu)造函數(shù)與賦值重載
list()//帶頭雙向循環(huán)鏈表,初始化要先把頭弄出來 { _head = new Node(); //自定義類型去調(diào)用對應(yīng)類的構(gòu)造函數(shù),帶不帶這個(gè)括號都可 _head->_next = _head; _head->_prev = _head; } // lt2(lt1)————傳統(tǒng)寫法 /*list(const list<T>& lt) { _head = new Node(); _head->_next = _head; _head->_prev = _head; for (auto e : lt) { push_back(e);//push_back中復(fù)用insert,insert中完成深拷貝 } }*/ void empty_init() { _head = new Node(); _head->_next = _head; _head->_prev = _head; } //如果我們寫現(xiàn)代寫法,那么必須提供相應(yīng)的帶參構(gòu)造 template <class InputIterator> list(InputIterator first, InputIterator last) { empty_init(); while (first != last) { push_back(*first);//push_back中調(diào)用insert時(shí)會完成相應(yīng)深拷貝 ++first; } } void swap(list<T>& lt) { std::swap(_head, lt._head);//交換頭節(jié)點(diǎn) } // lt2(lt1) -- 現(xiàn)代寫法 list(const list<T>& lt) { empty_init();//總不能把一個(gè)野指針換給別人呀! list<T> tmp(lt.begin(), lt.end()); swap(tmp); } // lt2 = lt1 list<T>& operator=(list<T> lt) //list<T> lt = lt1,傳值傳參這一步就調(diào)用了拷貝構(gòu)造完成深拷貝 { swap(lt); return *this; }
??????注意現(xiàn)代寫法的方法
七、析構(gòu)與清空
~list() { clear(); delete _head; _head = nullptr; } void clear() { iterator it = begin(); while (it != end()) { it = erase(it);//用返回值更新,防止迭代器失效 } }
到此這篇關(guān)于C++List容器常用函數(shù)接口刨析的文章就介紹到這了,更多相關(guān)C++List容器 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(一)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法中的插入類和交換類的各種排序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12C++中function的實(shí)現(xiàn)原理詳解
類模版std::function是一種通用、多態(tài)的函數(shù)封裝。function的實(shí)例可以對任何可以調(diào)用的目標(biāo)實(shí)體進(jìn)行存儲、復(fù)制、和調(diào)用操作。本文主要聊聊它的實(shí)現(xiàn)原理,需要的可以參考一下2022-12-12java 出現(xiàn)NullPointerException的原因及解決辦法
這篇文章主要介紹了java 出現(xiàn)NullPointerException的原因及解決辦法的相關(guān)資料,這里說明出現(xiàn)NullPointerException 的原因的總結(jié),并說明該如何解決,需要的朋友可以參考下2017-08-08淺談C++日志系統(tǒng)log4cxx的使用小結(jié)詳解
本篇文章是對C++日志系統(tǒng)log4cxx的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05