C++:list容器--模擬實現(下篇)

1. 模擬實現 list 一些常用接口

// list.h
#pragma once
#include <assert.h>
#include "Iterator.h"namespace room
{template<class T>struct list_node{list_node<T>* _next;list_node<T>* _prev;T _data;list_node(const T& x = T()):_next(nullptr),_prev(nullptr),_data(x){}};// 兩個迭代器 -- 合成一個模板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*(){return _node->_data;}Ptr operator->(){return &_node->_data;}// 前置++運算符重載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;}// !=運算符重載bool operator!=(const Self& it){return _node != it._node;}bool operator==(const Self& it){return _node == it._node;}};//// 迭代器//template<class T>//struct list_iterator//{//	typedef list_node<T> Node;//	Node* _node;//	list_iterator(Node* node)//		:_node(node)//	{}//	// 解引用運算符重載//	T& operator*()//	{//		return _node->_data;//	}//	T* operator->()//	{//		return &_node->_data;//	}//	//	// ++運算符重載//	list_iterator<T>& operator++()//	{//		_node = _node->_next;//		return *this;//	}//	// !=運算符重載//	bool operator!=(const list_iterator<T>& it)//	{//		return _node != it._node;//	}//	bool operator==(const list_iterator<T>& it)//	{//		return _node == it._node;//	}//};//// 迭代器//template<class T>//struct list_const_iterator//{//	typedef list_node<T> Node;//	Node* _node;//	list_const_iterator(Node* node)//		:_node(node)//	{}//	// 解引用運算符重載//	const T& operator*()//	{//		return _node->_data;//	}//	const T* operator->()//	{//		return &_node->_data;//	}//	// ++運算符重載//	list_const_iterator<T>& operator++()//	{//		_node = _node->_next;//		return *this;//	}//	// !=運算符重載//	bool operator!=(const list_const_iterator<T>& it)//	{//		return _node != it._node;//	}//	bool operator==(const list_const_iterator<T>& it)//	{//		return _node == it._node;//	}//};template<class T>class list{typedef list_node<T> Node;public:/*typedef Node* iterator;typedef list_iterator<T> iterator;typedef list_const_iterator<T> const_iterator;*/typedef list_iterator<T, T&, T*> iterator;typedef list_iterator<T, const T&, const T*> const_iterator;typedef ReverseIterator<iterator, T&, T*> reverse_iterator;typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;// 第一種寫法//reverse_iterator rbegin()//{//	return reverse_iterator(--end());//}//reverse_iterator rend()//{//	return reverse_iterator(end());//}// 對稱reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}const_reverse_iterator rbegin() const{return const_reverse_iterator(end());}const_reverse_iterator rend() const{return const_reverse_iterator(begin());}iterator begin(){//return iterator(_head->_next);iterator it(_head->_next);return it;}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}void empty_init(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(){/*_head = new Node;_head->_prev = _head;_head->_next = _head;*/empty_init();}list(initializer_list<T> lt){empty_init();for (auto& e : lt){push_back(e);}}// lt2(lt1)list(const list<T>& lt){empty_init();for (auto& e : lt){push_back(e);}}// lt1 = lt3list<T>& operator=(list<T> lt){swap(lt);return *this;}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}~list(){clear();delete _head;_head = nullptr;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}size_t size() const{return _size;}void push_back(const T& x){/*Node* newnode = new Node(x);Node* tail = _head->_prev;newnode->_prev = tail;tail->_next = newnode;newnode->_next = _head;_head->_prev = newnode;*/insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_front(){erase(begin());}void pop_back(){erase(--end());}void insert(iterator pos, const T& x){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;++_size;}iterator erase(iterator pos){assert(pos != end());Node* cur = pos._node;Node* nextNode = cur->_next;Node* prevNode = cur->_prev;prevNode->_next = nextNode;nextNode->_prev = prevNode;delete cur;--_size;return iterator(nextNode);}private:Node* _head;size_t _size;};
}

2. list 的反向迭代器

? ? ? ? 反向迭代器的 ++ 就是正向迭代器的 --,反向迭代器的 -- 就是正向迭代器的 ++。因此,反向迭代器的實現可以借助正向迭代器,即:反向迭代器內部可以包含一個正向迭代器,對正向迭代器的接口進行包裝即可。

// Iterator.h
// 所有容器的反向迭代器
// 迭代器適配器
#pragma oncetemplate<class Iterator, class Ref, class Ptr>
class ReverseIterator
{
public:typedef ReverseIterator<Iterator, Ref, Ptr> Self;ReverseIterator(Iterator it): _it(it){}Ref operator*(){//return *_it;	// 第一種寫法Iterator tmp = _it;--tmp;return *tmp;}Ptr operator->(){return &(operator*());}Self& operator++(){--_it;return *this;}Self& operator--(){++_it;return *this;}bool operator!=(const Self& s){return _it != s._it;}bool operator==(const Self& s){return _it == s._it;}private:Iterator _it;
};

3. list 與 vector 的對比

????????vector 與 list 都是STL中非常重要的序列式容器,由于兩個容器的底層結構不同,導致其特性以及應用場景不同,其主要不同如下:

vectorlist
底層結構動態順序表,一段連續空間帶頭結點的雙向循環鏈表
隨機訪問支持隨機訪問,訪問某個元素效率O(1)不支持隨機訪問,訪問某個元素效率O(n)
插入和刪除任意位置插入和刪除效率低,需要挪動元素,時間復雜度為O(n),插入時有可能需要擴容,擴容:開辟新空間,拷貝元素,釋放舊空間,導致效率更低任意位置插入和刪除效率高,不需要挪動元素,時間復雜度為O(1)
空間利用率底層為連續空間,不容易造成內存碎片,空間利用率高,緩存利用率高底層結點動態開辟,小結點容易造成內存碎片,空間利用率低,緩存利用率低
迭代器原生態指針對原生態指針(結點指針)進行封裝
迭代器失效在插入元素時,要給所有的迭代器重新賦值,因為插入元素有可能會導致重新擴容,致使原來迭代器失效,刪除時,當前迭代器需要重新賦值否則會失效插入元素不會導致迭代器失效,刪除元素時,只會導致當前迭代器失效,其他迭代器不受影響
使用場景需要高效存儲,支持隨機訪問,不關心插入刪除效率大量插入和刪除操作,不關心隨機訪問

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/95110.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/95110.shtml
英文地址,請注明出處:http://en.pswp.cn/web/95110.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

邊緣計算:一場由物理定律發起的“計算革命”

專欄引言:在前面的文章中,我們探討了云計算如何將計算資源變成了“數字水電煤”,構建了一個強大的中心化數字帝國。然而,當這個帝國試圖將它的觸角伸向物理世界的每一個角落時,卻遭遇了兩位“上古之神”的無情阻擊——光速與帶寬。今天,我們將聚焦于一場由物理定律發起的…

量化模型部署工具llama.cpp

量化模型部署工具llama.cppllama.cppllama.cpp 是什么使用場景是什么如何使用&#xff1f;第 1 步&#xff1a;獲取量化模型第 2 步&#xff1a;編譯 llama.cpp第 3 步&#xff1a;運行推理完整 Demo&#xff1a;與 Llama 3 對話進階使用&#xff1a;Python 集成總結概念解釋1.…

【光照】[光照模型]發展里程碑時間線

【從UnityURP開始探索游戲渲染】專欄-直達 圖形學光照模型發展史&#xff1a;技術演進與里程碑 section 基礎奠基期(1960s-1970s) 1967 &#xff1a; Lambert模型(漫反射) - Bui Tuong Phong提出1971 &#xff1a; Gouraud著色 - Henri Gouraud發明頂點插值著色1973 &#xf…

【從零開始java學習|第十篇】面向對象

目錄 一、面向對象介紹 二、類和對象 1. 類&#xff08;Class&#xff09;&#xff1a;對象的模板 2. 對象&#xff08;Object&#xff09;&#xff1a;類的實例 三、封裝 1. 封裝的概念 2. 封裝的優勢 四、就近原則和 this 關鍵字 1. 就近原則 2. this 關鍵字 五、…

Spark算子調優

Spark中可用下面的算子對數據計算進行優化處理&#xff0c;包括&#xff1a; mapPartition&#xff1a;一次處理一個分區數據&#xff0c;能夠使用mapPartition的盡量使用&#xff0c;但是使用時會一次性讀取整個分區數據到內存&#xff0c;占內存很大&#xff0c;同理還有fore…

碼農特供版《消費者權益保護法》逆向工程指北——附源碼級注釋與異常處理方案

尊敬的審核&#xff1a; 本人文章《碼農特供版〈消費者權益保護法〉逆向工程指北——附源碼級注釋與異常處理方案》 1. 純屬技術交流&#xff0c;無任何違法內容 2. 所有法律引用均來自公開條文 3. 請依據《網絡安全法》第12條“不得無故刪除合法內容”處理 附&#xff1a;本文…

MQTT 連接建立與斷開流程詳解(二)

三、核心機制與最佳實踐&#xff08;一&#xff09;會話管理與 QoS 保障Clean Session vs 持久會話&#xff1a;在 MQTT 連接中&#xff0c;會話管理是一個重要的概念&#xff0c;其中 Clean Session 和持久會話是兩種不同的會話模式。Clean Session&#xff0c;當設置為 1 時&…

[光學原理與應用-332]:ZEMAX - 序列模式與非序列模式的本質、比較

序列模式&#xff08;Sequential Mode&#xff09;與非序列模式&#xff08;Non-Sequential Mode&#xff09;是ZEMAX光學設計軟件中的兩種核心設計模式&#xff0c;二者在光路定義、分析工具、應用場景等方面存在本質差異。以下是兩者的詳細比較&#xff1a;一、本質差異光路定…

WeakAuras Lua Script (My Version)

分享下我的WA的簡約配置&#xff0c;大多數都是團隊框架高亮&#xff0c;輔助大腳DBM監控 表格&#xff1a; WeakAuras Lua Script &#xff1c;BiaoGe&#xff1e;_wa拍賣字符串-CSDN博客 ICC 監控&#xff0c;只要團隊框架監控 WeakAuras Lua Script ICC &#xff08;Barne…

【Python+requests】解決Python requests中的ProxyError:SSL版本錯誤問題詳解

解決Python requests中的ProxyError&#xff1a;SSL版本錯誤問題詳解 在使用Python進行網絡請求時&#xff0c;很多人都會用到requests庫配合代理服務器進行調試或抓包。但有時會遇到令人困惑的ProxyError&#xff0c;尤其是伴隨SSLError: [SSL: WRONG_VERSION_NUMBER]這樣的錯…

基于deepseek的Spring boot入門

一次跟著deepseek記筆記的嘗試&#xff0c;由于CSDN沒有思維導圖&#xff0c;只能按層級記錄提問 如果我想知道一個springboot項目的基本結構&#xff0c;比如用到了哪些組件&#xff0c;入口在哪&#xff0c;數據庫配置是怎樣的 應該從哪里開始 springboot有哪些常用注解 一個…

macOS 15.6 ARM golang debug 問題

前言 最近使用macmini m4在使用golang debug發現一些奇怪的問題&#xff0c;debug到c代碼&#xff0c;莫名其妙&#xff0c;而且不知道什么原因&#xff0c;知道搜索查詢&#xff0c;才發現是蘋果的Command Line Tools 的鍋&#xff0c;macOS 15果然是一堆bug&#xff0c;畢竟…

有個需求:切換車隊身份實現Fragment的Tab隱藏顯示(車隊不顯示獎賞)

核心實現&#xff1a; 1使用mmkv保存切換的身份 2借助eventbus實現通知Fragment的tab更新private void switchFleet(boolean isMore, EnterpriseInfo enterpriseInfo) {if (isMore) {tvSwitchFleetTitle.setText(getText(R.string.switch_to_other_accounts));} else {tvSwitch…

在 Android Studio 中修改 APK 啟動圖標(2025826)

在 Android Studio 中修改 Android 12 應用圖標可以按照以下步驟進行&#xff1a;1、準備圖標資源準備一個啟動圖標&#xff08;建議使用 SVG 格式或高分辨率 PNG&#xff0c;推薦尺寸為 512x512 像素&#xff09;圖標應符合 Android 12 的設計規范&#xff08;自適應圖標&…

Linux三劍客grep-sed-awk

linux三劍客-grep、sed、awk 文章目錄linux三劍客-grep、sed、awk1.正則表達式1.1正則表達式&#xff1f;1.2應用場景&#xff1f;-誰可以用&#xff1f;1.3正則注意事項&#xff08;避免90%以上的坑&#xff09;1.4正則符號1.5正則VS通配符2.基礎正則2.1 ^ 以...開頭的行2.2 $…

給某個conda環境安裝CUDA 12.4版本 全局CUDA不變

文章目錄&#x1f3af; 正確的安裝命令&#x1f4cb; 為什么這個命令安全&#xff1f;?? 重要說明&#x1f527; 安裝后環境配置方法一&#xff1a;在虛擬環境中設置方法二&#xff1a;使用conda環境變量&#x1f9ea; 驗證安裝&#x1f4ca; 版本共存確認&#x1f4a1; 額外…

【C++】日期類實現詳解:代碼解析與復用優化

代碼總覽與總結// 實現一個完善的日期類 class Date { public:int GetMonthDay(int year, int month){int monthDays[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };// 閏年2月if (month 2 && ((year % 4 0 && year % 100 ! 0) || year % 400 0))return …

零基礎json入門教程(基于vscode的json配置文件)

一、什么是 JSON&#xff1f;—— 最核心的概念JSON&#xff08;JavaScript Object Notation&#xff09;是一種輕量級的數據交換格式&#xff0c;長得像鍵值對的集合&#xff0c;用途是存儲和傳遞數據。在 VS Code 中&#xff0c;它常被用來寫配置文件&#xff08;比如調試配置…

深入對比分析SpringCloud和Dubbo兩大微服務框架的核心架構與特性。

1 微服務架構演進與核心概念1.1 微服務架構的本質微服務架構是一種將單一應用程序劃分為一組小型服務的方法&#xff0c;每個服務運行在自己的進程中&#xff0c;服務之間通過輕量級的通信機制進行協作。這些服務圍繞業務能力構建&#xff0c;并能夠獨立部署到生產環境中。微服…

鴻蒙ArkTS 核心篇-14-條件表達式(三目運算符)

目錄 根據邏輯條件結果&#xff0c;執行不同的表達式&#xff0c;得到不同結果 DevEco Studio代碼實戰 ctrl s 日志輸出 總結 根據邏輯條件結果&#xff0c;執行不同的表達式&#xff0c;得到不同結果 DevEco Studio代碼實戰 let num1: number 100 let num2: number 20l…