【C++】STL之list的使用和模拟实现

有了之前两个STL中容器和数据结构初阶链表的学习基础,下面list的学习将会简单很多。

目录

(一)list的介绍和使用

(1)list的介绍

(2)list的使用

(二)模拟实现list

(1)迭代器的模拟实现

(2)构造函数的模拟实现

(3)修改类型的成员函数

(4)完整的代码实现


(一)list的介绍和使用

(1)list的介绍

  • 1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代
  • 2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  • 3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  • 4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好
  • 5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说可能是一个重要的因素)

(2)list的使用

有了前面两章的学习,list的使用我们还是查文档

 list文档

首先,几个默认成员函数如下,我们着重看构造函数

 

构造函数( (constructor) )                                                                    接口说明
list (size_type n, const value_type& val = value_type()) 构造的 list 中包含 n 个值为 val 的元素
list()                                                                                      构造空的 list
list (const list& x)                                                                 拷贝构造函数
list (InputIterator fifirst, InputIterator last)                         [fifirst, last) 区间中的元素构造 list

 构造函数分为上面四种用法,我们用代码来使用操作:

void TestList1()
{
    list<int> l1;                         // 构造空的l1
    list<int> l2(4, 100);                 // l2中放4个值为100的元素
    list<int> l3(l2.begin(), l2.end());  // 用l2的[begin(), end())左闭右开的区间构造l3
    list<int> l4(l3);                    // 用l3拷贝构造l4

    // 以数组为迭代器区间构造l5
    int array[] = { 16,2,77,29 };
    list<int> l5(array, array + sizeof(array) / sizeof(int));

    // 列表格式初始化C++11
    list<int> l6{ 1,2,3,4,5 };

    // 用迭代器方式打印l5中的元素
    list<int>::iterator it = l5.begin();
    while (it != l5.end())
    {
        cout << *it << " ";
        ++it;
    }       
    cout << endl;

    // C++11范围for的方式遍历
    for (auto& e : l5)
        cout << e << " ";

    cout << endl;
}

=================================================================

list的迭代器操作的接口如下:

 迭代器的使用和string/vector没有区别(其实底层实现改变了,我们后面模拟实现会详解)

【注意】 list element access
1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
void TestList2()
{
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    list<int> l(array, array + sizeof(array) / sizeof(array[0]));
    // 使用正向迭代器正向list中的元素
    // list<int>::iterator it = l.begin();   // C++98中语法
    auto it = l.begin();                     // C++11之后推荐写法
    while (it != l.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    // 使用反向迭代器逆向打印list中的元素
    // list<int>::reverse_iterator rit = l.rbegin();
    auto rit = l.rbegin();
    while (rit != l.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;
}

=================================================================

 这两个主要返回头尾结点值的引用

关于容量的接口主要是empty判空和size返回大小的函数

 =================================================================

修改类接口(增删查改等)

函数声明                           接口说明
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 中的有效元素

 样例代码:

// list插入和删除
// push_back/pop_back/push_front/pop_front
void TestList3()
{
    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);
    PrintList(L);

    // 删除list尾部节点和头部节点
    L.pop_back();
    L.pop_front();
    PrintList(L);
}

// insert /erase 
void TestList4()
{
    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);
}

// resize/swap/clear
void TestList5()
{
    // 用数组来构造list
    int array1[] = { 1, 2, 3 };
    list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
    PrintList(l1);

    // 交换l1和l2中的元素
    list<int> l2;
    l1.swap(l2);
    PrintList(l1);
    PrintList(l2);

    // 将l2中的元素清空
    l2.clear();
    cout << l2.size() << endl;
}

(二)模拟实现list

文章开头介绍List就说过,list其实是双向链表。所以我们先定义每一个结点的构成——由指向前后的两个指针和存放的数据构成。

而链表类只需要一个成员就可以了————哨兵位的头结点。有了它,其实就等于一下子知道了头结点和尾结点了。

由于构造函数中有迭代器初始化,所以我们还是先看迭代器的模拟实现。

(1)迭代器的模拟实现

迭代器分为下面两种类型:
  • 1、迭代器要么就是原生指针
  •  2、迭代器要么就是自定义类型对原生指针的封装,模拟指针的行为

list的各个结点显然不是在同一块连续的空间上的,所以这里我们需要模拟实现第二种。原生指针的行为无非就是几类:解引用*,取地址->,++,--,!=,==等,所以我们把迭代器需要用到的几种指针的行为模拟实现即可。

这里有一点注意的是:

下面模板参数中Ref和Ptr的作用是什么?

  • 迭代器类型有两种:iterator和const_iterator,解引用*操作后返回_data的值,若是const_iterator迭代器返回的是const类型的数据,所以用模板参数Ref来统一实现*操作,当然,不用Ref模板参数也可以,但是要写一个重载函数返回const类型的数据,这样未免显得代码有点冗余,库里面的iterator也是这样使用模板参数的。Ptr也是同理。
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* n)
			:_node(n)
		{}

		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& s)
		{
			return _node != s._node;
		}

		bool operator==(const self& s)
		{
			return _node == s._node;
		}
		
	};

迭代器的几个成员函数:


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

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

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator end() const
		{
			//iterator it(_head->_next);
			//return it;
			return const_iterator(_head);
		}

(2)构造函数的模拟实现

我们查阅文档,构造函数有下面几种类型:

基本构造函数:

void empty_Init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
			empty_Init();

		}

迭代器构造:


template <class iterator>
		list(iterator first, iterator last)
		{
			empty_Init();

			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

拷贝构造(精简写法):

先自己初始化,然后把一个tmp临时变量利用迭代器构造,最后自己“渔翁得利”,直接把tmp的构造成果拿来。

void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}

		list(const list<T>& it)
		{
			empty_Init();
			list<T> tmp(it.begin(), it.end());
			swap(tmp);
		}
赋值重载=:
list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

析构函数:

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

		}

(3)修改类型的成员函数

 这里重点模拟实现几种常用的。

insert和erase:

他们两的实现在链表的数据结构中其实讲过。

void insert(iterator pos, const T& x)
		{
			node* cur = pos._node;
			node* prev = pos._node->_prev;

			node* new_node = new node(x);
			prev->_next = new_node;
			new_node->_prev = prev;
			new_node->_next = cur;
			cur->_prev = new_node;

		}


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

			node* prev = pos._node->_prev;
			node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			delete pos._node;

			return iterator(next);
			/*assert(pos != end());
			node* cur = pos._node;
			node* prev = pos._node->_prev;
			node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);*/
		}

尾插(删)、头插(删):

只需要调用insert和erase在特殊位置进行操作:

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


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

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		

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

(4)完整的代码实现

最后附完整代码实现及应用测试:

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

namespace zc
{
	template<class T>
	struct list_node
	{
		
		list_node<T>* _next;
		list_node<T>* _prev;
        T _data;

		list_node(const T& data = T())
			:
			_next(nullptr),
		_prev(nullptr),
		_data(data)
		{}
	};



	// 1、迭代器要么就是原生指针
	// 2、迭代器要么就是自定义类型对原生指针的封装,模拟指针的行为
	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* n)
			:_node(n)
		{}

		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& s)
		{
			return _node != s._node;
		}

		bool operator==(const self& s)
		{
			return _node == s._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;


		void empty_Init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
			empty_Init();

		}
		template <class iterator>
		list(iterator first, iterator last)
		{
			empty_Init();

			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}

		list(const list<T>& it)
		{
			empty_Init();
			list<T> tmp(it.begin(), it.end());
			swap(tmp);
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		

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

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

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator end() const
		{
			//iterator it(_head->_next);
			//return it;
			return const_iterator(_head);
		}

		void insert(iterator pos, const T& x)
		{
			node* cur = pos._node;
			node* prev = pos._node->_prev;

			node* new_node = new node(x);
			prev->_next = new_node;
			new_node->_prev = prev;
			new_node->_next = cur;
			cur->_prev = new_node;

		}


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

			node* prev = pos._node->_prev;
			node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			delete pos._node;

			return iterator(next);
			/*assert(pos != end());
			node* cur = pos._node;
			node* prev = pos._node->_prev;
			node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);*/
		}

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


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

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;

		}

	private:
		node* _head;

	};

	void print_list(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//(*it) *= 2;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

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

		// int*
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			(*it) *= 2;
			cout << *it << " ";
			++it;
		}
		cout << endl;

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

		print_list(lt);
	}

	struct AA
	{
		int _a1;
		int _a2;

		AA(int a1 = 0, int a2 = 0)
			:_a1(a1)
			, _a2(a2)
		{}
	};

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

		// AA* ptr
		list<AA>::iterator it = lt.begin();
		while (it != lt.end())
		{
			//cout << (*it)._a1 << ":" << (*it)._a2 << endl;		
			cout << it->_a1 << ":" << it->_a2 << endl;
			cout << it.operator->()->_a1 << ":" << it.operator->()->_a1 << 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);

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

		auto pos = lt.begin();
		++pos;
		lt.insert(pos, 20);

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

		lt.push_back(100);
		lt.push_front(1000);

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

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

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

	void test_list4()
	{

		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

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

		list<int> lt2(lt);

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

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

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

		lt2 = lt3;

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

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

	}


}

祝您学业有成!

猜你喜欢

转载自blog.csdn.net/m0_67821824/article/details/129974718