【STL】list常见用法及模拟实现(附完整源码)

这篇具有很好参考价值的文章主要介绍了【STL】list常见用法及模拟实现(附完整源码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

这篇文章我们继续STL中容器的学习,这篇文章要讲解的是list。

1. list介绍及使用

1.1 list介绍

list文档
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构
list的底层实现就是数据结构学过的带头双向循环链表:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构

1.2 list使用

我们来看一下几个常用的接口:

首先看一下构造函数:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构
这里几个都是我们熟悉的,默认构造、n个val构造、迭代器区间构造以及拷贝构造。
我们再来看一下迭代器:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构
我相信之前的文章对迭代器的介绍已经很详细了这里我们就不过多赘述了。

我们再来看一下修改操作:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构

这里list与string和vector区别在于:
list没有重载[],也就是说我们要遍历和访问list,就只能用迭代器。迭代器才是通用的方式,所有的容器都可以用迭代器,而[]只是针对特定容器的特殊方式。

遍历

int main()
{
    list<int> l;
    l.push_back(1);
    l.push_back(2);
    l.push_back(3);
    l.push_back(3);
    l.push_back(5);
    for (auto it = l.begin(); it != l.end(); ++it)
        cout << *it << " ";
    cout << endl;

    for (auto e : l)
        cout << e << " ";
    cout << endl;

    for (auto rit = l.rbegin(); rit != l.rend(); ++rit)
        cout << *rit << " ";
    cout << endl;
    return 0;
}

【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构

我们再来看几个之前没怎么用过的接口:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构
这里的splice ,它可以把链表的一部分转移到另一个链表:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构
remove就是删除指定的元素:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构
merge可以合并两个有序链表:
【STL】list常见用法及模拟实现(附完整源码),STL标准模版库,C++,c++,list,开发语言,数据结构

2. list模拟实现

2.1 迭代器功能分类

1、单向迭代器:只能++,不能- -。例如单链表,哈希表;
2、双向迭代器:既能++也能--。例如双向链表;
3、随机访问迭代器:能++ - -,也能+-。例如vector和string。
迭代器是内嵌类型(内部类或定义在类里)

2.2 list迭代器模拟实现
2.2.1 普通迭代器

在模拟实现string、vector时是使用的原生指针,没有将迭代器用类进行封装,但是STL标准库也是用类封装了迭代器。而在模拟实现list迭代器时,不能用原生指针了,因为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)
		{}

		Ref operator*()
		{
			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;
		}

		self operator--(int)
		{
			self tmp(*this);

			_node = _node->_prev;

			return tmp;
		}

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

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

2.2.2 const迭代器

我们先来看一下错误写法:
在这里插入代码片typedef __list_iterator<T> iterator; const list<T>::iterator it=lt.begin();

在STL库中是通过类模板多给一个参数来实现,这样,同一份类模板就可以生成两种不同的类型的迭代器

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;
			return iterator(_head->_next);
		}

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

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

		const_iterator end() const
		{
			return _head;
			//return const_iterator(_head);
		}

3. list和vector区别

vector list
底 层 结 构 动态顺序表,一段连续空间 带头结点的双向循环链表
随 机 访 问 支持随机访问,访问某个元素效率O(1) 不支持随机访问,访问某个元素效率O(N)
插 入 和 删 除 任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低 任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1)
空 间 利 用 率 底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高 底层结点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低
迭 代 器 原生态指针 对原生态指针进行封装
迭 代 器 失 效 在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效 带头结点的双向循环链表
使 用 场 景 需要高效存储,支持随机访问,不关心插入删除效率 大量插入和删除操作,不关心随机访问

4. 源码

list.h文章来源地址https://www.toymoban.com/news/detail-723539.html

#include<iostream>
using namespace std;
namespace w
{
	template<class T>
	struct list_node
	{
		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>
	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*()
		{
			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;
		}

		self operator--(int)
		{
			self tmp(*this);

			_node = _node->_prev;

			return tmp;
		}

		bool operator!=(const self& it) const
		{
			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;
			return iterator(_head->_next);
		}

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

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

		const_iterator end() const
		{
			return _head;
			//return const_iterator(_head);
		}

		void empty_init()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

		// lt2(lt1)
		list(const list<T>& lt)
		//list(const list& lt)
		{
			empty_init();

			for (auto& e : lt)
			{
				push_back(e);
			}
		}

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

		list<T>& operator=(list<T> lt)
		//list& operator=(list lt)
		{
			swap(lt);

			return *this;
		}

		~list()
		{
			clear();

			delete _head;
			_head = nullptr;
		}

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

			_size = 0;
		}

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

		// pos位置之前插入
		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);

			prev->_next = newnode;
			newnode->_next = cur;

			cur->_prev = newnode;
			newnode->_prev = prev;

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

		size_t size()
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

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

	
}

到了这里,关于【STL】list常见用法及模拟实现(附完整源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • stl中的list模拟实现

    首先我们要清楚list是一个带头双向循环的链表。 在下面代码中我们用到了 模板 ,并且用的是 struct 没有用 class ,这是因为我们使用struct时相当于 这一个类是公开的 ,当然我们也可以使用class但是得使用友元函数比较麻烦。 我们可以查看stl的源码进行查看,如下: 我们可以

    2024年02月02日
    浏览(34)
  • 【STL】模拟实现简易 list

    目录 1. 读源码 2. 框架搭建  3. list 的迭代器 4. list 的拷贝构造与赋值重载 拷贝构造 赋值重载 5. list 的常见重要接口实现 operator--()  insert 接口 erase 接口 push_back 接口 push_front 接口 pop_back 接口 pop_front 接口 size 接口 clear 接口 别忘了析构函数 源码分享 写在最后: 读源码千万

    2024年02月16日
    浏览(25)
  • 【STL】list用法&试做_底层实现

    目录 一,list  使用 1. list 文档介绍  2. 常见接口 1.   list中的sort 2. list  + sort 与 vector  + sort效率对比 3. 关于迭代器失效 4. clear 二,list 实现 1.框架搭建  2. 迭代器类——核心框架 3. operator-  实现  4. const——迭代器 5. insert 6. erase 7. clear——实现 8. 拷贝构造  首先实现迭

    2024年02月16日
    浏览(27)
  • C++ STL->list模拟实现

    list list文档 list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。 list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。 list与forward_list非常相似:最主

    2024年02月20日
    浏览(31)
  • C++_STL——list模拟实现

    list作为一个容器组件,有着其重要的价值,其实在底层这是一个 带头双向循环的链表数据结构 ,可以在任意位置进行插入和删除的序列式容器,并且该容器可以前后进行遍历。 那么同样作为序列式容器,list和vector相比又有哪些优缺点呢? 优点 :对于在任意位置进行插入、

    2024年02月16日
    浏览(28)
  • [ C++ ] STL---list的模拟实现

    目录 结点类的模拟实现 迭代器类的模拟实现 构造函数 前置++与后置++ 前置- -与后置 - - == 与 !=运算符重载 * 运算符重载 - 运算符重载 普通迭代器总体实现代码 list类的实现 list类的成员变量 构造函数 迭代器 insert() erase() push_front/push_back/pop_front/pop_back front/back clear() empty()

    2024年04月09日
    浏览(45)
  • 【C++】STL---list的模拟实现

    上次模拟实现了一个vector容器,那么我们这次来实现一个list(链表)容器,链表在实际的开发中并不常见。但是也是一种很重要的数据结构,下面给大家介绍一下链表(list) 和 vector(顺序表)的区别。 list 和 vector 一样,是一个存储容器。不同的是vector在内存中是连续存储的,而

    2024年01月22日
    浏览(37)
  • C++STL的list模拟实现

    要实现STL的list, 首先我们还得看一下list的源码。 我们看到这么一个东西,我们知道C++兼容C,可以用struct来创建一个类。但是我们习惯用class。 那什么时候会用struct呢? 这个类所有成员都想开放出去,比如结点的指针,它一般开放出来。所以我们用struct.。 继续看源码比较重

    2024年02月04日
    浏览(29)
  • C++ ——STL容器【list】模拟实现

    代码仓库: list模拟实现 list源码 数据结构——双向链表 源码的list是双向带头循环链表,所以我们定义两个节点,一个指向下一个,一个指向前一个 list类包含一个 _head 头节点,然后为了方便查出当前有多少个节点,还能多定义一个 _size 源码的迭代器设置了三个模板参数:

    2024年02月15日
    浏览(32)
  • C++ [STL之list模拟实现]

    本文已收录至《C++语言》专栏! 作者:ARMCSKGT list的底层与vector和string不同,实现也有所差别,特别是在迭代器的设计上,本节将为大家介绍list简单实现,并揭开list迭代器的底层! 本文介绍list部分简单接口,以list迭代器的介绍为主! list底层是一个带头双向循环链表,在节

    2024年02月09日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包