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

[STL-list]介紹、與vector的對(duì)比、模擬實(shí)現(xiàn)的迭代器問(wèn)題

這篇具有很好參考價(jià)值的文章主要介紹了[STL-list]介紹、與vector的對(duì)比、模擬實(shí)現(xiàn)的迭代器問(wèn)題。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、list使用介紹

  1. ?list的底層是帶頭雙向鏈表結(jié)構(gòu),雙向鏈表中每個(gè)元素存儲(chǔ)在互不相關(guān)的獨(dú)立節(jié)點(diǎn)中,在節(jié)點(diǎn)中通過(guò)指針指向其前一個(gè)元素和后一個(gè)元素。
  2. 與其他的序列式容器相比(array,vector,deque),list通常在任意位置進(jìn)行插入、移除元素的執(zhí)行效率更好
  3. list最大的缺陷是不支持任意位置的隨機(jī)訪問(wèn),比如:要訪問(wèn)list的第6個(gè)元素,必須從已知的位置(比如頭部或者尾部)迭代到該位置,在這段位置上迭代需要線性的時(shí)間開(kāi)銷;
常用接口:

構(gòu)造函數(shù):

構(gòu)造函數(shù) 接口說(shuō)明
list (size_type n, const value_type& val = value_type()) 構(gòu)造的list中包含n個(gè)值為val的元素
list() 構(gòu)造空的list
list (const list& x) 拷貝構(gòu)造函數(shù)
list (InputIterator ?rst, InputIterator last) 用[?rst, last)區(qū)間中的元素構(gòu)造list

代碼演示:

    list<int> l1;                         // 構(gòu)造空的l1
    list<int> l2(4, 100);                 // l2中放4個(gè)值為100的元素
    list<int> l3(l2.begin(), l2.end());  // 用l2的[begin(), end())左閉右開(kāi)的區(qū)間構(gòu)造l3
    list<int> l4(l3);                    // 用l3拷貝構(gòu)造l4

    // 以數(shù)組為迭代器區(qū)間構(gòu)造l5
    int array[] = { 16,2,77,29 };
    list<int> l5(array, array + sizeof(array) / sizeof(int));

    // 列表格式初始化C++11
    list<int> l6{ 1,2,3,4,5 };

list iterator的使用

對(duì)于迭代器的使用我們可以把它理解為一個(gè)指針,指向ist的某個(gè)節(jié)點(diǎn)

函數(shù)聲明 接口說(shuō)明
begin +?end 返回第一個(gè)元素的迭代器+返回最后一個(gè)元素下一個(gè)位置的迭代器
rbegin+?rend 返回第一個(gè)元素的reverse_iterator,即end位置,返回最后一個(gè)元素下一個(gè)位置reverse_iterator,即begin位置

【注意】
1. begin與end為正向迭代器,對(duì)迭代器執(zhí)行++操作,迭代器向后移動(dòng)
2. rbegin(end)與rend(begin)為反向迭代器,對(duì)迭代器執(zhí)行++操作,迭代器向前移動(dòng)

[STL-list]介紹、與vector的對(duì)比、模擬實(shí)現(xiàn)的迭代器問(wèn)題,C++,c++,開(kāi)發(fā)語(yǔ)言

代碼演示:

    list<int> lt(4, 100);  
    list<int>::iterator it = lt.begin();
    while (it != lt.end())
    {
        cout << *it << " ";
        ++it;
    }       
    cout << endl;

    // C++11范圍for的方式遍歷
    for (auto& e : lt)
        cout << e << " ";
     cout << endl;

list modi?ers

函數(shù)聲明 接口說(shuō)明
push_front 在list首元素前插入值為val的元素
pop_front 刪除list中第一個(gè)元素
push_back 在list尾部插入值為val的元素
pop_back 刪除list中最后一個(gè)元素
insert 在list position 位置中插入值為val的元素
erase 刪除list position位置的元素
swap 交換兩個(gè)list中的元素
clear 清空l(shuí)ist中的有效元素

代碼演示:

void TestList1()
{
    int array[] = { 1, 2, 3 };
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));

    // 在list的尾部插入4,頭部插入0
    L.push_back(4);
    L.push_front(0);

    // 刪除list尾部節(jié)點(diǎn)和頭部節(jié)點(diǎn)
    L.pop_back();
    L.pop_front();
}

// insert /erase 
void TestList2()
{
    int array1[] = { 1, 2, 3 };
    list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));

    // 獲取鏈表中第二個(gè)節(jié)點(diǎn)
    auto pos = ++L.begin();
    cout << *pos << endl;

    // 在pos前插入值為4的元素
    L.insert(pos, 4);

    // 在pos前插入5個(gè)值為5的元素
    L.insert(pos, 5, 5);

    // 在pos前插入[v.begin(), v.end)區(qū)間中的元素
    vector<int> v{ 7, 8, 9 };
    L.insert(pos, v.begin(), v.end());

    // 刪除pos位置上的元素
    L.erase(pos);

    // 刪除list中[begin, end)區(qū)間中的元素,即刪除list中的所有元素
    L.erase(L.begin(), L.end());
}

// resize/swap/clear
void TestList3()
{
    // 用數(shù)組來(lái)構(gòu)造list
    int array1[] = { 1, 2, 3 };
    list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));

    // 交換l1和l2中的元素
    list<int> l2;
    l1.swap(l2);

    // 將l2中的元素清空
    l2.clear();
    cout << l2.size() << endl;
}

其他接口

二、與vector對(duì)比

vector list


結(jié)
構(gòu)
動(dòng)態(tài)順序表,一段連續(xù)空間 帶頭結(jié)點(diǎn)的雙向循環(huán)鏈表

機(jī)

問(wèn)
支持隨機(jī)訪問(wèn),訪問(wèn)某個(gè)元素效率O(1) 不支持隨機(jī)訪問(wèn),訪問(wèn)某個(gè)元素效率O(N)




任意位置插入和刪除效率低,需要搬移元素,時(shí)間復(fù)雜度為O(N),插入時(shí)有可能需要增容,增容:開(kāi)辟新空間,拷貝元素,釋放舊空間,導(dǎo)致效率更低 任意位置插入和刪除效率高,不需要搬移元素,時(shí)間復(fù)雜度為
O(1)




底層為連續(xù)空間,不容易造成內(nèi)存碎片,空間利用率高,緩存利用率高 底層節(jié)點(diǎn)動(dòng)態(tài)開(kāi)辟,小節(jié)點(diǎn)容易造成內(nèi)存碎片,空間利用率低,緩存利用率低


原生態(tài)指針 對(duì)原生態(tài)指針(節(jié)點(diǎn)指針)進(jìn)行封裝




在插入元素時(shí),要給所有的迭代器重新賦值,因?yàn)椴迦?br> 元素有可能會(huì)導(dǎo)致重新擴(kuò)容,致使原來(lái)迭代器失效,刪
除時(shí),當(dāng)前迭代器需要重新賦值否則會(huì)失效
插入元素不會(huì)導(dǎo)致迭代器失效,刪除元素時(shí),只會(huì)導(dǎo)致當(dāng)前迭代器失效,其他迭代器不受影響
使

場(chǎng)
需要高效存儲(chǔ),支持隨機(jī)訪問(wèn),不關(guān)心插入刪除效率 大量插入和刪除操作,不關(guān)心隨機(jī)訪問(wèn)

vector與list排序效率對(duì)比:

void test_op1()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	vector<int> v;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		v.push_back(e);
	}

	int begin1 = clock();
	// 
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

[STL-list]介紹、與vector的對(duì)比、模擬實(shí)現(xiàn)的迭代器問(wèn)題,C++,c++,開(kāi)發(fā)語(yǔ)言?

void test_op2()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();
	// vector

	vector<int> v(lt2.begin(), lt2.end());
	// 
	sort(v.begin(), v.end());

	// lt2
	lt2.assign(v.begin(), v.end());

	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

[STL-list]介紹、與vector的對(duì)比、模擬實(shí)現(xiàn)的迭代器問(wèn)題,C++,c++,開(kāi)發(fā)語(yǔ)言

可見(jiàn)list的排序效率是非常低的,甚至將list的數(shù)據(jù)導(dǎo)入vector中排完序在導(dǎo)回來(lái)的效率都比直接在list中排序的效率快,這是因?yàn)閘ist不支持下標(biāo)隨機(jī)訪問(wèn),只能依靠迭代器迭代到指定位置訪問(wèn),而排序過(guò)程中避免不了需要訪問(wèn)大量中間元素,所以list并不適合對(duì)數(shù)據(jù)進(jìn)行排序

三、list迭代器問(wèn)題

鏈表節(jié)點(diǎn)與鏈表結(jié)構(gòu)

節(jié)點(diǎn)包含三部分:前驅(qū)指針、后驅(qū)指針、數(shù)據(jù)。list封裝了頭節(jié)點(diǎn)的指針,可以根據(jù)該指針對(duì)后續(xù)節(jié)點(diǎn)進(jìn)行遍歷

    template<class T>
	struct ListNode
	{
		ListNode* _next;
		ListNode* _prev;
		T _data;
        //節(jié)點(diǎn)的構(gòu)造函數(shù)
		ListNode(const T& x = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_data(x)
		{}
	};

    template<class T>
	class list
	{
        void Empty_Init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		//構(gòu)造函數(shù)
		list()
		{
			Empty_Init();
		}

	private:
		Node* _head;
		size_t _size;
	};
如何設(shè)定list迭代器

????????在string與vector的模擬實(shí)現(xiàn)中,迭代器使用的都是原生指針T*,這是因?yàn)樵羔樋梢詽M足迭代器的要求,++可以指向下一個(gè)元素,解引用可以訪問(wèn)該元素,他們可以使用原生指針的根本原因是他們儲(chǔ)存數(shù)據(jù)的結(jié)構(gòu)都是連續(xù)的物理地址。

? ? ? ? 在list中原生指針Node*不能滿足我們的要求,因?yàn)閘ist的節(jié)點(diǎn)都是依靠指針連接起來(lái)的,其物理地址并不是連續(xù)的,++指向的并不是下一個(gè)元素,而是指向了跳過(guò)了一個(gè)Node的大小的的地址,并且迭代器希望解引用直接可以訪問(wèn)節(jié)點(diǎn)中的數(shù)據(jù),而*(Node*)卻是一個(gè)節(jié)點(diǎn)類型,所以在list中使用原生指針并不符合迭代器的要求。

? ? ? ? ?所以我們可以自己新建一個(gè)類,作為迭代器的類型,在其中封裝了頭節(jié)點(diǎn),就可以訪問(wèn)該鏈表了,并且我們?cè)谠擃愔锌梢酝ㄟ^(guò)運(yùn)算符重載改變++與解引用的行為,這樣就可以使用迭代器訪問(wèn)鏈表數(shù)據(jù)了

    template<class T>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T> Self;

		Node* _node;
		ListIterator(Node* _node)
			:_node(_node)
		{}

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

		//前置++
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
        //后置++
		Self operator++(int)
		{
			Self tmp(_node);
			_node = _node->_next;
			return tmp;
		}

	};
    
    template<class T>
	class list
	{
        void Empty_Init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
    
		//構(gòu)造函數(shù)
		list()
		{
			Empty_Init();
		}
        
        iterator begin()
		{
			//隱式類型轉(zhuǎn)換
			return _head->_next;
		}

		iterator end()
		{
			//隱式類型轉(zhuǎn)換
			return _head;
		}
	private:
		Node* _head;
		size_t _size;
	};
完善迭代器功能??
operator->的重載

????????但是上述代碼具有一定的局限性,例如當(dāng)T為一個(gè)結(jié)構(gòu)體A時(shí),*iterator返回的是結(jié)構(gòu)體A,想要訪問(wèn)結(jié)構(gòu)體中的數(shù)據(jù)還需要用 .?例如:*(it).a1,但是這樣寫(xiě)有點(diǎn)多次一舉,因?yàn)榈鱥t本身就是指向節(jié)點(diǎn)的指針,訪問(wèn)數(shù)據(jù)可以直接使用 ->,例如:it->a1,所以我們還需要將->重載一下

  T* operator->()
 {
    //返回?cái)?shù)據(jù)的地址
	return &_node->_data;
 }

結(jié)構(gòu)體A存儲(chǔ)在節(jié)點(diǎn)的_data中,這里返回了_data的地址,如果按照正常的思路進(jìn)行訪問(wèn),應(yīng)該按照如下的方式:it.operator->()->_a1?應(yīng)該是兩個(gè)箭頭,第一個(gè)箭頭代表運(yùn)算符的重載,第二個(gè)代表指針解引用訪問(wèn)數(shù)據(jù)。
但是編譯器為了方便查看會(huì)進(jìn)行優(yōu)化,將兩個(gè)箭頭變成了一個(gè)箭頭 it->_a1?,這樣直接可以訪問(wèn)

const迭代器

const的本質(zhì)就是為了禁止對(duì)成員進(jìn)行修改,所以我們只需要const迭代器只需要對(duì)非const迭代器稍加修改即可

    template<class T>
	struct ListConstIterator
	{
		typedef ListNode<T> Node;
		typedef ListConstIterator<T> Self;

		Node* _node;
		ListConstIterator(Node* _node)
			:_node(_node)
		{}

		const T& operator*()
		{
			return _node->_data;
		}

		const T* operator->()
		{
			return &_node->_data;
		}

	};

但是這樣const迭代器與非const迭代器這兩個(gè)類的重合度非常高,僅僅是函數(shù)返回值前是否加用const修飾的區(qū)別,所以我們可以利用模板文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-852450.html

    typedef ListIterator<T,T&,T*> iterator;
    typedef ListIterator<T,const T&,const T*> const_iterator;
    ---------------------------------
    template<class T,class Ref,class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;

		Node* _node;
		ListIterator(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(_node);
			_node = _node->_next;
			return tmp;
		}
	};
完整代碼:
#include<iostream>
using namespace std;
namespace zyq
{
	template<class T>
	struct ListNode
	{
		ListNode* _next;
		ListNode* _prev;
		T _data;

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

	//template<class T>
	//struct ListIterator
	//{
	//	typedef ListNode<T> Node;
	//	typedef ListIterator<T> Self;

	//	Node* _node;
	//	ListIterator(Node* _node)
	//		:_node(_node)
	//	{}

	//	T& operator*()
	//	{
	//		return _node->_data;
	//	}

	//     T* operator->()
	//	{
	//		return &_node->_data;
	//	}

	//	//前置++
	//	Self& operator++()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}

	//	Self operator++(int)
	//	{
	//		Self tmp(_node);
	//		_node = _node->_next;
	//		return tmp;
	//	}

	//	bool operator!=(const Self& it)
	//	{
	//		return !(_node == it._node);
	//	}

	//	bool operator==(const Self& it)
	//	{
	//		return _node == it._node;
	//	}

	//};

	//template<class T>
	//struct ListConstIterator
	//{
	//	typedef ListNode<T> Node;
	//	typedef ListConstIterator<T> Self;

	//	Node* _node;
	//	ListConstIterator(Node* _node)
	//		:_node(_node)
	//	{}

	//	const T& operator*()
	//	{
	//		return _node->_data;
	//	}

	//	const T* operator->()
	//	{
	//		return &_node->_data;
	//	}

	//	//前置++
	//	 Self& operator++()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}

	//	Self operator++(int)
	//	{
	//		Self tmp(_node);
	//		_node = _node->_next;
	//		return tmp;
	//	}

	//	bool operator!=(const Self& it)
	//	{
	//		return !(_node == it._node);
	//	}

	//	bool operator==(const Self& it)
	//	{
	//		return _node == it._node;
	//	}
	//};

	template<class T,class Ref,class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;

		Node* _node;
		ListIterator(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(_node);
			_node = _node->_next;
			return tmp;
		}

		bool operator!=(const Self& it)
		{
			return !(_node == it._node);
		}

		bool operator==(const Self& it)
		{
			return _node == it._node;
		}

	};
	template<class T>
	class list
	{
	public:
		typedef ListNode<T> Node;
		//typedef ListIterator<T> iterator;
		//typedef ListConstIterator<T> const_iterator;
		typedef ListIterator<T,T&,T*> iterator;
		typedef ListIterator<T,const T&,const T*> const_iterator;

		iterator begin()
		{
			//隱式類型轉(zhuǎn)換
			return _head->_next;
		}

		iterator end()
		{
			//隱式類型轉(zhuǎn)換
			return _head;
		}

		const_iterator begin() const
		{
			//隱式類型轉(zhuǎn)換
			return _head->_next;
		}

		const_iterator end() const
		{
			//隱式類型轉(zhuǎn)換
			return _head;
		}

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

		//構(gòu)造函數(shù)
		list()
		{
			Empty_Init();
		}

		//拷貝構(gòu)造
		list(const list<T>& lt)
		{
			Empty_Init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		void swap(list<T> lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		//賦值運(yùn)算符重載
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		~list()
		{
			iterator it = begin();
			while (it != end())
			{
				it=erase(it);
			}
			delete _head;
			_head = nullptr;
		}

		size_t size()
		{
			return _size;
		}

		/*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;
			_size++;
		}*/

		void push_back(const T& x)
		{
			insert(end()  , x);
		}

		void  push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		void insert(iterator pos, const T& x)
		{
			Node* newnode = new Node(x);
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
		}

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

	private:
		Node* _head;
		size_t _size;
	};

	template<class T>
	void PrintList(const list<T>& clt)
	{
		typename list<T>::const_iterator it = clt.begin();
		while (it != clt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void testlist1()
	{
		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();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
		/*lt.push_back(9);
		lt.pop_front();*/
		PrintList(lt);
		list<int> lt1(lt);
		PrintList(lt1);
		list<int> lt2;
		lt2 = lt;
		PrintList(lt2);
	}

	struct A
	{
		int _a1;
		int _a2;
		A(int a1 = 0, int a2 = 0)
			:_a1(a1)
			, _a2(a2)
		{}
	};

	void testlist2()
	{
		list<A> lt;
		lt.push_back({ 1,2 });
		lt.push_back(A(1,2));
		list<A>::iterator it = lt.begin();
		while (it != lt.end())
		{
			//cout << (*it)._a1 << " " << (*it)._a2 << " ";
			cout << it->_a1 << " " << it->_a2 << " ";
			it++;
		}
		cout << endl;
	}
}

到了這里,關(guān)于[STL-list]介紹、與vector的對(duì)比、模擬實(shí)現(xiàn)的迭代器問(wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • STL之vector容器的介紹與模擬實(shí)現(xiàn)

    STL之vector容器的介紹與模擬實(shí)現(xiàn)

    所屬專欄:C“嘎嘎\\\" 系統(tǒng)學(xué)習(xí)?? ?? 博主首頁(yè):初陽(yáng)785?? ?? 代碼托管:chuyang785?? ?? 感謝大家的支持,您的點(diǎn)贊和關(guān)注是對(duì)我最大的支持?。?!?? ?? 博主也會(huì)更加的努力,創(chuàng)作出更優(yōu)質(zhì)的博文!!?? vector的文檔介紹 vector是表示可變大小數(shù)組的序列容器。 就像

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

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

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

    2024年02月14日
    瀏覽(27)
  • 【C++】STL反向迭代器模擬實(shí)現(xiàn),迭代器適配器,迭代器類型簡(jiǎn)單介紹

    【C++】STL反向迭代器模擬實(shí)現(xiàn),迭代器適配器,迭代器類型簡(jiǎn)單介紹

    本篇主要講反向迭代器的模擬實(shí)現(xiàn)。 能夠加深各位對(duì)泛型的理解。 前面我那篇string介紹里面已經(jīng)提到過(guò)反向迭代器是啥了,如果點(diǎn)進(jìn)來(lái)的同學(xué)還不知道,可以看看:[string介紹](https://blog.csdn.net/m0_62782700/article/details/130796914? spm=1001.2014.3001.5501) 迭代器,可以在不暴露底層實(shí)現(xiàn)細(xì)

    2024年02月16日
    瀏覽(31)
  • 【C++庖丁解?!縎TL之vector容器的介紹及使用 | vector迭代器的使用 | vector空間增長(zhǎng)問(wèn)題

    【C++庖丁解牛】STL之vector容器的介紹及使用 | vector迭代器的使用 | vector空間增長(zhǎng)問(wèn)題

    ??你好,我是 RO-BERRY ?? 致力于C、C++、數(shù)據(jù)結(jié)構(gòu)、TCP/IP、數(shù)據(jù)庫(kù)等等一系列知識(shí) ??感謝你的陪伴與支持 ,故事既有了開(kāi)頭,就要畫(huà)上一個(gè)完美的句號(hào),讓我們一起加油 vector的文檔介紹 vector是表示可變大小數(shù)組的序列容器。 就像數(shù)組一樣,vector也采用的連續(xù)存儲(chǔ)空間來(lái)存

    2024年03月14日
    瀏覽(36)
  • STL常用梳理——VECTOR常用接口及其迭代器實(shí)現(xiàn)

    STL常用梳理——VECTOR常用接口及其迭代器實(shí)現(xiàn)

    vector是STL中容器之一,特性如下: vector是表示可變大小數(shù)組的序列容器。 就像數(shù)組一樣,vector也采用的連續(xù)存儲(chǔ)空間來(lái)存儲(chǔ)元素。也就是意味著可以采用下標(biāo)對(duì)vector的元素 進(jìn)行訪問(wèn),和數(shù)組一樣高效。但是又不像數(shù)組,它的大小是可以動(dòng)態(tài)改變的,而且它的大小會(huì)被容器自

    2024年02月05日
    瀏覽(29)
  • 【C++】vector模擬實(shí)現(xiàn)+迭代器失效

    【C++】vector模擬實(shí)現(xiàn)+迭代器失效

    鐵汁們,今天給大家分享一篇vector模擬實(shí)現(xiàn) + 迭代器失效,來(lái)吧,開(kāi)造?? 指向最后一個(gè)空間的下一個(gè)位置 ?? iterator _endofstorage 指向存儲(chǔ)第一個(gè)有效數(shù)據(jù)空間的位置 ?? iterator _start 指向存儲(chǔ)最后一個(gè)有效數(shù)據(jù)空間的下一個(gè)位置 ?? iterator _finish 在成員變量聲明處給缺省值,

    2024年02月21日
    瀏覽(86)
  • 【STL】:vector的模擬實(shí)現(xiàn)

    【STL】:vector的模擬實(shí)現(xiàn)

    朋友們、伙計(jì)們,我們又見(jiàn)面了,本期來(lái)給大家解讀一下有關(guān)vector的模擬實(shí)現(xiàn),如果看完之后對(duì)你有一定的啟發(fā),那么請(qǐng)留下你的三連,祝大家心想事成! C 語(yǔ) 言 專 欄: C語(yǔ)言:從入門(mén)到精通 數(shù)據(jù)結(jié)構(gòu)專欄: 數(shù)據(jù)結(jié)構(gòu) 個(gè)? 人? 主? 頁(yè)?: stackY、 C + + 專 欄? ?: C++ Linux 專

    2024年02月06日
    瀏覽(22)
  • 【STL】vector的模擬實(shí)現(xiàn)

    【STL】vector的模擬實(shí)現(xiàn)

    目錄 前言 結(jié)構(gòu)解析 構(gòu)造析構(gòu) 構(gòu)造 默認(rèn)構(gòu)造 初始化成 n 個(gè) val? 以迭代器區(qū)間構(gòu)造 拷貝構(gòu)造 析構(gòu) 運(yùn)算符重載 賦值重載 下標(biāo)訪問(wèn) 迭代器 const迭代器 容量操作 查看大小和容量 容量修改 數(shù)據(jù)修改 尾插尾刪 指定位置插入和刪除 insert erase 清空 判空 交換 源碼 從vector開(kāi)始就要開(kāi)

    2024年02月06日
    瀏覽(25)
  • [STL] vector 模擬實(shí)現(xiàn)詳解

    [STL] vector 模擬實(shí)現(xiàn)詳解

    目錄 一,準(zhǔn)備工作 二,push_back?? 1, 關(guān)于引用 2. 參數(shù)const 的修飾 ?補(bǔ)充 三,迭代器實(shí)現(xiàn) 四,Pop_back 五,insert 1. 補(bǔ)充——迭代器失效 六, erase 七,構(gòu)造函數(shù)? 1. 迭代器構(gòu)造? 2. 其他構(gòu)造 3. 拷貝構(gòu)造? 1) 傳統(tǒng)寫(xiě)法 2)現(xiàn)代寫(xiě)法(提高函數(shù)復(fù)用性)? 八,賦值符號(hào)重載 九,

    2024年02月16日
    瀏覽(30)
  • 【STL】模擬實(shí)現(xiàn)vector(詳解)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包