【C++精华铺】12.STL list模拟实现

1.序言

        STL (Standard Template Library)是C++标准库中的一个重要组件,提供了许多通用的数据结构和算法。其中,STL list是一种带头双向链表容器,可以存储任意类型的元素。

list的特点包括:

  1. 双向性:list中的元素可以根据需要在前向和后向方向上进行遍历和访问。

  2. 动态大小:list的大小可以根据需要动态增长和收缩,不像数组需要预先定义大小。

  3. 高效的插入和删除:在list中插入或删除元素的操作非常高效,时间复杂度为常数时间。

  4. 不支持随机访问:由于list的实现是基于链表的,所以不支持随机访问,只能通过遍历来访问指定位置的元素。

list提供了一系列的成员函数来操作元素,包括:

  1. push_back()和push_front():分别在list的尾部和头部插入一个元素。

  2. pop_back()和pop_front():分别删除list的尾部和头部的元素。

  3. insert():在指定位置插入一个或多个元素。

  4. erase():删除指定位置的一个或多个元素。

  5. size():返回list中元素的个数。

  6. empty():判断list是否为空。

        值得注意的是,由于list是双向链表,所以在内存上的开销相对较大,而且无法通过下标直接访问元素。因此,在选择容器时需要根据实际需求进行权衡。

2.list整体结构

template<class T>
struct list_node
{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>
struct __list_iterator
{typedef list_node<T> node;typedef __list_iterator<T, Ref, Ptr> iterator;     node* _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;//······
private:node* _head;};

3.list迭代器

3.1 operator*()

        operator*() 返回的是list某节点存储的数据,并且返回时要能修改数据,所以返回类型是T&(Ref:是个模板参数,兼顾 T& 和 const T &,用哪个传那个 )。

Ref operator*()
{return _node->_Data;
}

3.2 operator->()

        operator->() 用于取list存储的数据对象里面的属性,也是模拟指针的行为,返回数据对象的地址。

Ptr operator->()
{return &_node->_Data;
}

        需要注意的是如果我们使用这个 -> 的运算符重载,假设一迭代器对象 it :it.operator->()->(某属性) 等价于 it->->(某属性) ,这里实际上有俩个 -> ,为了增加代码的可读性,这里进行了特殊处理,优化成了一个 -> :it->(某属性) 。

3.3 operator++() 和 operator++(int)、operator--() 和 operator--(int)

        operator++()(operator--())是前置++(--),返回++(--)后的值,operator++(int)(operator--())是后置++(--),返回++前的值(--)。

iterator& operator++()
{_node = _node->_next;return *this;
}
iterator operator++(int)
{ iterator tmp(*this);_node = _node->_next;return tmp;
}
iterator& operator--()
{_node = _node->_prev;return *this;
}
iterator operator--(int)
{iterator tmp(*this);_node = _node->_prev;return tmp;
}

 3.4 operator==() 和 operator!=()

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

3.5 迭代器完整代码

template<class T>
struct list_node
{
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>
struct __list_iterator
{
typedef list_node<T> node;
typedef __list_iterator<T, Ref, Ptr> iterator;
__list_iterator(node* n):_node(n)
{}
Ref operator*()
{return _node->_Data;
}
Ptr operator->()
{return &_node->_Data;
}iterator& operator++()
{_node = _node->_next;return *this;
}
iterator operator++(int)
{ iterator tmp(*this);_node = _node->_next;return tmp;
}
iterator& operator--()
{_node = _node->_prev;return *this;
}
iterator operator--(int)
{iterator tmp(*this);_node = _node->_prev;return tmp;
}bool operator==(const iterator& it)
{return _node == it._node;
}
bool operator!=(const iterator& it)
{return _node != it._node;
}
node* _node; 
};

4.list接口

4.1构造函数

list()
{_head = new node;_head->_next = _head->_prev = _head;
}~list()
{while (end() != _head){erase(end());}delete _head;_head = nullptr;
}
template<class Iterator>
list(Iterator first, Iterator last)
{_head = new node;_head->_next = _head->_prev = _head;while (first != last){push_back(*first);first++;}
}void swap(list<T>& tmp)
{std::swap(_head, tmp._head);
}list(const list<T>& l)
{_head = new node;_head->_next = _head->_prev = _head;list<T> tmp(l.begin(), l.end());swap(tmp);
}

4.2 push_back() push_front() pop_back() pop_front()

void push_back(const T& x)
{node* tail = _head->_prev;node* newnode = new node(x);newnode->_prev = tail;newnode->_next = tail->_next;tail->_next = newnode;_head->_prev = newnode;
}void push_front(const T& x)
{node* head = _head->_next;node* newnode = new node(x);newnode->_prev = _head;newnode->_next = head;_head->_next = newnode;head->_prev = newnode;}
void pop_back()
{node* tail = _head->_prev;_head->_prev = tail->_prev;tail->_prev->_next = _head;delete tail;
}
void pop_front()
{node* head = _head->_next;_head->_next = head->_next;head->_next->_prev = _head;delete head;
}

 4.3迭代器

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

 4.4 insert() 和 erase()

        注意erase的迭代器失效,需要更新pos

void insert(iterator pos, const T& x)
{node* cur = pos._node;node* newnode = new node(x);newnode->_next = cur;newnode->_prev = cur->_prev;cur->_prev->_next = newnode;cur->_prev = newnode;
}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);
}

5. list完整代码

#pragma once
#include<iostream>
#include<assert.h>
using namespace std; 
namespace zy
{template<class T>struct list_node{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>struct __list_iterator{typedef list_node<T> node;typedef __list_iterator<T, Ref, Ptr> iterator;__list_iterator(node* n):_node(n){}Ref operator*(){return _node->_Data;}Ptr operator->(){return &_node->_Data;}iterator& operator++(){_node = _node->_next;return *this;}iterator operator++(int){ iterator tmp(*this);_node = _node->_next;return tmp;}iterator& operator--(){_node = _node->_prev;return *this;}iterator operator--(int){iterator tmp(*this);_node = _node->_prev;return tmp;}bool operator==(const iterator& it){return _node == it._node;}bool operator!=(const iterator& it){return _node != it._node;}node* _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;list(){_head = new node;_head->_next = _head->_prev = _head;}~list(){while (end() != _head){erase(end());}delete _head;_head = nullptr;}template<class Iterator>list(Iterator first, Iterator last){_head = new node;_head->_next = _head->_prev = _head;while (first != last){push_back(*first);first++;}}void swap(list<T>& tmp){std::swap(_head, tmp._head);}list(const list<T>& l){_head = new node;_head->_next = _head->_prev = _head;list<T> tmp(l.begin(), l.end());swap(tmp);}list<T>& operator=(list<T> lt){swap(lt);return *this;}void push_back(const T& x){node* tail = _head->_prev;node* newnode = new node(x);newnode->_prev = tail;newnode->_next = tail->_next;tail->_next = newnode;_head->_prev = newnode;}void push_front(const T& x){node* head = _head->_next;node* newnode = new node(x);newnode->_prev = _head;newnode->_next = head;_head->_next = newnode;head->_prev = newnode;}void pop_back(){node* tail = _head->_prev;_head->_prev = tail->_prev;tail->_prev->_next = _head;delete tail;}void pop_front(){node* head = _head->_next;_head->_next = head->_next;head->_next->_prev = _head;delete head;}iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return iterator(_head->_next);}const_iterator end() const{return iterator(_head);}void insert(iterator pos, const T& x){node* cur = pos._node;node* newnode = new node(x);newnode->_next = cur;newnode->_prev = cur->_prev;cur->_prev->_next = newnode;cur->_prev = newnode;}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);}private:node* _head;};
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/872146.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

怎样去除视频上的水印和文字,视频水印文本移除教程

在观看和分享视频时&#xff0c;我们经常会遇到带有水印或额外文字的情况。这些标记有时是为了版权保护&#xff0c;有时则是平台的标识&#xff0c;但在某些情况下&#xff0c;它们可能会干扰视频的观赏体验。本文将向你介绍常见的视频水印类型以及如何使用简鹿水印助手去除这…

浅谈安数云智能安全运营管理平台:DCS-SOAR

SOAR&#xff08;security orchestration&#xff0c;automation and response&#xff09;&#xff0c;由Gartner于2015年提出&#xff0c;最初的含义是安全运营、分析与报告。2017年&#xff0c;Gartner又重新定义了SOAR的能力&#xff0c;包括安全编排、安全自动化和安全响应…

Purple Pi OH在Android11下测试WiFi和LAN的TCP和UDP传输速率

本文适用于在Purple Pi OH在Andriod11下如何测试WiFi和LAN的TCP和UDP传输速率。触觉智能的Purple Pi OH鸿蒙开源主板&#xff0c;是华为Laval官方社区主荐的一款鸿蒙开发主板。 该主板主要针对学生党&#xff0c;极客&#xff0c;工程师&#xff0c;极大降低了开源鸿蒙开发者的…

AI安全系列——[第五空间 2022]AI(持续更新)

最近很长时间没有更新&#xff0c;其实一直在学习AI安全&#xff0c;我原以为学完深度学习之后再学AI安全会更加简单些&#xff0c;但是事实证明理论转实践还是挺困难的&#xff0c;但是请你一定要坚持下去&#xff0c;因为“不是所有的坚持都有结果&#xff0c;但总有一些坚持…

QT简介、安装与运行

QT5.9.0 安装 下载地址&#xff1a;https://download.qt.io/archive/qt/ 安装过程&#xff0c;直接点击下一步&#xff0c;设置勾选如下&#xff1a; 下载VS编译插件地址如下&#xff08;已安装vs&#xff09;&#xff1a; https://download.qt.io/archive/vsaddin/2.3.2/

【ARMv8/v9 异常模型入门及渐进 9.1 - FIQ 和 IRQ 打开和关闭】

请阅读【ARMv8/v9 ARM64 System Exception】 文章目录 FIQ/IRQ Enable and Disable汇编指令详解功能解释使用场景和注意事项 FIQ/IRQ Enable and Disable 在ARMv8/v9架构中&#xff0c;可以使用下面汇编指令来打开FIQ和 IRQ,代码如下&#xff1a; asm volatile ("msr da…

敏捷营销在AI智能名片微信小程序中的应用探索

摘要&#xff1a;在数字化转型的浪潮中&#xff0c;企业面临着前所未有的挑战与机遇。AI智能名片微信小程序作为一种创新的营销工具&#xff0c;以其便捷性、智能化和高效性&#xff0c;正逐步成为企业连接客户、推广品牌的新宠。然而&#xff0c;如何在快速变化的市场环境中&a…

docker 安装 onlyoffice

1.文档地址 Installing ONLYOFFICE Docs for Docker on a local server - ONLYOFFICE 2.安装onlyoffice docker run -i -t -d -p 9000:8000 --restartalways -e JWT_ENABLEDfalse onlyoffice/documentserver 如果发现镜像无法下载,可以尝试更换镜像源 {"registry-mir…

ES6 Symbol (十三)

ES5的对象属性名都是字符串&#xff0c;这容易造成属性名的冲突。比如&#xff0c;你使用了一个他人提供的对象&#xff0c;但又想为这个对象添加新的方法&#xff08;mixin 模式&#xff09;&#xff0c;新方法的名字就有可能与现有方法产生冲突。如果有一种机制&#xff0c;保…

flutter实现语言的国际化

目录 前言 一、GetX实现国际化(推荐) 1.安装Getx 2.创建国际化的文件 3.使用国际化字符串 4.配置GetMaterialApp 5.更改语言 6.系统语言 ​编辑 7.原生工程配置 1.iOS工程配 1.打开iOS工程&#xff0c;在Project的info里面添加语言 2.创建String File文件 2.andr…

Milvus 核心设计(5)--- scalar indexwork mechanism

目录 背景 Scalar index 简介 属性过滤 扫描数据段 相似性搜索 返回结果 举例说明 1. 属性过滤 2. 扫描数据段 3. 相似性搜索 实际应用中的考虑 Scalar Index 方式 Auto indexing Inverted indexing 背景 继续Milvus的很细设计&#xff0c;前面主要阐述了Milvu…

从零开始搭建vue框架

流程图 开始 | |-- 2013 年底&#xff0c;尤雨溪开始开发 Vue 框架&#xff0c;最初命名为 Seed&#xff0c;后更名为 Vue | |-- 2013 年 12 月&#xff0c;Vue 0.6.0 版本 | |-- 2014 年 1 月 24 日&#xff0c;Vue 0.8.0 版本发布 | |-- 2014 年 2 月 25 日&#xff0c;…

智能招聘系统的AI功能解析

一、引言 随着科技的飞速发展&#xff0c;人工智能&#xff08;AI&#xff09;技术正逐步渗透到各个领域&#xff0c;为企业带来前所未有的变革。在人力资源管理领域&#xff0c;智能招聘系统的出现&#xff0c;不仅大大提高了招聘效率&#xff0c;还为企业带来了更精准、更科…

Elasticsearch:6.0及其ES-Head插件安装

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎;是目前全文搜索引擎的首选&#xff0c;性能测试我们就不累赘了&#xff0c;网上能搜索到很多性能测试结果&#xff0c;不管老版本还是新版本对Java的兼容很友好。搜索也很高效。那我们…

2024年初级注册安全工程师职业资格考试首次开考!

​2024年初级注册安全工程师考试首次开考&#xff08;注&#xff1a;该考试由各省人事考试局组织考试&#xff09;。目前未取得中级注册安全工程师证书的各位同学&#xff0c;可以关注该考试&#xff0c;毕竟初级考证相对较容易&#xff0c;先去考一个。 目前初安开考地区汇总…

高德地图+Vue3基础地图从0到1实现

前言 本文主要讲解如何利用高德地图api与vue3实现对世界地图的可视化显示。 实现效果 流程总结 流程1&#xff1a;Vue3搭建 搭建没什么说的&#xff0c;可以用cli、webpack、vite等构建工具进行初步搭建 注&#xff1a;必须要带eslint.js 流程2&#xff1a;高德地图密钥下…

请你谈谈:AnnotatedBeanDefinitionReader 显式地注册一个Bean到Spring容器,以及注册并解析配置类

为了深入探讨Spring框架中的beanDefinition对象&#xff0c;我们不可避免地要提及BeanFactoryPostProcessor这一核心类&#xff0c;它作为Spring的bean工厂后置处理器发挥着关键作用。接下来&#xff0c;我们将详细讨论BeanFactoryPostProcessor的执行时机&#xff0c;这是一个…

uniapp 微信小程序根据后端返回的文件链接打开并保存到手机文件夹中【支持doc、docx、txt、xlsx等类型的文件】!

项目场景&#xff1a; 我们在使用uniapp官方提供的uni.downloadFile以及uni.saveFile时&#xff0c;会发现这个文件下载的默认保存位置和我们预想的不太一样&#xff0c;容易找不到&#xff0c;而且没有提示&#xff0c;那么我们就需要把文件打开自己保存并且有提示保存到哪个…

网络安全保险产业发展洞察报告(2024)

数字经济高速增长&#xff0c;黑客攻击、数据泄露等网络安全风险可能直接导致企业遭受巨额的财务损失。网络安全保险作为风险转移和风险管理的有效工具&#xff0c;正逐渐成为数字安全框架中不可或缺的一环。 《网络安全保险产业发展洞察报告&#xff08;2024&#xff09;》梳…

扫地机器人自动回充功能

当然&#xff0c;增加自动化功能是提升扫地机器人智能化和易用性的重要方面。其中&#xff0c;自动回充是一项非常关键的功能&#xff0c;能够显著减少用户的干预和操作负担。以下是关于自动回充功能的一些拓展描述以及可以额外增加的自动化功能&#xff1a; 1.自动回充功能优化…