C++ --list接口使用及实现

1.list 接口使用

1.1构造

1.2iterator的使用

可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。

1.3 capacity

1.4 element access

1.5 modifiers

2.list的迭代器失效

迭代器失效即迭代器所指向的节点的无 效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入 时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭 代器,其他迭代器不会受到影响。

3.list模拟实现

cpp 复制代码
#pragma once
namespace my_list
{
	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)
		{
		}
	};


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


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

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

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




	//template<class T>    //使用三个模板参数就就可以不再另开一个函数
	//struct _list_const_iterator
	//{
	//	typedef list_node<T> Node;
	//	Node* _node;
	//
	//	_list_const_iterator(Node* node)
	//		:_node(node)
	//	{
	//	}
	//
	//	const T& operator*()
	//	{
	//		return _node->_data;
	//	}
	//
	//	_list_const_iterator<T>& operator++()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}
	//	_list_const_iterator<T> operator++(int)
	//	{
	//		_list_const_iterator<T> tmp(*this);
	//		_node = _node->_next;
	//		return tmp;
	//	}
	//	_list_const_iterator<T>& operator--()
	//	{
	//		_node = _node->_prev;
	//		return *this;
	//	}
	//	_list_const_iterator<T> operator--(int)
	//	{
	//		_list_const_iterator<T> tmp(*this);
	//		_node = _node->_prev;
	//		return tmp;
	//	}
	//
	//	bool  operator!=(const _list_const_iterator<T>& it) const
	//	{
	//		return _node != it._node;
	//	}
	//	bool  operator==(const _list_const_iterator<T>& it) const
	//	{
	//		return _node == it._node;
	//	}
	//};


	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;
		// 不能实现,因为const迭代器本身,迭代器无法++
		// typedef const __list_iterator<T> const_iterator;

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

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

		list()
		{
			empty_init();
		}

		list(const list<T>& lt)
		{
			empty_init();
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}

		list(std::initializer_list<T> il)
		{
			empty_init();
			for (const auto& e : il)
			{
				push_back(e);
			}
		}

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

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

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

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

		iterator insert(iterator pos, const T& val)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(val);
			Node* prev = cur->_prev;
			prev->_next = newnode;
			newnode->_next = cur;
			newnode->_prev = prev;
			cur->_prev = newnode;
			++_size;
			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			--_size;
			return next;
		}

		size_t size() const
		{
			return _size;
		}
		private:
			Node* _head;
		size_t _size = 0;
	};
}

打印list:

cpp 复制代码
template<class T>
void print(const list<T>& lt)
{
	// 类模板未实例化,不能去类模板中找后面的东西
		// 编译器就分不清const_iterator是嵌套内类,还是静态成员变量
		// typename告诉编译器,我确认过了这里是类型
		//typename list<T>::const_iterator it = lt.begin();
	auto it = lt.begin();//使用auto就可以避免上面问题
	while (it != lt.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

4.list与vector的对比

相关推荐
辞旧 lekkk8 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode8 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19979 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk9 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite10 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
王老师青少年编程11 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
Cyber4K11 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
初願致夕霞11 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
Le_ee11 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
yong999012 小时前
MATLAB读取高光谱图像
开发语言·matlab