C++List容器常用函數(shù)接口刨析
一、基本結(jié)構(gòu)
由源代碼可知,list容器是有一個帶頭雙向循環(huán)鏈表實現(xiàn),所以我們模擬實現(xiàn)也需要實現(xiàn)一個帶頭雙向循環(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())//用一個匿名對象來做缺省參數(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的迭代器是一個自定義類型的對象,成員變量包含一個指向節(jié)點的指針。
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é)點不屬于迭代器,不需要迭代器釋放 // 拷貝構(gòu)造和賦值重載 -- 默認生成的淺拷貝就可以 // *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;//因為tmp出了作用域就不在了,所以不可以引用返回 } self& operator--() { _node = _node->_prev; return *this; } self operator--(int) { self tmp(*this); _node = _node->_prev; return tmp; }
??????即用一個自定義類型封裝,通過運算符的重載使迭代器實現(xiàn)像指針一樣的操作行為。
三、迭代器的實現(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)造一個匿名對象 } const_iterator end() const { return const_iterator(_head); } iterator begin() { return iterator(_head->_next);//構(gòu)造一個匿名對象來返回 //return _head->_next;//也可以,因為單參數(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是一個迭代器對象 { 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吧,因為尾部迭代器在尾刪后是需要變得 } void pop_front() { erase(begin()); }
??這里均復用了insert和erase函數(shù)。
六、構(gòu)造函數(shù)與賦值重載
list()//帶頭雙向循環(huán)鏈表,初始化要先把頭弄出來 { _head = new Node(); //自定義類型去調(diào)用對應類的構(gòu)造函數(shù),帶不帶這個括號都可 _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中復用insert,insert中完成深拷貝 } }*/ void empty_init() { _head = new Node(); _head->_next = _head; _head->_prev = _head; } //如果我們寫現(xiàn)代寫法,那么必須提供相應的帶參構(gòu)造 template <class InputIterator> list(InputIterator first, InputIterator last) { empty_init(); while (first != last) { push_back(*first);//push_back中調(diào)用insert時會完成相應深拷貝 ++first; } } void swap(list<T>& lt) { std::swap(_head, lt._head);//交換頭節(jié)點 } // lt2(lt1) -- 現(xiàn)代寫法 list(const list<T>& lt) { empty_init();//總不能把一個野指針換給別人呀! 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);//用返回值更新,防止迭代器失效 } }
到此這篇關于C++List容器常用函數(shù)接口刨析的文章就介紹到這了,更多相關C++List容器 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(一)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法中的插入類和交換類的各種排序,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2021-12-12java 出現(xiàn)NullPointerException的原因及解決辦法
這篇文章主要介紹了java 出現(xiàn)NullPointerException的原因及解決辦法的相關資料,這里說明出現(xiàn)NullPointerException 的原因的總結(jié),并說明該如何解決,需要的朋友可以參考下2017-08-08淺談C++日志系統(tǒng)log4cxx的使用小結(jié)詳解
本篇文章是對C++日志系統(tǒng)log4cxx的使用進行了詳細的分析介紹,需要的朋友參考下2013-05-05