C++List容器常用函数接口刨析

 更新时间:2022年08月03日 11:31:09   作者:Rookiep  
最近我学习了C++中的STL库中的list容器,对于常用容器,我们不仅要会使用其常用的函数接口,我们还有明白这些接口在其底层是如何实现的。所以特意整理出来一篇博客供我们学习

一、基本结构

由源代码可知,list容器是有一个带头双向循环链表实现,所以我们模拟实现也需要实现一个带头双向循环链表的数据结构。

template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;
		list_node(const T& val = T())//用一个匿名对象来做缺省参数
			:_next(nullptr)
			, _prev(nullptr)
			, _data(val)
		{}
	};
template<class T>
	class list
	{
	public:
		typedef list_node<T> Node;
	private:
		Node* _head;
	};

二、list的迭代器的构造

list的迭代器与vector的迭代器不一样,list的迭代器是一个自定义类型的对象,成员变量包含一个指向节点的指针。

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)
		{}
		// 析构函数  -- 节点不属于迭代器,不需要迭代器释放
		// 拷贝构造和赋值重载 -- 默认生成的浅拷贝就可以
		// *it
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			//return &(operator*());
			return &_node->_data;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);//拷贝构造
			_node = _node->_next;
			return tmp;//因为tmp出了作用域就不在了,所以不可以引用返回
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

⭐️⭐️⭐️即用一个自定义类型封装,通过运算符的重载使迭代器实现像指针一样的操作行为。

三、迭代器的实现

	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;
		//仅仅是为了改名,如果不是为了改名,不用写。
		__list_iterator<T, const T&, const T*> begin() const
		{
			// list_node<int>*
			return __list_iterator<T, const T&, const T*>(_head->_next);
			//构造一个匿名对象
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		iterator begin()
		{
			return iterator(_head->_next);//构造一个匿名对象来返回
			//return _head->_next;//也可以,因为单参数构造函数支持隐式类型转换。
			//iterator it = _head->_next   隐式调用
		}
		iterator end()
		{
			return iterator(_head);
		}
	}

四、insert,erase

		// 插入在pos位置之前
		iterator insert(iterator pos, const T& x)//pos是一个迭代器对象
		{
			Node* newNode = new Node(x);
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			// prev  newnode  cur
			prev->_next = newNode;
			newNode->_prev = prev;
			newNode->_next = cur;
			cur->_prev = newNode;
			return iterator(newNode);
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			// prev  next
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);
		}

五、push_back,push_front,pop_back,pop_front

void push_back(const T& x)
		{
			//Node* tail = _head->_prev;
			//Node* newnode = new Node(x);
			 _head    tail  newnode
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(--end());
			//这里不可以用end()-1吧,因为尾部迭代器在尾删后是需要变得
		}
		void pop_front()
		{
			erase(begin());
		}

⭐️这里均复用了insert和erase函数。

六、构造函数与赋值重载

		list()//带头双向循环链表,初始化要先把头弄出来
		{
			_head = new Node();
			//自定义类型去调用对应类的构造函数,带不带这个括号都可
			_head->_next = _head;
			_head->_prev = _head;
		}
		// lt2(lt1)————传统写法
		/*list(const list<T>& lt)
		{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;
		for (auto e : lt)
		{
		push_back(e);//push_back中复用insert,insert中完成深拷贝
		}
		}*/
		void empty_init()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}
		//如果我们写现代写法,那么必须提供相应的带参构造
		template <class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first);//push_back中调用insert时会完成相应深拷贝
				++first;
			}
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);//交换头节点
		}
		// lt2(lt1) -- 现代写法
		list(const list<T>& lt)
		{
			empty_init();//总不能把一个野指针换给别人呀!
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		// lt2 = lt1
		list<T>& operator=(list<T> lt)
		//list<T> lt = lt1,传值传参这一步就调用了拷贝构造完成深拷贝
		{
			swap(lt);
			return *this;
		}

⭐️⭐️⭐️注意现代写法的方法

七、析构与清空

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);//用返回值更新,防止迭代器失效
			}
		}

到此这篇关于C++List容器常用函数接口刨析的文章就介绍到这了,更多相关C++List容器 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++适用入门同学的模板讲解

    C++适用入门同学的模板讲解

    人们需要编写多个形式和功能都相似的函数,因此有了函数模板来减少重复劳动;人们也需要编写多个形式和功能都相似的类,于是 C++ 引人了类模板的概念,编译器从类模板可以自动生成多个类,避免了程序员的重复劳动
    2022-07-07
  • C语言数据结构图的创建与遍历实验示例

    C语言数据结构图的创建与遍历实验示例

    这篇文章主要为大家介绍了C语言数据结构图的创建与遍历实验示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • C语言结构体计算内存占用问题解析

    C语言结构体计算内存占用问题解析

    这篇文章主要介绍了C语言结构体计算内存占用问题解析,本文通过案例来解析了C语言计算结构体内存的方式和方法,需要的朋友可以参考下
    2021-07-07
  • 一起来学习C++中类的this指针以使用

    一起来学习C++中类的this指针以使用

    这篇文章主要为大家详细介绍了C++中类的this指针以使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • getdate()函数的用法实例

    getdate()函数的用法实例

    getdate()函数的用法实例,需要的朋友可以参考一下
    2013-03-03
  • C++超详细讲解贪心策略的设计及解决会场安排问题

    C++超详细讲解贪心策略的设计及解决会场安排问题

    为了更好的应对《算法设计与分析》这门课程,我把书上以及老师讲过的案例都详细的做一个重现及解剖,让你熟记每一个潜在的考点,希望能给大家帮助
    2022-05-05
  • C语言实现简易文本编译器

    C语言实现简易文本编译器

    这篇文章主要为大家详细介绍了C语言实现简易文本编译器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • C++ 动态规划算法使用分析

    C++ 动态规划算法使用分析

    动态规划算法通常用于求解具有某种最优性质的问题。在这类问题中,可能会有许多可行解。每一个解都对应于一个值,我们希望找到具有最优值的解
    2022-03-03
  • C++ 手撸简易服务器

    C++ 手撸简易服务器

    本文主要介绍了C++ 手撸简易服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • C语言实现简单猜数字游戏

    C语言实现简单猜数字游戏

    这篇文章主要为大家详细介绍了C语言实现简单猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论