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

相关推荐
穗余2 小时前
Rust——impl是什么意思
开发语言·后端·rust
程序员大辉2 小时前
开源LibreOffice(Office办公套件)下载完整安装教程
开发语言·microsoft·c#
yngsqq2 小时前
运行c#脚本
开发语言·数据库·c#
代码羊羊2 小时前
Rust模式匹配
开发语言·后端·rust
Wild_Pointer.2 小时前
项目实战:编写CMakeLists管理Qt+OpenCV项目
开发语言·c++·qt
莫逸风2 小时前
【java-core-collections】集合框架深度解析
java·开发语言
geovindu2 小时前
go: Bridge Pattern
开发语言·设计模式·golang·软件构建·桥接模式
Fate_I_C2 小时前
Kotlin 为什么是 Android 开发的首选语言
android·开发语言·kotlin