STL篇:list的用法注意事项以及模拟实现

本文详细介绍了C++STL中的list容器的使用方法,包括其特点(如带头双向循环链表),迭代器的性质(forward_iterator、bidirectional_iterator和random_access_iterator),以及模拟实现和常见操作如插入、删除、迭代器处理等。
摘要由CSDN通过智能技术生成

 一、list的用法注意事项

1、SLT提供的list为带头双向循环链表,以下模拟实现均按此进行。
2、有关list的迭代器失效:
insert后,迭代器it不失效
erase后,迭代器it失效
3、迭代器根据性质(容器底层结构)决定
forward iterator单向(++):forward_list(单链表)/unordered_xxx(哈希)
bidirectional Iterator双向(++/--):list/map/set
Random access iterator随机(++/--/+/-):vector/string/deque
4、迭代器对象不写析构函数,因为迭代器仅用于访问容器,节点不属于迭代器,不需要迭代器释放。

一、list的模拟实现以及测试

模拟实现:

//list.h

#pragma once
#include<assert.h>
#include<iostream>
using namespace std;

namespace star
{
	template<class T>
	struct list_node//struct默认公有
	{
		list_node<T>* next;
		list_node<T>* prev;
		T val;

		list_node(const T& _val = T())
			:next(nullptr)
			, prev(nullptr)
			, val(_val)
		{}
	};

	//typedef __list_iterator<T, T&, T*> iterator;
	//typedef __list_iterator<T, const T&, const T*> const_iterator;
	template<class T, class Ref, class Ptr>//T模版控制链表类型,Ref,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*()//返回T&,可读可写
		{
			return _node->val;
		}

		Ptr operator->()
		{
			return &_node->val;
		}

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

		bool operator!=(const self& it) const//end传值返回_head的拷贝(临时对象),具有常性
		{
			return _node != it._node;
		}

		bool operator==(const self& 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;

		iterator begin()
		{
			return _head->next;//单参数的构造函数支持隐式类型转换,故可以不用强制类型转换
		}

		iterator end()
		{
			return _head;
		}

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

		const iterator end() const
		{
			return _head;
		}

		//构造函数
		list()
		{
			empty_init();
		}
		//初始化链表
		void empty_init()
		{
			_head = new Node;
			_head->prev = _head;
			_head->next = _head;
			_size = 0;
		}
		//拷贝构造
		list(list<T>& It)
		{
			empty_init();
			for (auto& e : It)
			{
				push_back(e);
			}
		}

		void swap(list<T>& It)
		{
			std::swap(_head, It._head);
			std::swap(_size, It._size);
		}
		//赋值重载
		list<T>& operator=(list<T> It)
		{
			swap(It);
			return *this;
		}
		//析构函数
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);//接收返回值,解决迭代器失效问题
			}
			_size = 0;
		}

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

			prev->next = newnode;
			newnode->prev = prev;

			newnode->next = cur;
			cur->prev = newnode;

			++_size;

			return newnode;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* cur = pos._node;
			Node* prev = cur->prev;
			Node* next = cur->next;

			prev->next = next;
			next->prev = prev;

			delete cur;

			--_size;

			return next;
		}
		//void push_back(const T& /*//val=T()*/ x)
		//{
		//	Node* tail = _head->prev;//找尾
		//	Node* newnode = new Node(x);

		//	tail->next = newnode;
		//	newnode->prev = tail;

		//	newnode->next = _head;
		//	_head->prev = newnode;
		//}

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

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

		void pop_back(const T& x)
		{
			erase(--end());
		}

		void pop_front(const T& x)
		{
			erase(begin());
		}

		size_t size()
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

    void Print(const list<int>& It)
	{
		//typename list<int>::const_iterator it = It.begin();//typename必须加,否则编译器无法识别是静态对象还是内嵌类型。
		auto it = It.begin();//自动识别类型
		while (it != It.end())
		{
			/*(*it) += 1;*/
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
};

测试:

//test.cpp

#include"list.h"
#include<algorithm>
using namespace star;

void test_list1()
{
	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())
	{
		(*it) += 1;
		cout << *it << " ";
		++it;
	}
	cout << endl;

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

    Print(lt);
}

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

	int _a1;
	int _a2;
};

void test_list2()
{
	list<A> lt;
	lt.push_back(A(1, 1));
	lt.push_back(A(2, 2));
	lt.push_back(A(3, 3));
	lt.push_back(A(4, 4));

	list<A>::iterator it = lt.begin();
	while (it != lt.end())
	{
		//cout << (*it)._a1 << " " << (*it)._a2 << endl;
		cout << it->_a1 << " " << it->_a2 << endl;

		++it;
	}
	cout << endl;
}

void test_list3()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_front(5);
	lt.push_front(6);
	lt.push_front(7);
	lt.push_front(8);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	/*lt.insert(lt.begin(), 100);
	lt.erase(lt.end());
	for (auto e : lt)
	{
		cout << e << " ";
	}*/
	cout << endl;

	lt.pop_front(1);
	lt.pop_back(1);

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

	lt.clear();
	lt.push_back(10);
	lt.push_back(20);
	lt.push_back(30);
	lt.push_back(40);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	cout << lt.size() << endl;
}

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

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

	list<int> lt1(lt);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	list<int> lt2;
	lt2.push_back(10);
	lt2.push_back(20);
	lt2.push_back(30);
	lt2.push_back(40);

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

	lt1 = lt2;

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

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值