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的对比

相关推荐
Dr.kangder36 分钟前
嵌入式总线设备解析——TTE总线应用与实践
开发语言·网络·算法·嵌入式·多任务·同步机制
-银雾鸢尾-37 分钟前
C#中的多线程
开发语言·c#
yyy(十一月限定版)44 分钟前
016【入门】双端队列
数据结构·c++
2401_858286111 小时前
OS82.【Linux】设计线程池
java·linux·运维·服务器·开发语言·算法·线程池
数据知道1 小时前
IDOR 不安全的直接对象引用:API 越权实战检测
java·开发语言·网络·安全·网络安全
c238561 小时前
# Mini OJ — 项目详解文档
开发语言·数据库·c++
2401_894915532 小时前
Geo 优化源码部署常见报错排查:连接失败、地图加载、地域词失效解决方案
服务器·开发语言·python·安全·php
c238562 小时前
C/C++每日一练20
c语言·开发语言·c++
可爱系程序猿2 小时前
msvcp140_codecvt_ids.dll 缺失的排查记录:C++ 程序启动异常时如何核对运行库版本
开发语言·c++·程序人生·电脑
weixin_440784112 小时前
【IntentSeivice实现原理】
android·java·开发语言·intentservice