国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C++:關(guān)于模擬實現(xiàn)vector和list中迭代器模塊的理解

這篇具有很好參考價值的文章主要介紹了C++:關(guān)于模擬實現(xiàn)vector和list中迭代器模塊的理解。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

本篇是關(guān)于vectorlist的模擬實現(xiàn)中,關(guān)于迭代器模塊的更進(jìn)一步理解,以及在前文的基礎(chǔ)上增加對于反向迭代器的實現(xiàn)和庫函數(shù)的對比等

本篇是寫于前面模擬實現(xiàn)的一段時間后,重新回頭看迭代器的實現(xiàn),尤其是在模板角度對list中迭代器封裝的部分進(jìn)行解析,希望可以對迭代器的封裝實現(xiàn)有更深層次的理解,同時將與庫內(nèi)的實現(xiàn)做對比進(jìn)行優(yōu)化處理

list和vector的迭代器對比

listvector的迭代器實現(xiàn)是不一樣的,在vector中的迭代器就是一個原生指針,因此在實現(xiàn)的時候也就是用的原生指針即可,但是對于list來說不能這樣,list的迭代器中例如++--這樣的操作,是不能和vector中的原生指針一樣直接去實現(xiàn)的,而是需要用next或者是prior來模擬這個過程,還有例如*和->這樣的訪問數(shù)據(jù)的模式,因此在list的迭代器實現(xiàn)中是使用了封裝來進(jìn)行實現(xiàn)的

STL中有六大組件,其中迭代器算其中一個,那么也就意味著迭代器具有通用的功能,例如下面的代碼,在使用構(gòu)造函數(shù)時可以使用迭代器進(jìn)行構(gòu)造,而這個構(gòu)造的過程使用的迭代器對于各種容器來說都是通用的:

#include <iostream>
#include <vector>
#include <list>
using namespace std;

int main()
{
	vector<int> v1{ 1,2,3,4,5,3,2,2 };
	vector<int> v2(v1.begin(), v1.end());
	list<int> l1(v1.begin(), v1.end());
	return 0;
}

那么就意味著,在實現(xiàn)迭代器的過程中也是就可以根據(jù)迭代器來進(jìn)行不同容器間的構(gòu)造

list的實現(xiàn)過程

首先,在list的實現(xiàn)過程中要有下面幾個部分組成:list中包含的節(jié)點,list的迭代器訪問,list的節(jié)點之間的關(guān)系

那么首先就是list中節(jié)點的定義,用到了模板,現(xiàn)在再對該部分進(jìn)行解析,其實就是在創(chuàng)建的時候可以根據(jù)需要的內(nèi)容開辟出對應(yīng)的節(jié)點類型

template<class T>
struct list_node
{
	T _data;
	list_node<T>* _next;
	list_node<T>* _prev;

	list_node(const T& x = T())
		:_data(x)
		, _next(nullptr)
		, _prev(nullptr)
	{}
};

有了節(jié)點信息,就可以對list做一些基礎(chǔ)的操作,例如頭插頭刪,尾插尾刪等操作,但對于inserterase這些操作還不能夠?qū)崿F(xiàn),因此就要實現(xiàn)迭代器模塊的內(nèi)容

不管是對于vector還是list,迭代器的本質(zhì)就是用一個原生指針去進(jìn)行訪問等等,但是listvector不同的是,list的存儲地址并不是連續(xù)的,因此在訪問的過程中就需要對迭代器進(jìn)行一定程度的封裝,例如在++或者--的操作符上可以進(jìn)行一些體現(xiàn),因此list迭代器的實現(xiàn)是需要進(jìn)行單獨的封裝的

// 定義正向迭代器
template <class T, class Ref, class Ptr>
class __list_iterator
{
public:
	typedef list_node<T> Node;
	typedef __list_iterator<T, T&, T*> self;
	__list_iterator(Node* node)
		:_node(node)
	{}

	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;
	}
	Ref operator*()
	{
		return _node->_data;
	}
	Ptr operator->()
	{
		return &_node->_data;
	}
	bool operator !=(const self& s)
	{
		return _node != s._node;
	}
	bool operator ==(const self& s)
	{
		return _node == s._node;
	}
	Node* _node;
};

上面的片段對list進(jìn)行了一些封裝,實現(xiàn)了它的一些基礎(chǔ)功能,從代碼中也能看出來,迭代器的本質(zhì)確確實實就是指針,用指針來進(jìn)行一系列的操作,對下面這幾個片段單獨進(jìn)行解析:

	Ptr operator->()
	{
		return &_node->_data;
	}

這是對于迭代器中解引用的運算符重載,這里使用的是&_node->_data的寫法,看似很怪,但是實際上這里的函數(shù)調(diào)用過程應(yīng)該是這樣

C++:關(guān)于模擬實現(xiàn)vector和list中迭代器模塊的理解,C++,# 模擬實現(xiàn),知識總結(jié),c++,list
也就是說,這里對于->進(jìn)行運算符重載后,還需要進(jìn)行解引用,這個運算符重載函數(shù)返回的是一個指針,而這個指針還要繼續(xù)進(jìn)行解引用才能得到用戶想要得到的值,編譯器在這里進(jìn)行了特殊處理,兩個->使得可讀性下降,因此將兩個->進(jìn)行了一定程度的合并,只需要一個->就可以實現(xiàn)解引用的目的

其次是對模板的使用部分

template <class T, class Ref, class Ptr>

	typedef list_node<T> Node;
	typedef __list_iterator<T, T&, T*> self;

這里在最開始定義的時候,就定義了數(shù)據(jù)類型,引用和指針三種模板參數(shù),借助這個參數(shù)就可以用模板將復(fù)雜的內(nèi)容實現(xiàn)簡單化,只需要一份代碼,模板就可以直接實例化出用戶在調(diào)用的時候需要的代碼,在進(jìn)行封裝const迭代器的時候,只需要提供的參數(shù)改為const版本就可以實現(xiàn)目的,很便捷

這樣就實現(xiàn)了正向迭代器的普通版本,而對于const迭代器的版本也只需要進(jìn)行不同的模板參數(shù)就可以實例化出const版本的迭代器

typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;

有了迭代器,在實現(xiàn)鏈表的各種函數(shù)功能就更加方便,例如可以實現(xiàn)eraseinsert的操作,實現(xiàn)了這兩個函數(shù),在進(jìn)行頭插頭刪,尾插尾刪的時候就更加方便,只需要進(jìn)行原地的調(diào)用即可

	// Modifiers
	void push_front(const T& val)
	{
		//Node* newnode = new Node;
		//newnode->_data = val;
		//newnode->_next = _head->_next;
		//_head->_next->_prev = newnode;
		//_head->_next = newnode;
		//newnode->_prev = _head;
		//_size++;
		insert(begin(), val);
	}
	void pop_front()
	{
		//Node* delnode = _head->_next;
		//_head->_next = _head->_next->_next;
		//_head->_next->_prev = _head;
		//delete delnode;
		//delnode = nullptr;
		//_size--;
		erase(begin());
	}
	void push_back(const T& val)
	{
		//Node* newnode = new Node;
		//newnode->_data = val;
		//newnode->_prev = _head->_prev;
		//_head->_prev->_next = newnode;
		//newnode->_next = _head;
		//_head->_prev = newnode;
		//_size++;
		insert(end(), val);
	}
	void pop_back()
	{
		//Node* delnode = _head->_prev;
		//delnode->_prev->_next = _head;
		//_head->_prev = delnode->_prev;
		//delete delnode;
		//delnode = nullptr;
		//_size--;
		erase(--end());
	}
	iterator insert(iterator pos, const T& val)
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* newnode = new Node(val);

		_size++;
		prev->_next = newnode;
		newnode->_prev = prev;
		newnode->_next = cur;
		cur->_prev = newnode;
		return iterator(newnode);
	}
	iterator erase(iterator pos)
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;

		delete cur;
		cur = nullptr;
		_size--;

		prev->_next = next;
		next->_prev = prev;
		return iterator(next);
	}

那么上面是對前面已經(jīng)有過的內(nèi)容進(jìn)行的重新溫故,但是上面的代碼其實是沒有實現(xiàn)反向迭代器的,而在STL的庫中,反向迭代器的定義和正向迭代器其實并不相同,它是直接借助正向迭代器對反向迭代器進(jìn)行適配,有些類似于用vectorlist或者deque對棧和隊列進(jìn)行的適配,也體現(xiàn)了封裝和代碼復(fù)用

template<class Iterator, class Ref, class Ptr>
class ReverseIterator
{
public:
	typedef ReverseIterator<Iterator, Ref, Ptr> Self;

	ReverseIterator(Iterator it)
		:_it(it)
	{}

	Self& operator++()
	{
		--_it;
		return *this;
	}

	Ref operator*()
	{
		return *_it;
	}

	Ptr operator->()
	{
		return _it.operator->();
	}

	bool operator!=(const Self& s)
	{
		return _it != s._it;
	}
private:
	Iterator _it;
};

上面的代碼就是對于反向迭代器的封裝,從中可以看出反向迭代器是使用了正向迭代器的,并且在它的基礎(chǔ)上進(jìn)行的修改,因此這個反向迭代器是可以適配于vector也可以適配于list的

整體來說,對于反向迭代器就是用正向迭代器進(jìn)行適配出來的,體現(xiàn)了模板的好處文章來源地址http://www.zghlxwxcb.cn/news/detail-726757.html

完整代碼

#pragma once

namespace mylist
{
	// 定義節(jié)點內(nèi)容
	template <class T>
	struct list_node
	{
		list_node(const T& x = T())
			:_data(x)
			, _next(nullptr)
			, _prev(nullptr)
		{}
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;
	};

	// 定義正向迭代器
	template <class T, class Ref, class Ptr>
	class __list_iterator
	{
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T, T&, T*> self;
		__list_iterator(Node* node)
			:_node(node)
		{}

		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;
		}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		bool operator !=(const self& s)
		{
			return _node != s._node;
		}
		bool operator ==(const self& s)
		{
			return _node == s._node;
		}
		Node* _node;
	};

	// 定義反向迭代器
	template<class Iterator, class Ref, class Ptr>
	class reverse_iterator
	{
		typedef reverse_iterator<Iterator, Ref, Ptr> self;
	public:
		reverse_iterator(Iterator it)
			:_it(it)
		{}

		self& operator++()
		{
			_it--;
			return *this;
		}
		self& operator++(int)
		{
			self tmp(*this);
			_it--;
			return tmp;
		}
		self& operator--()
		{
			_it++;
			return *this;
		}
		self& operator--(int)
		{
			self tmp(*this);
			_it++;
			return tmp;
		}
		Ref operator*()
		{
			Iterator cur = _it;
			return *(--cur);
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		bool operator !=(const self& s)
		{
			return _it != s._it;
		}
		bool operator ==(const self& s)
		{
			return _it == s._it;
		}

		Iterator _it;
	};

	// 定義鏈表
	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;
		typedef reverse_iterator<iterator, T&, T*> reverse_iterator;
		//typedef reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
		// Constructor
		list()
		{
			empty_init();
		}
		list(const list<T>& lt)
		{
			for (auto x : lt)
			{
				push_back(x);
			}
		}
		list(iterator begin, iterator end)
		{
			empty_init();
			while (begin != end)
			{
				push_back(begin._node->_data);
				begin++;
			}
		}
		//list(reverse_iterator rbegin, reverse_iterator rend)
		//{
		//	empty_init();
		//	while (rbegin != rend)
		//	{
		//		push_back(rbegin._node->_data);
		//		rbegin++;
		//	}
		//}

		// Destructor
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		// Operator=
		list<T>& operator=(const list<T>& lt)
		{
			swap(lt);
			return *this;
		}

		// Iterators
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		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());
		//}

		// Capacity
		bool empty()
		{
			return _size == 0;
		}
		size_t size()
		{
			return _size;
		}

		// Element access
		T& front()
		{
			return begin()._node->_data;
		}
		T& back()
		{
			return (--end())._node->_data;
		}

		// Modifiers
		void push_front(const T& val)
		{
			//Node* newnode = new Node;
			//newnode->_data = val;
			//newnode->_next = _head->_next;
			//_head->_next->_prev = newnode;
			//_head->_next = newnode;
			//newnode->_prev = _head;
			//_size++;
			insert(begin(), val);
		}
		void pop_front()
		{
			//Node* delnode = _head->_next;
			//_head->_next = _head->_next->_next;
			//_head->_next->_prev = _head;
			//delete delnode;
			//delnode = nullptr;
			//_size--;
			erase(begin());
		}
		void push_back(const T& val)
		{
			//Node* newnode = new Node;
			//newnode->_data = val;
			//newnode->_prev = _head->_prev;
			//_head->_prev->_next = newnode;
			//newnode->_next = _head;
			//_head->_prev = newnode;
			//_size++;
			insert(end(), val);
		}
		void pop_back()
		{
			//Node* delnode = _head->_prev;
			//delnode->_prev->_next = _head;
			//_head->_prev = delnode->_prev;
			//delete delnode;
			//delnode = nullptr;
			//_size--;
			erase(--end());
		}
		iterator insert(iterator pos, const T& val)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);

			_size++;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			return iterator(newnode);
		}
		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			delete cur;
			cur = nullptr;
			_size--;

			prev->_next = next;
			next->_prev = prev;
			return iterator(next);
		}
		void swap(const list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}
		void clear()
		{
			Node* cur = _head->_next;
			while (cur != _head)
			{
				Node* next = cur->_next;
				delete(cur);
				cur = next;
			}
			_head->_next = _head;
			_head->_prev = _head;
		}


		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_head->_data = T();
			_size = 0;
		}

		void Print()
		{
			Node* cur = _head->_next;
			while (cur != _head)
			{
				cout << cur->_data << "->";
				cur = cur->_next;
			}
			cout << "null" << endl;
		}
	private:
		Node* _head;
		size_t _size;
	};

	void test_iterator()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		cout << "iterator constructor:";
		list<int> lt1(lt.begin(), lt.end());
		lt1.Print();
		cout << "front():" << lt.front() << endl;
		cout << "back():" << lt.back() << endl;
		mylist::__list_iterator<int, int&, int*> it = lt.begin();
		while (it != lt.end())
		{
			std::cout << *it << " ";
			it++;
		}
		cout << endl;
	}
	void test_clear()
	{
		list<int> lt1;
		cout << "push_back:" << endl;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		lt1.Print();
		lt1.clear();
		lt1.push_back(5);
		lt1.push_back(6);
		lt1.push_back(7);
		lt1.push_back(8);
		lt1.Print();
	}
	void test_push_pop()
	{
		list<int> lt1;
		cout << "push_back:" << endl;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		lt1.Print();
		cout << "pop_back:" << endl;
		lt1.pop_back();
		lt1.Print();

		list<int> lt2;
		cout << "push_front:" << endl;
		lt2.push_front(1);
		lt2.push_front(2);
		lt2.push_front(3);
		lt2.push_front(4);
		lt2.Print();
		cout << "pop_front:" << endl;
		lt2.pop_front();
		lt2.Print();
	}
	void test_reverse_iterator()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		cout << "iterator constructor:";
		//list<int> lt1(lt.rbegin(), lt.rend());
		//lt1.Print();
		cout << "front():" << lt.front() << endl;
		cout << "back():" << lt.back() << endl;
		mylist::list<int>::reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			std::cout << *rit << " ";
			rit++;
		}
		cout << endl;
	}

	void test()
	{
		test_reverse_iterator();
		//test_push_pop();
		//test_clear();
	}
}

到了這里,關(guān)于C++:關(guān)于模擬實現(xiàn)vector和list中迭代器模塊的理解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • C++:模擬實現(xiàn)list及迭代器類模板優(yōu)化方法

    C++:模擬實現(xiàn)list及迭代器類模板優(yōu)化方法

    本篇模擬實現(xiàn)簡單的list和一些其他注意的點 如下所示是利用拷貝構(gòu)造將一個鏈表中的數(shù)據(jù)挪動到另外一個鏈表中,構(gòu)造兩個相同的鏈表 lt作為形參,傳引用可以提高傳參的效率,同時應(yīng)該加上const修飾來保證不會因為不小心修改了形參導(dǎo)致外部的實參也被修改,這樣的結(jié)果是

    2024年02月13日
    瀏覽(20)
  • 【C++進(jìn)階(五)】STL大法--list模擬實現(xiàn)以及l(fā)ist和vector的對比

    【C++進(jìn)階(五)】STL大法--list模擬實現(xiàn)以及l(fā)ist和vector的對比

    ??博主CSDN主頁:杭電碼農(nóng)-NEO?? ? ?專欄分類:C++從入門到精通? ? ??代碼倉庫:NEO的學(xué)習(xí)日記?? ? ??關(guān)注我??帶你學(xué)習(xí)C++ ? ???? 本篇文章立足于上一篇文章: list深度剖析(上) 請先閱讀完上一篇文章后再閱讀這篇文章! 本章重點: 本章著重講解list的模擬實現(xiàn) list模擬實

    2024年02月09日
    瀏覽(33)
  • 深入篇【C++】手搓模擬實現(xiàn)list類(詳細(xì)剖析底層實現(xiàn)原理)&&模擬實現(xiàn)正反向迭代器【容器適配器模式】

    深入篇【C++】手搓模擬實現(xiàn)list類(詳細(xì)剖析底層實現(xiàn)原理)&&模擬實現(xiàn)正反向迭代器【容器適配器模式】

    1.一個模板參數(shù) 在模擬實現(xiàn)list之前,我們要理解list中的迭代器是如何實現(xiàn)的。 在vector中迭代器可以看成一個指針,指向vector中的數(shù)據(jù)。它的解引用會訪問到具體的數(shù)據(jù)本身,++會移動到下一個數(shù)據(jù)位置上去,這些都是因為vector具有天生的優(yōu)勢:空間上是連續(xù)的數(shù)組,這樣指

    2024年02月15日
    瀏覽(34)
  • 【C++】STL——list的模擬實現(xiàn)、構(gòu)造函數(shù)、迭代器類的實現(xiàn)、運算符重載、增刪查改

    【C++】STL——list的模擬實現(xiàn)、構(gòu)造函數(shù)、迭代器類的實現(xiàn)、運算符重載、增刪查改

    list使用文章 析構(gòu)函數(shù) ??在定義了一個類模板list時。我們讓該類模板包含了一個內(nèi)部結(jié)構(gòu)體_list_node,用于表示鏈表的節(jié)點。該結(jié)構(gòu)體包含了指向前一個節(jié)點和后一個節(jié)點的指針以及節(jié)點的值。在list中保存了鏈表的頭節(jié)點指針和鏈表長度大小。 ? ? ??因為list的底層實現(xiàn)

    2024年02月14日
    瀏覽(27)
  • STL之list模擬實現(xiàn)(反向迭代器講解以及迭代器失效)

    這次是關(guān)于list的模擬實現(xiàn)的代碼,先看看下面的代碼: 上面是list的代碼,其底層是一個帶頭雙向循環(huán)的鏈表,實現(xiàn)的方法就不說了,相信大家已經(jīng)都會了,然后自己實心的list我沒有寫析構(gòu)函數(shù)等,這個也很簡單,循環(huán)利用成員函數(shù)中的刪除函數(shù)就可以。 先來說說個人認(rèn)為

    2024年02月11日
    瀏覽(19)
  • list【2】模擬實現(xiàn)(含迭代器實現(xiàn)超詳解哦)

    list【2】模擬實現(xiàn)(含迭代器實現(xiàn)超詳解哦)

    在前面,我們介紹了list的使用: 戳我看list的介紹與使用詳解哦 在本篇文章中將重點介紹list的接口實現(xiàn),通過模擬實現(xiàn)可以更深入的理解與使用list 我們模擬實現(xiàn)的 list 底層是一個帶頭雙向循環(huán)鏈表 在實現(xiàn)list時,我們首先需要一個 結(jié)構(gòu)體以表示鏈表中結(jié)點的結(jié)構(gòu) list_node ,

    2024年02月10日
    瀏覽(13)
  • C++模擬實現(xiàn)vector

    C++模擬實現(xiàn)vector

    目錄 1.代碼實現(xiàn) 2.注意事項 1.成員變量 2. 不能使用memcpy函數(shù)拷貝數(shù)據(jù) 1.用string類型測試時,要考慮到vs可能把數(shù)據(jù)存儲在數(shù)組buffer里面 3.insert函數(shù)中指針的失效性 1.加引用,那么就不能傳常量,比如v.begin() + 3 2.加引用,就只能傳變量了 ?4.erase成員函數(shù)的指針的失效性 這邊以

    2024年02月17日
    瀏覽(27)
  • 【C++】vector模擬實現(xiàn)

    【C++】vector模擬實現(xiàn)

    ?? 作者簡介:一名在后端領(lǐng)域?qū)W習(xí),并渴望能夠?qū)W有所成的追夢人。 ?? 個人主頁:不 良 ?? 系列專欄:??C++ ???Linux ?? 學(xué)習(xí)格言:博觀而約取,厚積而薄發(fā) ?? 歡迎進(jìn)來的小伙伴,如果小伙伴們在學(xué)習(xí)的過程中,發(fā)現(xiàn)有需要糾正的地方,煩請指正,希望能夠與諸君一同

    2024年02月13日
    瀏覽(20)
  • C++ 模擬實現(xiàn)vector

    C++ 模擬實現(xiàn)vector

    目錄 一、定義 二、模擬實現(xiàn) 1、無參初始化 2、sizecapacity 3、reserve 4、push_back 5、迭代器 6、empty 7、pop_back 8、operator[ ] 9、resize 10、insert 迭代器失效問題 11、erase 12、帶參初始化 13、迭代器初始化 14、析構(gòu)函數(shù) 15、深拷貝 16、賦值運算符重載 完整版代碼測試代碼 本次參考SGI版

    2024年02月04日
    瀏覽(24)
  • 【c++迭代器模擬實現(xiàn)】

    【c++迭代器模擬實現(xiàn)】

    打怪升級:第52天 什么是STL STL(standard template libaray-標(biāo)準(zhǔn)模板庫):是C++標(biāo)準(zhǔn)庫的重要組成部分,不僅是一個可復(fù)用的組件庫,而且是一個包羅數(shù)據(jù)結(jié)構(gòu)與算法的軟件框架。 STL的版本 原始版本 Alexander Stepanov、Meng Lee 在惠普實驗室完成的原始版本,本著開源精神,他們聲明允許

    2024年02月02日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包