STL--list的模拟实现
生活随笔
收集整理的這篇文章主要介紹了
STL--list的模拟实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
List
- list的迭代器
- list鏈表
- list的功能函數
- list測試
- 函數主體
要模擬實現list,必須要熟悉list的底層結構以及其接口的含義,通過上面的學習,這些內容已基本掌握,現
在我們來模擬實現list。
list的迭代器
List 的迭代器
迭代器有兩種實現方式,具體應根據容器底層數據結構實現:
方法:
至于operator–()/operator–(int)釋放需要重載,根據具體的結構來抉擇,雙向鏈表可
以向前 移動,所以需要重載,如果是forward_list就不需要重載–
*/
迭代器實現原理簡要框架
說明:因為list的迭代器會指向下一個節點,所以我們創建一個迭代器結構體,這個結構體成員函數就是一個一個節點的指針_node。該構造函數會通過一個傳遞過來的節點來構造對象
迭代器代碼
析構不需要寫,因為節點的生命周期是跟著鏈表走的,鏈表銷毀了節點才會銷毀
迭代器不是管理一個節點,它是封裝這個節點之后讓我們用同樣的方式去遍歷這個鏈表,而不暴露里面的東西
我們可以理解為迭代器就像是一個隔離層,屏蔽了里面實現的東西,但是外面的使用都是一樣的
list鏈表
(1)空容器構造函數(默認構造函數)
構造一個空容器,不包含任何元素。
(2)填充構造函數
構造一個包含n個元素的容器。每個元素都是val的副本。
(3)范圍構造器
構造一個包含范圍為[first,last)的元素的容器,每個元素由該范圍內其對應元素以相同的順序構造。
(4)復制構造函數
用相同的順序構造一個容器,其中包含x中每個元素的副本。
list的功能函數
//在pos前邊插入iterator insert(iterator pos,const T&x) {node* newnode = new node(x);node* cur = pos._node;node* prev = cur->_prev;cur->_prev = newnode;newnode->_next = cur;newnode->_prev = prev;prev->_next = newnode;return newnode;}iterator erase(iterator pos1) {assert(pos1 != end());node* pos = pos1._node;node* cur = pos->_next;node* prev = pos->_prev;cur->_prev = prev;prev->_next = cur;delete pos;return cur;}//頭插void push_front(const T& x){insert(begin(), x);}//尾刪void pop_back(){erase(--end());//--end尾節點}//頭刪void pop_front(){erase(begin());}void push_back(const T& x){node* newnode = new node(x);node* tail = _head->_prev;tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}list測試
函數主體
acc.h
#pragma once #include<iostream> #include<algorithm> #include<assert.h> using namespace std; namespace td {template<class T>struct _list_node {T _date;_list_node<T>* _next;_list_node<T>* _prev;_list_node(const T& x = T()):_date(x), _next(nullptr), _prev(nullptr){}};//迭代器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){}//淺拷貝 不需要拷貝構造和析構Ref operator*()const {return _node->_date;}//前置++it.operator(&it)構成函數重載self& operator++() {_node = _node->_next;return *this;}//后置++it.operator(&it,0)構成函數重載self& operator++(int) {self tmp(*this);_node = _node->_next;return tmp;}//前置--it.operator(&it)構成函數重載self& operator--() {_node = _node->_prev;return *this;}//后置--it.operator(&it,0)構成函數重載self& operator--(int) {self tmp(*this);_node = _node->_prev;return tmp;}Ptr operator->() const{return &_node->_date;}bool operator!=(const self& s) const {return _node != s._node;}bool operator==(const self& s) const{return !(*this != s);}};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;}~list() {clear();delete _head;}void push_back(const T& x){node* newnode = new node(x);node* tail = _head->_prev;tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}template<class InputIterator>list(InputIterator first, InputIterator last){_head = new node;_head->_next = _head;_head->_prev = _head;while (first != last){push_back(*first);++first;}}list(const list<T>& lt){_head = new node;_head->_next = _head;_head->_prev = _head;list<T> tmp(lt.begin(), lt.end());std::swap(_head, tmp._head);}//lt1 = lt2list<T>& operator=(list<T> lt){std::swap(_head, lt._head);return *this;}/*list(const list<T>& it) {_head = new node();_head->_prev = _head;_head->_next = _head;const_iterator i = it.begin();while (i != it.end()) {push_back(*i);i++;}}*/void clear () {iterator it = begin();while (it != end()) {erase(it++);//不用++這里刪除自動往后移動}}const_iterator begin() const {return _head->_next;}const_iterator end() const {return _head->prev;}iterator begin() {return _head->_next;}iterator end() {return _head;}//在pos前邊插入iterator insert(iterator pos,const T&x) {node* newnode = new node(x);node* cur = pos._node;node* prev = cur->_prev;cur->_prev = newnode;newnode->_next = cur;newnode->_prev = prev;prev->_next = newnode;return newnode;}iterator erase(iterator pos1) {assert(pos1 != end());node* pos = pos1._node;node* cur = pos->_next;node* prev = pos->_prev;cur->_prev = prev;prev->_next = cur;delete pos;return cur;}//頭插void push_front(const T& x){insert(begin(), x);}//尾刪void pop_back(){erase(--end());//--end尾節點}//頭刪void pop_front(){erase(begin());}private:node* _head;}; }源.cpp
#include"acc.h" namespace td {void test_list1(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_front(0);// 遍歷方式1list<int>::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;list<int>::reverse_iterator rit = lt.rbegin();while (rit != lt.rend()){cout << *rit << " ";++rit;}cout << endl;// 遍歷方式2for (auto e : lt){cout << e << " ";}cout << endl;}void test_list2(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list<int>::iterator pos = std::find(lt.begin(), lt.end(), 2);if (pos != lt.end()){lt.insert(pos, 20);}list<int>::iterator it = lt.begin();while (it != lt.end()){if (*it % 2 == 0){it = lt.erase(it);}else{++it;}}// 遍歷方式2for (auto e : lt){cout << e << " ";}cout << endl;} }namespace bit {void print_list(const list<int>& lt){list<int>::const_iterator it = lt.begin();while (it != lt.end()){// const T& it.operator*()//*it = 1;cout << *it << endl;++it;}cout << endl;}void test_list1(){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();list<int>::const_iterator it = lt.begin();while (it == lt.end()){*it = 1;cout << *it << endl;++it;}cout << endl;for (auto e : lt){cout << e << " ";}cout << endl;}struct Point{int x;int y;};void test_list2(){list<Point> lt;lt.push_back({ 1, 2 });lt.push_back({ 3, 3 });lt.push_back({ 5, 6 });list<Point>::iterator it = lt.begin();list<Point>::const_iterator it = lt.begin();while (it != lt.end()){cout << it->x << ":" << it->y << endl;//cout << (*it).x << ":" << (*it).y << endl;++it;}cout << endl;}void test_list3(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list<int> copy = lt;for (auto e : copy){cout << e << " ";}cout << endl;} }int main() {bit::test_list3();return 0; }總結
以上是生活随笔為你收集整理的STL--list的模拟实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创客(米思奇编程)-03-传感器
- 下一篇: mongodb 服务器性能监控,Mong