溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2022-08-03 15:15:30 來源:億速云 閱讀:112 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“C++ List容器常用函數(shù)接口實例分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“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)造和賦值重載 -- 默認(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;//因為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());
		}

??這里均復(fù)用了insert和erase函數(shù)。

六、構(gòu)造函數(shù)與賦值重載

		list()//帶頭雙向循環(huán)鏈表,初始化要先把頭弄出來
		{
			_head = new Node();
			//自定義類型去調(diào)用對應(yīng)類的構(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中復(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時會完成相應(yīng)深拷貝
				++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ù)接口實例分析”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI