STL容器----list

先来介绍一下list:list也是STL的容器

它的底层结构是双向链表,每个元素存在于互不相关的结点中。

属于序列式容器,这在前面也提到过,与其他序列式容器(array,vector,deque)相比较,list通常在任意位置插入,移除元素的效率更高

说到list就不得不提一下forward_list,不同的是forward_list底层结构是单链表

当然list与forward_list是有缺陷的:不支持随机访问,它们必须利用迭代器由已知结点遍历的要访问结点

list也支持四种迭代器:正向、反向、const正向、const反向迭代器

list也存在迭代器失效问题:

    结点删除时导致迭代器失效,并且只有删除结点的迭代器失效,其他不会受影响

下来我们来看看list的接口

#include<iostream>
#include<list>
#include<vector>
using namespace std;
//list底层是双向链表
//不支持任意位置访问,必须从已知位置迭代过去
int main()
{
	//list<int>l1;//构造空list
	list<int>l2(4, 100);//构造4个100的list
	//list<int>l3(l1.begin(), l1.end());//用迭代器构造
	//list<int>l4(l3);//拷贝构造
	//int a[] = { 1,2,3,4,5,6,7,8,9 };
	//list<int>l5(a, a + sizeof(a) / sizeof(int));//使用区间构造
	/*for (list<int>::iterator it = l2.begin(); it != l2.end(); ++it)
	{
		cout << *it << " ";
	}
	cout << endl;
*/
/*for (list<int>::reverse_iterator it = l5.rbegin(); it != l5.rend(); ++it)
{
	cout << *it << " ";
}
cout << endl;
cout << l5.size() << endl;*/
/*if (l1.empty())
{
	cout << "l1为空 "<< endl;
}
else
{
	for (auto&e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
}*/
/*l5.front() = 10;
l5.back() = 2;
for (auto&e : l5)
{
	cout << e << " ";
}
cout << endl;*/
/*const list<int>l6(a, a + sizeof(a) / sizeof(a[0]));
const int&ca = l6.front();
const int&cb = l6.back();*/
//l5.push_front(3);
//l5.pop_front();
//l5.push_back(11);
//l5.pop_back();
//auto pos = ++l5.begin();
//l5.insert(pos, 5);//在pos位置前插入5
//pos = ++l5.begin();
//l5.insert(pos, 4,4);//在pos位置前插入4个4
//l5.insert(pos, l2.begin(), l2.end());//在pos位置前插入[l2.begin(),l2.end()]区间的值
l5.erase(pos);//删除pos位置的数值
//pos = ++l5.begin();

//l5.erase(pos, l5.end());//删除pos位置到end()区间的数值
l5.erase(l2.begin(), l2.end());//不能这样删*****
//l5.resize(10);
//l5.resize(20, 7);
//if (l5.empty())
//{
//	cout << "list l5 is nullptr" << endl;
//}
//else
//{

//	for (auto&e : l5)
//	{
//		cout << e << " ";
//	}
//}

//cout << ca << endl;
//cout << cb << endl;

	vector<int> v{ 4,5,6,7 };
	list<int> l1(v.begin(), v.end());
	//list<int>::iterator it = l1.begin();
	auto it = l1.begin();
	auto pos = ++l1.begin();
	while (it != pos)
	{
	/*	l1.erase(++it);*/
		it = l1.erase(it);
		++it;
	}
	return 0;
}
	/*if (l1.empty())
	{
		cout << "list l1 is nullptr" << endl;
	}
	else
	{

		for (auto&e : l1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	l1.swap(l2);
	if (l1.empty())
	{
		cout << "list l1 is nullptr" << endl;
	}
	else
	{

		for (auto&e : l1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	auto pos = ++l1.begin();
	pos =l1.erase(pos);
	
	cout << *pos << endl;
	if (l1.empty())
	{
		cout << "list l1 is nullptr" << endl;
	}
	else
	{

		for (auto&e : l1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	l2.clear();
	cout << l2.size() << endl;

	return 0;
}*/

之后我们就要模拟实现,在这里我们说说list迭代器的实现方式:

                        1、原生态指针,如vector;

                        2、将原生态指针进行封装,因迭代器的形式与指针完全相同,因此在自定义类中必须实现

迭代器实现需要注意:

   1、指针可以解引用,迭代器必须重载operator*();

   2、指针可以通过->访问其成员,迭代器必须重载operator->();

   3、指针可以++向后移动,迭代器必须重载operator++()与operator++(int); operator--()与operator--(int),forward_list不需要

   4、迭代器需要进行是否相等比较,需要重载operator==()与operator!=();

下面我们就来模拟实现一下list:

#pragma once
#include<iostream>
using namespace std;
namespace hello
{

	template<class T>
	class ListNode
	{
	public:
		ListNode(const T& val = T())
			:_pPre(nullptr)
			, _pNext(nullptr)
			, _val(val)
		{}
	public:
		ListNode<T>* _pPre;
		ListNode<T>* _pNext;
		T _val;
		
	};
	template <class T,class Ref,class Ptr>
	class ListIterator
	{
		typedef ListNode<T>* PNode;
		typedef ListIterator<T, Ref, Ptr>Self;
	public:
		ListIterator(PNode pNode = nullptr)
			:_pNode(nullptr)
		{}
		ListIterator(const Self & l)
			:_pNode(l._pNode)
		{}
		T& operator*()
		{
			return _pNode->_val;
		}
		T* operator->()
		{
			return &(operator*());
		}
		Self& operator++()
		{
			_pNode = _pNode->_pNext;
			return *this;
		}
		Self& operator++(int)
		{
			Self temp(*this);
			_pNode = _pNode->next;
			return temp;
		}
		bool operator!=(const Self& l)
		{
			return _pNode != l._pNode;
		}
		bool operator==(const Self& l)
		{
			return _pNode != l._pNode;
		}
		PNode _pNode;

	};
	template <class T ,class Ref,class Ptr,class Iterator>
	class ListReverseIterator
	{
		typedef ListReverseIterator<T, Ref, Ptr, Iterator>Self;
	public:ListReverseIterator(const Iterator& it)
		:_it(it)
	{}
		   ListReverseIterator(const Self& s)
			   :_it(s._it)
		   {}
		   Ref operator*()
		   {
			   Iterator temp = it;
			   return *(--temp);
		   }
		   Ptr operator->()
		   {
			   return &operator*();
		   }
		   Self& operator++()
		   {
			   --_it;
			   return *this;
		   }
		   Self& operator++(int)
		   {
			   Iterator temp(_it);
			   --_it;
			   return temp;
		   }
		   Self& operator--()
		   {
			   ++_it;
			   return *this;
		   }
		   Self& operator--(int)
		   {
			   Iterator temp(_it);
			   ++_it;
			   return temp;
		   }
		   bool operator!=(const Self& s)
		   {
			   return _it != s._it;
		   }
		   bool operator==(const Self& s)
		   {
			   return _it != s._it;
		   }
	private:
		Iterator _it;
	};
	template<class T>
	class List
	{
		typedef ListNode<T> Node;
		typedef Node* PNode;

	public:
		typedef ListIterator<T, T&, T*>Iterator;
		typedef ListIterator<T, const T&, const T*>ConstIterator;
		typedef ListReverseIterator<T, T&, T*, Iterator>ReverseIterator;
		typedef ListReverseIterator<T, const T&,const T*, ConstIterator>ConstReverseIterator;
	public:

		List()
		{
			CreateHead();
		}
		List(int n, const T& value = T())
		{
			CreateHead();
			for (int i = 0; i < n; ++i)
				PushBack(value);
		}
		template<class Iterator>
		List(Iterator first, Iterator last)
		{
			CreateHead();
			while (first != last)
			{
				PushBack(*first);
				++first;
			}
		}
		List(const List<T>&l)
		{
			CreateHead();
			List<T>temp(l.CBegin(), l.CEnd());
			this->Swap(temp);
		}
		List<T>& operator = (const List<T>& l)
		{
			if (this != &l)
			{
				List<T> temp(l);
				this->Swap(temp);
			}
			return *this;
		}
		~List()
		{
			Clear();
			delete _pHead;
			_pHead = nullptr;
		}
		Iterator Begin()
		{
			return Iterator(_pHead->_pNext);
		}
		Iterator End()
		{
			return Iterator(_pHead);
		}
		ReverseIterator RBegin()
		{
			return ReverseIterator(End());
		}
		ReverseIterator REnd()
		{
			return ReverseIterator(Begin());
		}
		ConstIterator CBegin()const
		{
			return ConstIterator(_pHead->pNext);
		}
		ConstIterator CEnd()const
		{
			return ConstIterator(_pHead);
		}
		ConstReverseIterator CRBegin()const
		{
			return ConstReverseIterator(CEnd());
		}
		ConstReverseIterator CREnd()const
		{
			return ConstReverseIterator(CBegin());
		}
		size_t Size()
		{
			size_t count = 0;
			PNode pCur = _pHead->_pNext;
			while (pCur != _pHead)
			{
				++count;
				pCur = pCur->_pNext;
			}
			return count;
		}
		bool Empty()
		{
			return _pHead->_pNext == _pHead;
		}
		void ReSize(size_t newSize, const T& val = T())
		{
			size_t oldSize = Size();
			if (oldSize <= newSize)
			{
				for (size_t i = oldSize; i < newSize; ++i)
					PushBack();
			}

		}
		T& Front()
		{
			return _pHead->_pNext->_val;
		}
		T& Front()const
		{
			return _pHead->_pNext->_val;

		}
		T& Back()
		{
			return _pHead->_pPre->_val;
		}
		T& Back()const
		{
			return _pHead->_pPre->_val;

		}
		void PushBack(const T& val)
		{
			PNode pNewNode = new Node(val);
			pNewNode->_pNext = _pHead;
			pNewNode->_pPre = _pHead->_pPre;
			_pHead->_pPre = pNewNode;
			pNewNode->_pPre->_pNext = pNewNode;
		}
		void PopBack()
		{
			PNode pDel = _pHead->_pPre;
			if (pDel != _pHead)
			{
				_pHead->_pPre = pDel->_pPre;
				pDel->_pPre->_pNext = _pHead;
				delete pDel;
			}
		}
		void PushFront()
		{
			PNode pNewNode = new Node(val);
			pNewNode->_pNext = _pHead->_pNext;
			pNewNode->_pPre = _pHead;
			_pHead->_pNext = pNewNode;
			_pHead->_pNext->_pPre = pNewNode;
		}
		void PopFront()
		{
			PNode pDel = _pHead->_pNext;
			if (pDel != _pHead)
			{
				_pHead->_pNext = pDel->_pNext;
				pDel->_pNext->_pPre = _pHead;
				delete pDel;
			}
		}
		Iterator Insert(Iterator pos, const T& val)
		{
			PNode pNewNode = new Node(val);
			PNode pCur = pos._pNode;
			pNewNode->_pPre = pCur->_pPre;
			pNewNode->_pNext = pCur;
			pNewNode->_pPre->_pNext = pNewNode;
			pCur->_pPre = pNewNode;
			return Iterator(pNewNode);
		}
		Iterator Erase(Iterator pos)
		{
			PNode pDel = pos._pNode;
			pNode pRet = pDel->pNext;
			pDel->_pPre->_pNext = pDel->_pNext;
			pDel->_pNext->_pPre = pDel->_pPre;
			delete pDel;
			return Iterator(pRet);
		}
		void Clear()
		{
			PNode pCur = _pHead->_pNext;
			while (pCur != _pHead)
			{
				_pHead->_pNext = pCur->_pNext;
				delete pCur;
				pCur = _pHead->_pNext;
			}
			_pHead->_pNext = _pHead;
			_pHead->_pPre = _pHead;
		}
		void Swap(List<T>& l)
		{
			swap(_pHead, l._pHead);
		}
	private:
			void CreateHead()
			{
				_pHead = new Node;
				_pHead->_pNext = _pHead;
				_pHead->_pPre = _pHead;
			}

	private:
		PNode _pHead;
	};
	
}

 要对每个实现的接口进行测试:

#include"list.h"
#include<iostream>
using namespace std;
template<class T>
void PrintList(hello::List<T>& l)
{
	auto it = l.Begin();
	while (it != l.End())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
template<class T>
void PrintListReverse(const hello::List<T>& l)
{
	auto it = l.CRBegin();
	while (it != l.CREnd())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
void TestList1()
{
	//hello::List<int> l1;
	hello::List<int> l2(10,5);
	PrintList(l2);
	int a[] = { 1,2,3,4,5,6,7,8,9,0 };
	hello::List<int>l3(a, a + sizeof(a) / sizeof(a[0]));
	PrintList(l3);


}
int main()
{
	TestList1();
	return 0;
}

这样就对list有了一定的认识!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值