欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++List容器常用函數(shù)接口刨析

 更新時間:2022年08月03日 11:31:09   作者:Rookiep  
最近我學習了C++中的STL庫中的list容器,對于常用容器,我們不僅要會使用其常用的函數(shù)接口,我們還有明白這些接口在其底層是如何實現(xiàn)的。所以特意整理出來一篇博客供我們學習

一、基本結(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++ set的使用方法詳解

    C++ set的使用方法詳解

    這篇文章主要介紹了C++ set的使用方法詳解的相關資料,希望通過本文能幫助到大家,讓大家理解掌握set的使用方法,需要的朋友可以參考下
    2017-10-10
  • 使用QPainter畫一個3D正方體

    使用QPainter畫一個3D正方體

    這篇文章主要為大家詳細介紹了使用QPainter畫一個3D正方體,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(一)

    C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(一)

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法中的插入類和交換類的各種排序,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2021-12-12
  • C++中function的實現(xiàn)原理詳解

    C++中function的實現(xiàn)原理詳解

    類模版std::function是一種通用、多態(tài)的函數(shù)封裝。function的實例可以對任何可以調(diào)用的目標實體進行存儲、復制、和調(diào)用操作。本文主要聊聊它的實現(xiàn)原理,需要的可以參考一下
    2022-12-12
  • java 出現(xiàn)NullPointerException的原因及解決辦法

    java 出現(xiàn)NullPointerException的原因及解決辦法

    這篇文章主要介紹了java 出現(xiàn)NullPointerException的原因及解決辦法的相關資料,這里說明出現(xiàn)NullPointerException 的原因的總結(jié),并說明該如何解決,需要的朋友可以參考下
    2017-08-08
  • STL容器之list源碼詳細解讀

    STL容器之list源碼詳細解讀

    這篇文章主要介紹了STL容器之list源碼詳細解讀,相對于vector的連續(xù)線性空間,list就顯得更加復雜,它每插入或者刪除一個元素,就配置或釋放一個元素空間,需要的朋友可以參考下
    2024-01-01
  • C++解析特殊符號tab和換行符號詳情

    C++解析特殊符號tab和換行符號詳情

    這篇文章主要給大家介紹的是C++解析一些特殊符號tab、換行符號的一些相關資料,需要的小伙伴可以參考下面文章的具體內(nèi)容
    2021-09-09
  • C語言菜鳥基礎教程之Hello World

    C語言菜鳥基礎教程之Hello World

    C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產(chǎn)生少量的機器碼以及不需要任何運行環(huán)境支持便能運行的編程語言。
    2017-10-10
  • C語言中%c與%s的區(qū)別與劃分詳解

    C語言中%c與%s的區(qū)別與劃分詳解

    這篇文章主要介紹了C語言中%c與%s的區(qū)別與劃分詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 淺談C++日志系統(tǒng)log4cxx的使用小結(jié)詳解

    淺談C++日志系統(tǒng)log4cxx的使用小結(jié)詳解

    本篇文章是對C++日志系統(tǒng)log4cxx的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論