C++——模拟实现list

news2024/10/7 6:09:46

1.初步实现结点和链表

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

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

	template<class T>
	class list//list的框架本质是封装+运算符重载
	{
		typedef list_node<T> Node;

	public:

    	void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_init();
		}

	private:
		Node* _head;
		size_t _size;
//不写_size变量的话,想获取size的大小,从头遍历链表,时间复杂度是O(N)
	};
}

2.push_back

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

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

	template<class T>
	class list//list的框架本质是封装+运算符重载
	{
		typedef list_node<T> Node;

	public:

    	void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

		void push_back(const T& x)
		{
			Node* tail = _head->_prev;
			Node* newnode = new Node(x);//调用构造函数

			tail->_next = newnode;
			newnode->_prev = tail;

			newnode->_next = _head;
			_head->_prev = newnode;
		}

	private:
		Node* _head;
		size_t _size;
	};
}

3.初步实现迭代器

将迭代器封装为类来实现其功能

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

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

	template<class T>
	struct __list_iterator//一般前加__就说明这是内部的实现
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		self& operator++()//不是连续的物理空间,++不能到下一个位置
		{
			_node = _node->_next;
			return *this;
		}

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

		bool operator!=(const self& s)
		{
			return (_node != s._node);
		}
	};

	template<class T>
	class list//list的框架本质是封装+运算符重载
	{
		typedef list_node<T> Node;

	public:
		typedef __list_iterator<T> iterator;
		iterator begin()
		{
			//return iterator(_head->_next);
			return _head->_next;
		}

		iterator end()
		{
			//return iterator(_head);
			return _head;
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_init();
		}

		void push_back(const T& x)
		{
			Node* tail = _head->_prev;
			Node* newnode = new Node(x);//调用构造函数

			tail->_next = newnode;
			newnode->_prev = tail;

			newnode->_next = _head;
			_head->_prev = newnode;
		}

	private:
		Node* _head;
		size_t _size;
	};

	void test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		// 封装,屏蔽了底层差异和实现细节
		// 提供了统一的访问、修改和遍历方式
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			*it += 10;//还可以修改

			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}

4.插入和删除

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

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

	template<class T>
	struct __list_iterator//一般前加__就说明这是内部的实现
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		self& operator++()//不是连续的物理空间,++不能到下一个位置
		{
			_node = _node->_next;
			return *this;
		}

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

		bool operator!=(const self& s)
		{
			return (_node != s._node);
		}
	};

	template<class T>
	class list//list的框架本质是封装+运算符重载
	{
		typedef list_node<T> Node;

	public:
		typedef __list_iterator<T> iterator;

		void clear()//不清哨兵位的头结点
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		void push_back(const T& x)
		{
			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* newnode = new Node(x);

		//	Node* prev = cur->_prev;

		//	//prev  newnode  cur
		//	prev->_next = newnode;
		//	newnode->_prev = prev;

		//	newnode->_next = cur;
		//	cur->_prev = newnode;

		//	//理论上可以认为list的迭代器不存在失效问题
		//}

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

			Node* prev = cur->_prev;

			//prev  newnode  cur
			prev->_next = newnode;
			newnode->_prev = prev;

			newnode->_next = cur;
			cur->_prev = newnode;

			++_size;

			return iterator(newnode);//指向新插入的元素
		}

		//void erase(iterator pos)
		//{
		//	Node* cur = pos._node;

		//	Node* prev = cur->_prev;
		//	Node* next = cur->_next;

		//	delete cur;
		//	prev->_next = next;
		//	next->_prev = prev;

		//}

		iterator erase(iterator pos)
		{
			Node* cur = pos._node;

			Node* prev = cur->_prev;
			Node* next = cur->_next;

			delete cur;
			prev->_next = next;
			next->_prev = prev;

			--_size;

			//erase后的迭代器会失效,所有最好把返回值类型改为iterator
			return iterator(next);
		}

		size_t size()
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

}

5.拷贝构造和赋值

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

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

	template<class T>
	struct __list_iterator//一般前加__就说明这是内部的实现
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		self& operator++()//不是连续的物理空间,++不能到下一个位置
		{
			_node = _node->_next;
			return *this;
		}

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

		bool operator!=(const self& s)
		{
			return (_node != s._node);
		}
	};

	template<class T>
	class list//list的框架本质是封装+运算符重载
	{
		typedef list_node<T> Node;

	public:
		typedef __list_iterator<T> iterator;

		iterator begin()
		{
			//return iterator(_head->_next);
			return _head->_next;
		}

		iterator end()
		{
			//return iterator(_head);
			return _head;
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_init();
		}

		~list()
		{
			clear();

			delete(_head);
			_head = nullptr;
		}

		//拷贝构造
		//list(const list<T>& lt)//这里是const迭代器
		list(list<T>& lt)
		{
			empty_init();
			for (auto e : lt)
			{
				push_back(e);
			}
		}

		传统写法
		//list<int>& operator=(const list<int>& lt)
		//{
		//	if (this != &lt)
		//	{
		//		clear();
		//		for (auto e : lt)
		//		{
		//			push_back(e);
		//		}
		//	}
		//	return *this;
		//}

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

		list<int>& operator=(list<int> lt)//调用拷贝构造
		{
			swap(lt);
			return *this;
		}

	private:
		Node* _head;
		size_t _size;
	};

}

6.完善迭代器

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

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

	template<class T>
	struct __list_iterator//一般前加__就说明这是内部的实现
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		self& operator++()//不是连续的物理空间,++不能到下一个位置
		{
			_node = _node->_next;
			return *this;
		}

		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		//自定义类型尽量使用前置++,后置++要返回++之前的值
		self operator++(int)
		{
			self tmp(*this);

			_node = _node->_next;
			return tmp;
		}

		self operator--(int)
		{
			self tmp(*this);

			_node = _node->_prev;
			return tmp;
		}

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

		bool operator!=(const self& s)
		{
			return (_node != s._node);
		}

		bool operator==(const self& s)
		{
			return (_node == s._node);
		}

		//迭代器不需要析构函数
		//它虽然有结点的指针,但是指针是不属于它的,它不能释放

		//迭代器的拷贝构造和赋值也不需要去实现深拷贝
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;

	public:

	private:
		Node* _head;
		size_t _size;
	};

}

7.补充:迭代器的->运算符重载

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

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

	template<class T>
	struct __list_iterator//一般前加__就说明这是内部的实现
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		self& operator++()//不是连续的物理空间,++不能到下一个位置
		{
			_node = _node->_next;
			return *this;
		}

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

		bool operator!=(const self& s)
		{
			return (_node != s._node);
		}

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

	template<class T>
	class list
	{
		typedef list_node<T> Node;

	public:

	private:
		Node* _head;
		size_t _size;
	};

	void test_list3()
	{//存一个自定义类型
		list<AA> lt;
		lt.push_back(AA(1, 2));
		lt.push_back(AA(2, 3));
		lt.push_back(AA(3, 4));

		list<AA>::iterator it = lt.begin();
		while (it != lt.end())
		{
			//cout << *it << " ";//*it的类型是AA,不支持流插入

			//一、让AA类支持流插入

			//二、打印公有成员变量
			//cout << (*it)._a1 << " " << (*it)._a2 << endl;

			//三、让AA类支持->
		    //自定义类型不支持运算符,需要重载
			cout << it->_a1 << " " << it->_a2 << endl;
			cout << it.operator->()->_a1 << " " << it.operator->()->_a2 << endl;

			++it;

			//当数据存储自定义类型时,重载->
		}

		//解引用有三种方式:*、[]、->
		int* p = new int;
		*p = 1;

		AA* ptr = new AA;
		ptr->_a1=1;
	}

}

8.const迭代器

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

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


    //const迭代器模拟的是指向的内容不能改变,而不是本身不能改变
    //const迭代器本身可以++
	template<class T>
	struct __list_const_iterator//const迭代器是一个单独的类
	{
		typedef list_node<T> Node;
		typedef __list_const_iterator<T> self;
		Node* _node;

		__list_const_iterator(Node* node)
			:_node(node)
		{}

		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);

			_node = _node->_next;
			return tmp;
		}

		self operator--(int)
		{
			self tmp(*this);

			_node = _node->_prev;
			return tmp;
		}

		const T& operator*() //通过解引用才能修改数据
		{
			return _node->_data;//返回的是const别名,不能修改
		}

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

		bool operator!=(const self& s)
		{
			return (_node != s._node);
		}

		bool operator==(const self& s)
		{
			return (_node == s._node);
		}
	};

	template<class T>
	class list//list的框架本质是封装+运算符重载
	{
		typedef list_node<T> Node;

	public:
		typedef __list_iterator<T> iterator;
		typedef __list_const_iterator<T> const_iterator;
		//单独去支持一个类型:const_iterator

		const_iterator begin() const
		{
			//return const_iterator(_head->_next);
			return _head->_next;
		}

		const_iterator end() const
		{
			//return const_iterator(_head);
			return _head;
		}

	private:
		Node* _head;
		size_t _size;
	};

    //测试const迭代器
	void print_list(const list<int>& lt)
//容器通常不支持流插入,所以要写个print函数
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//*it = 100;//不可修改

			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void test_list4()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		print_list(lt);
	}
}

9.改善为模板

当前代码的实现太过冗余,iterator类和const_iterator类的代码实现高度重合

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

		list_node(const T& x = T())
			:_data(x)//这里是拷贝构造
			,_prev(nullptr)
			,_next(nullptr)
		{}
	};


//T  T&  T*
//T  const T&  const T*
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)
	{}

	self& operator++()//不是连续的物理空间,++不能到下一个位置
	{
		_node = _node->_next;
		return *this;
	}

	self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}


	self operator++(int)
	{
		self tmp(*this);

		_node = _node->_next;
		return tmp;
	}

	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);
	}

	//所以迭代器到底如何实现,是根据容器的底层结构来决定的
};


template<class T>
class list//list的框架本质是封装+运算符重载
{
	typedef list_node<T> Node;

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

	const_iterator begin() const
	{
		//return const_iterator(_head->_next);
		return _head->_next;
	}

	const_iterator end() const
	{
		//return const_iterator(_head);
		return _head;
	}


	iterator begin()
	{
		//return iterator(_head->_next);
		return _head->_next;
	}

	iterator end()
	{
		//return iterator(_head);
		return _head;
	}

	void empty_init()
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;

		_size = 0;
	}

	list()
	{
		empty_init();
	}

	~list()
	{
		clear();

		delete(_head);
		_head = nullptr;
	}

	//拷贝构造
	list(const list<T>& lt)//这里是const迭代器
	{
		empty_init();
		for (auto e : lt)
		{
			push_back(e);
		}
	}


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

	list<int>& operator=(list<int> lt)//调用拷贝构造
	{
		swap(lt);
		return *this;
	}

	void clear()//不清哨兵位的头结点
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}

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

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

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

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

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

		Node* prev = cur->_prev;

		//prev  newnode  cur
		prev->_next = newnode;
		newnode->_prev = prev;

		newnode->_next = cur;
		cur->_prev = newnode;

		++_size;

		return iterator(newnode);//指向新插入的元素
	}


	iterator erase(iterator pos)
	{
		Node* cur = pos._node;

		Node* prev = cur->_prev;
		Node* next = cur->_next;

		delete cur;
		prev->_next = next;
		next->_prev = prev;

		--_size;

		//erase后的迭代器会失效,所有最好把返回值类型改为iterator
		return iterator(next);
	}

	size_t size()
	{
		return _size;
	}

private:
	Node* _head;
	size_t _size;
};

    //测试const迭代器
	void print_list(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//*it = 100;//不可修改

			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list4()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		print_list(lt);
	}
}

10.关于typename

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

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

	template<class T>
	void print_list(const list<T>& lt)//之前的print只支持int类型
	{
		list<T>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list4()
	{
		list<string> lt1;
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");

		//这里会不会涉及深层次的深拷贝问题?
		//之前的vector容器就涉及到了深层次的浅拷贝问题
		//它的主要原因是因为扩容时使用了memcpy
		//list不存在扩容的事情,也就不涉及这个问题

		print_list(lt1);

	}
}

但事实上,这样修改以后是编译不通过的。

 

这就涉及到另一个知识点,在日常使用中,模板命名时,使用class和使用typename是等价的

template<class T>	
template<typename T>

但此时的场景下,就要使用typename了。

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

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


	template<typename T>
	void print_list(const list<T>& lt)//之前的print只支持int类型
	{
		typename list<T>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list4()
	{

		list<string> lt1;
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");

		print_list(lt1);

	}
}
typename list<T>::const_iterator it = lt.begin();
当模板是未实例化的类模板时,如list<T>
编译器不能去它里面查找,它里面带有许多不可确定的东西
编译器在编译阶段、没有实例化时,只会对这里进行初步的检查
所以编译器无法判断list<T>::const_iterator是内嵌类型,还是静态成员变量
所以这里要加上typename,凡是这种类模板中取类型都要加
前面加一个typename就是告诉编译器,这里是一个类型
要等list<T>实例化了,再去类里面去取
模板里面取东西都要加typename,使得编译器初步检查时先通过
然后实例化后,再去类中取这个类型,就知道它具体是什么了

11.再次改善为模板

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

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


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

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list4()
	{

		list<string> lt1;
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");

		print_list(lt1);


		vector<string> v;
		v.push_back("111111111111");
		v.push_back("111111111111");
		v.push_back("111111111111");
		v.push_back("111111111111");
		v.push_back("111111111111");

		print_list(v);//此时是打印不了的
	}
}

print_list是专门打印list的,想要打印vector,难道再去写一个逻辑高度重复的print函数吗?

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

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


	template<typename Container>
	void print_container(const Container& con)//针对容器的打印
	{
		typename Container::const_iterator it = con.begin();
		while (it != con.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}
	//模板实现了泛型编程,本质就是本来应该由我们做的事情交给了编译器

	void test_list4()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		print_container(lt);

		list<string> lt1;
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");
		lt1.push_back("1111111111111111111111");

		print_container(lt1);

		vector<string> v;
		v.push_back("111111111111");
		v.push_back("111111111111");
		v.push_back("111111111111");
		v.push_back("111111111111");
		v.push_back("111111111111");

		print_container(v);

	}
}

12.总结:对比vector和list

vector与list都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不同
vectorlist
底层结构
动态顺序表,一段连续空间
带头结点的双向循环链表
随机访问
支持随机访问,访问某个元素效率O(1),适合sort数据
不支持随机访问,访问某个元素效率O(N)
插入和删除
任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低
任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1)
空间利用率
底层为连续空间,不容易造成内存碎片,空间利用率高,CPU高速缓存利用率高
底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低
迭代器原生态指针
对原生态指针(节点指针)进行封装
迭代器失效
在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效
插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响
使用场景
需要高效存储,支持随机访问,不关心插入删除效率
大量插入和删除操作,不关心随
机访问

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2193645.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C# 雷赛运动控制器 SMC304 新建工程

雷赛SMC304资料下载地址 https://www.leisai.com/cn/cpzx/info_36_itemid_3389_lcids_140_cid_3340.html 在官网下载需要的资料 新建文件 在官网下载的资料中找出需要三个文件 把文件添加到现有的项目中 编译选择x64 将连接雷赛电脑的网口IP号改为&#xff1a;如下图所示

深度学习环境安装

**前置知识&#xff1a; 1、各个软件之间的关系&#xff1a; pytorch/tensorflow库&#xff0c;调用cuda runtime version&#xff0c;接着cuda调用驱动&#xff08;cuda driver version&#xff09;&#xff0c;最后驱动又调用GPU显卡。 Anaconda&#xff1a; 集成了python&…

华为海思:大小海思的双轮驱动战略分析

华为海思,作为华为旗下的半导体设计部门,近年来在芯片设计领域取得了显著成就,成为了中国乃至全球芯片设计的重要力量。实际上,华为海思并非单一实体,而是由两个主要分支构成:大海思和小海思。这两个分支虽然同属华为海思,但在定位、产品布局以及市场策略上有所不同,共…

YOLOv8改进 - 注意力篇 - 引入EMA注意力机制

一、本文介绍 作为入门性篇章&#xff0c;这里介绍了EMA注意力在YOLOv8中的使用。包含EMA原理分析&#xff0c;EMA的代码、EMA的使用方法、以及添加以后的yaml文件及运行记录。 二、EMA原理分析 EMA官方论文地址&#xff1a;EMA文章 EMA代码&#xff1a;EMA代码 EMA注意力机…

Oracle中MONTHS_BETWEEN()函数详解

文章目录 前言一、MONTHS_BETWEEN()的语法二、主要用途三、测试用例总结 前言 在Oracle数据库中&#xff0c;MONTHS_BETWEEN()函数可以用来计算两个日期之间的月份差。它返回一个浮点数&#xff0c;表示两个日期之间的整月数。 一、MONTHS_BETWEEN()的语法 MONTHS_BETWEEN(dat…

毕业设计项目 基于大数据人才岗位数据分析

文章目录 1 前言1. 数据集说明2. 数据处理2.1 数据清洗2.2 数据导入 3. 数据分析可视化3.1 整体情况&#xff08;招聘企业数、岗位数、招聘人数、平均工资&#xff09;3.2 企业主题行业情况公司类型最缺人的公司 TOP平均薪资最高的公司 TOP工作时间工作地点福利词云 3.3 岗位主…

晶体管最佳效率区域随频率逆时针旋转原因分析

晶体管最佳效率区域随频率逆时针旋转原因分析 在功率放大器的设计时&#xff0c;晶体管最佳区域随频率逆时针旋转。但是&#xff0c;对于一般的微带电路&#xff0c;匹配阻抗区域是随着频率顺时针旋转的&#xff08;也有称这个特性是Foster特性&#xff09;&#xff0c;因此功…

运动员场景分割系统源码&数据集分享

运动员场景分割系统源码&#xff06;数据集分享 [yolov8-seg-HGNetV2&#xff06;yolov8-seg-aux等50全套改进创新点发刊_一键训练教程_Web前端展示] 1.研究背景与意义 项目参考ILSVRC ImageNet Large Scale Visual Recognition Challenge 项目来源AAAI Global Al lnnovati…

基于SpringBoot+Vue+MySQL的在线学习交流平台

系统展示 用户前台界面 管理员后台界面 系统背景 随着互联网技术的飞速发展&#xff0c;在线学习已成为现代教育的重要组成部分。传统的面对面教学方式已无法满足广大学习者的需求&#xff0c;特别是在时间、地点上受限的学习者。因此&#xff0c;构建一个基于SpringBoot、Vue.…

CPU 多级缓存

在多线程并发场景下&#xff0c;普通的累加很可能错的 CPU 多级缓存 Main Memory : 主存Cache : 高速缓存&#xff0c;数据的读取存储都经过此高速缓存CPU Core : CPU 核心Bus : 系统总线 CPU Core 和 Cache 通过快速通道连接&#xff0c;Main menory 和 Cache 都挂载到 Bus 上…

gm/ID设计方法学习笔记(二)

一、任务 设计一个二级运放&#xff0c;第一级为有源负载差动对&#xff08;五管OTA&#xff09;&#xff0c;第二级为电流源负载的共源极。 二、参数指标 GBW≥50MHz|Av|≥80dBPM60~70SR≥50V/us10pF 本文使用smic13mmrf_1233工艺库进行设计。 三、电路设计 &#xff08;…

【进阶OpenCV】 (6)--指纹识别

文章目录 指纹识别1. 计算指纹间匹配点的个数2. 获取指纹编号3. 获取对应姓名4. 代码实现 总结 指纹识别 假设&#xff0c;现在我们有一个小的指纹库&#xff0c;此时&#xff0c;有一个指纹图片需要我们识别是不是指纹库中某一个人的。如果是&#xff0c;是谁的呢&#xff1f…

力扣110:判断二叉树是否为平衡二叉树

利用二叉树遍历的思想编写一个判断二叉树&#xff0c;是否为平衡二叉树 示例 &#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;true思想&#xff1a; 代码&#xff1a; int getDepth(struct TreeNode* node) {//如果结点不存在&#xff0c;返回…

有趣幽默彩虹屁文案生成工具微信小程序源码

有趣幽默彩虹屁文案生成工具小程序源码 此文案小程序主要功能为分享各种有趣幽默的文案 免服务器免域名&#xff0c;源码只提供彩虹屁&#xff0c;朋友圈&#xff0c;毒鸡汤API接口&#xff0c;其他需自行查找替代 小程序拥有复制收藏功能&#xff0c;可自行体验&#xff0c;设…

FineReport 11 在线学习

文章目录 学习路线图FineReport 11 在线学习资源链接分享帆软report 特点 学习路线图 学习生态 自测题 FineReport 11 在线学习资源链接分享 帮助中心https://help.fanruan.com/finereport/ FineReport 入门学习路径https://edu.fanruan.com/guide/finereport 普通报表…

【c++】string类 (一)

简介 由于c的历史包袱&#xff0c;c要兼容c语言&#xff0c;c的字符串要兼容c语言&#xff0c;在 C 中&#xff0c;字符串通常使用两种主要的方式来表示&#xff1a; C风格字符串&#xff08;C-style strings&#xff09;&#xff1a; 依然是以 \0 结尾的字符数组。这种表示方…

【Java 并发编程】初识多线程

前言 到目前为止&#xff0c;我们学到的都是有关 “顺序” 编程的知识&#xff0c;即程序中所有事物在任意时刻都只能执行一个步骤。例如&#xff1a;在我们的 main 方法中&#xff0c;都是多个操作以 “从上至下” 的顺序调用方法以至结束的。 虽然 “顺序” 编程能够解决相当…

YOLO11改进|卷积篇|引入SPDConv

目录 一、【SPD】卷积1.1【SPD】卷积介绍1.2【SPD】核心代码 二、添加【SPD】卷积2.1STEP12.2STEP22.3STEP32.4STEP4 三、yaml文件与运行3.1yaml文件3.2运行成功截图 一、【SPD】卷积 1.1【SPD】卷积介绍 SPD-Conv卷积的结构图如下&#xff0c;下面我们简单分析一下其处理过程…

贪心算法.

序幕 贪心算法&#xff08;Greedy Algorithm&#xff09;是一种在求解问题时采取逐步构建解决方案的策略&#xff0c;每一步都选择当前状态下局部最优的解&#xff0c;期望通过局部最优解能够得到全局最优解。 以上为了严谨性&#xff0c;引用了官方用语。 而用大白话总结就是&…

如何移除 iPhone 上的网络锁?本文筛选了一些适合您的工具

您是否对 iPhone 运营商的网络感到困惑&#xff1f;不用担心&#xff0c;我们将向您介绍 8 大免费 iPhone 解锁服务。这些工具可以帮助您移除 iPhone 上的网络锁&#xff0c;并使您能够永久在网络上使用您的设备。如果您想免费解锁 iPhone&#xff0c;请阅读本文并找到最适合您…