STL list

2023-11-15

在这里插入图片描述

list 是一个带头双向循环链表,可以存储任意类型

模板参数 T 表示存储元素的类型,Alloc 是空间配置器,一般不用传

一、list 类的模拟实现

iterator 和 const_iterator 除了下述不同外,其他代码基本一模一样:

  • iterator 调用 operator* / operator-> 返回 T& / T*
  • const_iterator 调用 operator* / operator-> 返回 const T& / const T*

为了减少代码冗余,创建一个公有的模板,并增加两个模板参数,在调用时通过传递不同的模板参数从而得到 iterator 和 const_iterator

reverse_iterator 和 const_reverse_iterator 通过封装 iterator 实现

list 类常用接口模拟实现:

//test.cpp
#include "list.h"

int main()
{
	//starrycat::list_test5();

	starrycat::list_reverse_iterator_test();

	return 0;
}

//iterator.h
#pragma once

namespace starrycat
{
    template<class Iterator, class Ref, class Ptr>
    class __list_reverse_iterator
    {
        typedef __list_reverse_iterator<Iterator, Ref, Ptr> self;
    public:
        __list_reverse_iterator<Iterator, Ref, Ptr>() {}
        __list_reverse_iterator<Iterator, Ref, Ptr>(Iterator iter) : _cur(iter) {}

		//rbegin() 底层返回 end(),rend() 底层返回 begin()
		//因此在访问元素时,需要访问当前迭代器的前一个位置 
        Ref operator*()
        {
            Iterator tmp = _cur;
            return *--tmp;
        }

        Ptr operator->()
        {
            Iterator tmp = _cur;
            return (--tmp).operator->();
        }

        self& operator++()
        {
            --_cur;
            return *this;
        }

        self operator++(int)
        {
            Iterator tmp = _cur;
            --_cur;
            return tmp;
        }

        self& operator--()
        {
            ++_cur;
            return *this;
        }

        self operator--(int)
        {
            Iterator tmp = _cur;
            ++_cur;
            return tmp;
        }

        bool operator==(const self& s) { _cur == s._cur; }
        bool operator!=(const self& s) { _cur != s._cur; }

    private:
        Iterator _cur;
    };
}

//list.h
#pragma once

#include "iterator.h"
#include <iostream>
#include <assert.h>
#include <algorithm>

using std::cout;
using std::endl;

namespace starrycat
{
	//带头双向链表结点
	template<class T>
	struct __list_node
	{
		__list_node<T>* _prev;
		__list_node<T>* _next;
		T _data;
	};

	//迭代器
	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<T, Ref, Ptr>()
		{}

		//构造函数
		__list_iterator<T, Ref, Ptr>(node* node)
			: _node(node)
		{}

		//解引用重载
		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) const
		{
			return _node == s._node;
		}

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

	//带头双向链表
	template<class T>
	class list
	{
	public:
		typedef __list_node<T> node;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		typedef __list_reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef __list_reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;

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

		//默认构造函数
		list()
		{
			empty_Init();
		}

		//迭代器区间构造
		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			empty_Init();

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

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

		list(const list<T>& lt)
		{
			empty_Init();

			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

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

			return *this;
		}

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

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

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

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

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

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

		reverse_iterator rbegin()
		{
			return end();
		}

		const_reverse_iterator rbegin() const
		{
			return end();
		}

		reverse_iterator rend()
		{
			return begin();
		}

		const_reverse_iterator rend() const
		{
			return begin();
		}

		bool empty() const
		{
			return _head->_next == _head;
		}

		size_t size() const
		{
			size_t result = 0;
			node* cur = _head->_next;
			while (cur != _head)
			{
				++result;
				cur = cur->_next;
			}

			return result;
		}

		T& front()
		{
			return _head->_next->_data;
		}

		const T& front() const
		{
			return _head->_next->_data;
		}

		T& back()
		{
			return _head->_prev->_data;
		}

		const T& back() const
		{
			return _head->_prev->_data;
		}

		void push_front(const T& x)
		{
			//node* _head_next = _head->_next;

			//node* new_node = new node;
			//new_node->_data = x;

			//_head->_next = new_node;
			//new_node->_prev = _head;
			//new_node->_next = _head_next;
			//_head_next->_prev = new_node;

			insert(begin(), x);
		}

		void pop_front()
		{
			//assert(!empty());

			//node* del = _head->_next;
			//node* _head_new_next = del->_next;

			//_head->_next = _head_new_next;
			//_head_new_next->_prev = _head;

			//delete del;

			erase(begin());
		}

		void push_back(const T& x)
		{
			//node* tail = _head->_prev;

			//node* new_node = new node;
			//new_node->_data = x;

			//tail->_next = new_node;
			//new_node->_prev = tail;
			//new_node->_next = _head;
			//_head->_prev = new_node;
			insert(end(), x);
		}

		void pop_back()
		{
			//assert(!empty());

			//node* del = _head->_prev;
			//node* new_tail = del->_prev;

			//new_tail->_next = _head;
			//_head->_prev = new_tail;

			//delete del;

			erase(--end());
		}

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

			node* new_node = new node;
			new_node->_data = x;

			prev->_next = new_node;
			new_node->_prev = prev;
			new_node->_next = cur;
			cur->_prev = new_node;

			return pos;
		}

		iterator erase(iterator pos)
		{
			node* del = pos._node;
			node* prev = del->_prev;
			node* next = del->_next;

			prev->_next = next;
			next->_prev = prev;

			delete del;

			return next;
		}

	private:
		node* _head;
	};

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

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

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

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

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

		Print1(lt);
	}

	struct A
	{
		int _a1;
		int _a2;

		//构造函数
		A(int a1 = 0, int a2 = 0)
			: _a1(a1)
			, _a2(a2)
		{}
	};

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

	void list_test2()
	{
		list<A> lt;
		lt.push_back(A(1, 1));
		lt.push_back(A(2, 2));
		lt.push_back(A(3, 3));
		lt.push_back(A(4, 4));
		
		list<A>::iterator it = lt.begin();
		while (it != lt.end())
		{
			//cout << (*it)._a1 << " " << (*it)._a2 << endl;

			//-> 都需要这样玩
			//it->_a1 编译器默认解释为 it->->_a1 <==> it.operator->()->_a1;
			it->_a1 *= 10;
			it->_a2 *= 10;
			cout << it->_a1 << " " << it->_a2 << endl;
			++it;
		}
		cout << endl;

		Print2(lt);
	}

	void list_test3()
	{
		list<int> lt;
		cout << "empty:" << lt.empty() << endl;
		cout << "size:" << lt.size() << endl;

		lt.push_front(1);
		lt.push_front(2);
		lt.push_front(3);
		lt.push_front(4);
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		cout << "empty:" << lt.empty() << endl;
		cout << "size:" << lt.size() << endl;

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

		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

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

		lt.front() *= 10;
		lt.back() *= 100;
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void list_test4()
	{
		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;

		//list<int>::iterator pos = std::find(lt.begin(), lt.end(), 2); err
		lt.insert(++lt.begin(), 20);
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		lt.erase(++lt.begin());
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void list_test5()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		for (auto e : lt1)
		{
			cout << e << " ";
		}
		cout << endl;

		list<int> lt2(lt1.begin(), lt1.end());
		for (auto e : lt1)
		{
			cout << e << " ";
		}
		cout << endl;

		list<int> lt3(lt2);
		for (auto e : lt3)
		{
			cout << e << " ";
		}
		cout << endl;

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

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

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

	void Print3(const list<int>& lt)
	{
		list<int>::const_reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			//*rit *= 2;
			cout << *rit << " ";
			++rit;
		}
		cout << endl;
	}

	void Print4(const list<A>& lt)
	{
		list<A>::const_reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			//rit->_a1 *= 10;
			//rit->_a2 *= 10;
			cout << rit->_a1 << " " << rit->_a2 << endl;
			++rit;
		}
		cout << endl;
	}

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

		list<int>::reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			*rit *= 2;
			cout << *rit << " ";
			++rit;
		}
		cout << endl;

		Print3(lt);

		list<A> ltA;
		ltA.push_back(A(1, 1));
		ltA.push_back(A(2, 2));
		ltA.push_back(A(3, 3));
		ltA.push_back(A(4, 4));

		list<A>::reverse_iterator ritA = ltA.rbegin();
		while (ritA != ltA.rend())
		{
			ritA->_a1 *= 10;
			ritA->_a2 *= 10;
			cout << ritA->_a1 << " " << ritA->_a2 << endl;
			++ritA;
		}
		cout << endl;

		Print4(ltA);
	}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

STL list 的相关文章

随机推荐

  • Ubuntu下安装egg

    http blog csdn net flydirk article details 8506463 用easy install安装就可以了 安装之前需要python setuptools sudo apt get install pyth
  • 数字图像散斑计算Matlab连续处理1/2

    数字图像散斑计算Matlab连续处理 1 数字散斑相关测量法原理 2 打开 All m 文件 设置路径 3 运行程序 输入参考图像序号 4 框选高对比度区域 下图左图 双击以结束 结果后为下图右图 5 回到命令行 输入高对比度区域裁剪位置
  • RabbitMQ(二)confirm/return机制

    程序用了1 5 3 RELEASE版本的spring boot starter amqp依赖 confirm确认机制 配置文件
  • Python介绍

    Python由荷兰数学和计算机科学研究学会的吉多 范罗苏姆 于1990 年代初设计 作为一门叫做ABC语言的替代品 1 Python提供了高效的高级数据结构 还能简单有效地面向对象编程 Python语法和动态类型 以及解释型语言的本质 使它
  • prometheus的介绍&环境搭建配置服务启动监控

    一 prometheus的介绍 环境搭建配置 1 prometheus grafana构成 2 功能简介 Prometheus是一个开源监控系统 它前身是SoundCloud的警告工具包 主要具有如下功能 多维 数据模型 时序由 metri
  • 消息队列状态:struct msqid_ds

    Linux的消息队列 queue 实质上是一个链表 它有消息队列标识符 queue ID msgget创建一个新队列或打开一个存在的队列 msgsnd向队列末端添加一条新消息 msgrcv从队列中取消息 取消息是不一定遵循先进先出的 也可以
  • Mybatis学习

    mybatis面向接口编程 1 mybatis配置文件
  • 为什么pnpm比npm、yarn使用更好

    performant npm 意味高性能的 npm pnpm由 npm yarn 衍生而来 解决了 npm yarn 内部潜在的bug 极大的优化了性能 扩展了使用场景 被誉为 最先进的包管理工具 我们按照包管理工具的发展历史开始讲起 np
  • 转载--Windows下比较两个不同版本的二进制文件

    接手前人的软件 发现主程序依赖的动态库文件的源码没有包含在工程里面 花了好长时间找到了源代码 但是不知道它是不是最新版本的源代码 发现现有用到的动态库有两个版本的 其中一个修改时间旧一点的动态库文件在源代码的Release目录中可以找到 可
  • C# 自定义Label实现 指定字符串(关键词)高亮显示(字体、颜色)

    C 自定义Label实现 指定字符串 关键词 高亮显示 字体 颜色 原来是搞android的 本来自己就菜 现在由于项目需要开始着手弄C WPF 虽然了解一些 毕竟只是皮毛 唉 苦不堪言啊 还是得倚靠万能的互联网啊 需求 提示用户的文字 但
  • 机器学习--支持向量机(sklearn)

    机器学习 支持向量机 1 1 线性可分支持向量机 硬间隔支持向量机 训练数据集 T x 1 y 1 x 2 y 2 x N y N 当 y i 1 y i 1
  • Flutter页面不流畅,难道是使用姿势有问题?

    作者 檀婷婷 三莅 出品 阿里巴巴新零售淘系技术部 背景 高性能高流畅度一直是Flutter团队宣传的一大亮点 也是当初闲鱼选择Flutter的重要因素之一 但是随着复杂业务的应用落地 通过Flutter页面和原生页面滑动流畅度对比 我们开
  • 使用Azure Data Factory REST API和

    题解 给数组加一 class Solution public 代码中的类名 方法名 参数名已经指定 请勿修改 直接返回方法规定的值即可 题解 统计每种性别的人数 字符串子串函数的使用 substring index profile 1 SE
  • listView闪烁的问题

    用了一个ListView来实时的显示数据传输情况 于是问题就来了 当数据量比较大 而且处理速度很快时 这该死的界面闪得人眼花 废话不多说 直接上代码 首先 自定义一个类ListViewNF 继承自 System Windows Forms
  • stata 数据处理

    目录 按类别求均值 然后创建一个新的变量 缩尾处理 日期处理 连续变量处理成虚拟变量 按条件删除数据 按类别求均值 然后创建一个新的变量 bysort year industry egen meanvariable mean variabl
  • MySQL系列---事务与锁详解

    table of contents 1 背景 2 事务隔离级别 2 1 事务及其ACID属性 2 2 并发事务带来的问题 2 3 数据库事务隔离级别 3 锁机制 3 1 定义 3 2 分类 3 2 1 性能上划分 悲观乐观 3 2 2 从对
  • 解决微信小程序button的hover-class不生效问题

    在小程序开发过程中我们会遇到button添加style样式后即使添加hover class样式也没有点击效果的问题 产生该问题的原因为 在css中 内联样式style的优先级要高于class选择器的优先级 所以在我们添加style标签后即使
  • RabbitMq 报 An unexpected connection driver error occured和socket close异常处理

    进入rabbitMQ后台 1 后台地址为http localhost 15672 如果state状态为无法访问 那么我们就需要把这个链接给关掉 2 点击地址 找到close this connection 选择force close强制关闭
  • Centos7配置静态IP

    Centos7配置服务器静态IP 1 使用 ip addr 查看当前网卡信息 通过执行结果我们可以看到我们使用的网卡名称为ens33 2 配置服务器静态IP vi etc sysconfig network scripts ifcfg en
  • STL list

    文章目录 一 list 类的模拟实现 list 是一个带头双向循环链表 可以存储任意类型 模板参数 T 表示存储元素的类型 Alloc 是空间配置器 一般不用传 一 list 类的模拟实现 iterator 和 const iterator