STL—List容器

一.List介绍。
  1. list是可以在常数范围内在任意位置进行插入和删除序列式容器,并且该容器可以前后双向迭代
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息。
二.List的使用。
(一)List构造。
  1. 构造函数( (constructor))
  • list() :构造空的list
  • list (size_type n, const value_type& val = value_type()): 构造的list中包含n个值为val的元素
  • list (const list& x) :拷贝构造函数
  • list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list(区间构造)
int main()
{
	std::list<int> l1; // 构造空的l1
	std::list<int> l2(4, 100); // l2中放4个值为100的元素
	std::list<int> l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
	std::list<int> l4(l3); // 用l3拷贝构造l4
	// 以数组为迭代器区间构造l5
	int array[] = { 16, 2, 77, 29 };
	std::list<int> l5(array, array + sizeof(array) / sizeof(int));//区间构造
	// 用迭代器方式打印l5中的元素
	for (std::list<int>::iterator it = l5.begin(); it != l5.end(); it++)
		std::cout << *it << " ";
	std::cout << endl;

	for (auto& e : l5)
		std::cout << e << " ";
	std::cout << endl;
	return 0;
}
  1. 迭代器的使用
  • begin +end
    返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
  • rbegin +rend
    返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置。
    注意
    (1) begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
    (2) rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
int main()
{
	int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	//构造list容器
	list<int> ls(arr,arr+sizeof(arr)/sizeof(arr[0]));

	list<int>::iterator it = ls.begin();
	//正向迭代器
	while (it != ls.end())
	{
		cout << *it<<" ";
		++it;
	}
	cout << endl;
	//反向迭代器
	for (list<int>::reverse_iterator it = ls.rbegin(); it != ls.rend(); ++it)
		cout << *it << " ";
	cout << endl;

	return 0;
}
  1. list常用接口。
  • empty :检测list是否为空,是返回true,否则返回false

  • size :返回list中有效节点的个数

  • front :返回list的第一个节点中值的引用。

  • back :返回list的最后一个节点中值的引用。

  • push_front :在list首元素前插入值为val的元素

  • pop_front :删除list中第一个元素

  • push_back :在list尾部插入值为val的元素。

  • pop_back: 删除list中最后一个元素。

  • insert :在list position 位置中插入值为val的元素。

  • erase: 删除list position位置的元素。

  • swap :交换两个list中的元素。

  • clear : 清空list中的有效元素。
    //push_back/pop_back/push_front/pop_front:

void PrintList(list<int> L)
{
	for (auto& e : L)
		cout << e << " ";
	cout << endl;
}

void main()
{
	int array[] = { 1, 2, 3 };
	list<int> L(array, array + sizeof(array) / sizeof(array[0]));
	// 在list的尾部插入4,头部插入0
	L.push_back(4);
	L.push_front(0);
	// 删除list尾部节点和头部节点
	PrintList(L);
	L.pop_back();
	L.pop_front();
	PrintList(L);
}

// insert /erase:

void main()
{
	int array1[] = { 1, 2, 3 };
	list<int> L(array1, array1+sizeof(array1)/sizeof(array1[0]));
// 获取链表中第二个节点
	auto pos = ++L.begin();
	cout << *pos << endl;
// 在pos前插入值为4的元素
	L.insert(pos, 4);
	PrintList(L);
// 在pos前插入5个值为5的元素
	L.insert(pos, 5, 5);
	PrintList(L);
// 在pos前插入[v.begin(), v.end)区间中的元素
	vector<int> v{ 7, 8, 9 };
	L.insert(pos, v.begin(), v.end());
	PrintList(L);
// 删除pos位置上的元素
	L.erase(pos);
	PrintList(L);
// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
	L.erase(L.begin(), L.end());
	PrintList(L);
}

//swap,clear

void PrintList(list<int> L)
{
	for (auto& e : L)
		cout << e << " ";
	cout << endl;
}

int main()
{
	// 用数组来构造list
	int array1[] = { 1, 2, 3 };
	list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
	list<int> l2{ 4, 5, 6 };
	PrintList(l1);
	// 交换l1和l2中的元素
	l1.swap(l2);
	PrintList(l2);
	// 将l2中的元素清空
	l2.clear();
	cout << l2.size() << endl;
	return 0;
}
  1. list迭代器失效
  • 迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
void TestListIterator1()
{
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	list<int> l(array, array+sizeof(array)/sizeof(array[0]));
	auto it = l.begin();
	while (it != l.end())
	{
// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋
值
		l.erase(it);
		++it;
		}
	}
// 改正
void TestListIterator()
{
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	list<int> l(array, 					array+sizeof(array)/sizeof(array[0]));

	auto it = l.begin();
	while (it != l.end())
	{
		l.erase(it++); // it = l.erase(it);
	}
}

三.list模拟实现。
底层:双向循环链表:

#include<iostream>
#include<assert.h>
using namespace std;
namespace ice
{
	template<class T>
	struct _list_node//list节点
	{
		_list_node<T>* _next;//后继
		_list_node<T>* _prev;//前驱
		T _data;//值

		_list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};

	template<class T, class Ref, class Ptr>
	class _list_iterator//迭代器类
	{
		typedef _list_node<T> node;//list节点
		typedef _list_iterator<T, Ref, Ptr> Self;//迭代器
	public:
		_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)
		{
			_list_iterator<T, Ref, Ptr> tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			__list_iterator<T> tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

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

		bool operator==(const Self& it)
		{
			return _node == it._node;
		}
		//private:
		node* _node;
	};


	template<class T>
	class list//构造list类
	{
		typedef _list_node<T> node;//节点
		typedef _list_iterator<T, T&, T*> iterator;//迭代器
		typedef _list_iterator<T, const T&, const T*> const_iterator;//常迭代器
	public:
		list()//构造函数(无参)
		{
			_head = new node(T());
			_head->_next = _head;
			_head->_prev = _head;
		}

		list(const list<T>& l)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			auto it = l.begin();
			while (it != l.end())
			{
				push_back(*it);
				++it;
			}
		}

		list<T>& operator=(const list<T>& l)
		{
			clear();//不要忘记清理之前的
			auto it = l.begin();
			while (it != l.end())
			{
				push_back(*it);
				++it;
			}
			return *this;
		}

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

		void push_back(const T& x)//尾插
		{
			node* newnode = new node(x);
			node* tail = _head->_prev;//先找出尾元素
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
			//insert(end(),x);
		}

		void pop_back()//尾删
		{
			//node* tail = _head->_prev->_prev;
			//tail->_next = _head;
			//_head->_prev = tail;
			erase(--end());
		}

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

		void pop_front()//头删
		{
			erase(begin());
		}

		iterator erase(iterator pos)
		{
			node* cur = pos._node;
			assert(cur != _head);

			node* prev = cur->_prev;
			node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;

			delete cur;
			return iterator(next);
		}

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

		void print_list()
		{
			auto it = begin();
			while (it != end())
			{
				cout << *it << " ";
				++it;
			}
			cout << endl;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);//list的 end 就是 _head
		}

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

		const_iterator end()const
		{
			return const_iterator(_head);
		}
	private:
		node* _head;
	};
}

至于测试,将代码复制于编译器,自己进行插入删除操作。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值