鞍山晟宇网站建设/产品设计
目录
1,list的介绍及使用
2,list_node
3,list_node()
3,list
4,list()
5,push_back(const T& x)
6,print()
7,_list_iterator
8,operator*()
9,begin()
10,end()
11,operator->()
12,operator++()
13,operator++(int)
14,operator--()
15,operator--(int)
16,operator==(const sefl& s)
17,operator!=(const sefl& s)
18,_list_const_iterator
19,list(iterator first, iterator last)
20,begin()const
21,end()const
22,list(const list& lt)
23,operator=(list lt)
24,insert(iterator pos, const T& x)
25,erase(iterator pos)
26,clear()
27,~list()
28,push_front(const T& x)
29,pop_front()
30,pop_back()
31,源代码
32,总结
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_node
结点的结构体框架
template<class T>struct list_node{list_node<T>* _next;list_node<T>* _prev;T data;};
因为是双向循环链表,需要一个上指针 _prev,下指针 _next,还有数据 data;
3,list_node()
对结点进行初始化
list_node(const T& x = T()):_next(nullptr), _prev(nullptr), data(x){}
然后还要将其初始化,指针为空,数据为内置类型初始化的值;
3,list
链表结构框架
template <class T>class list{typedef list_node<T> node;public:private:node* _head;};
链表是带头结点的,所以我们需要一个哨兵位头结点;
4,list()
对链表初始化
void empty_init(){_head = new node;_head->_next = _head;_head->_prev = _head;}list(){empty_init();}
因为我们是双向循环链表,所以我们的下一个结点和上一个结点都是指向自己的,形成一个环;
5,push_back(const T& x)
尾插
void push_back(const T& x){node* tail = _head->_prev;node* newnode = new node(x);tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}
我们先找到尾结点(tail),申请一个新结点,然后就插入其中;
6,print()
打印数据
void print(){node* cur = _head->_next;while (cur != _head){cout << cur->data << " ";cur = cur->_next;}}
哨兵位头结点本身是没有数据的,所以要从下一个结点开始
void test1(){wxd::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.print();}
也是没有任何问题的
7,_list_iterator
迭代器的框架和初始化
template<class T,class ref,class ptr>struct _list_iterator{typedef list_node<T> node;typedef _list_iterator<T,ref,ptr> sefl;node* _node;_list_iterator(node* n):_node(n){}}
有人会好奇,为什么模板里面有三个参数,现在先不急下面会进行分晓的;
指向结点的迭代器嘛,底层类型就是指针;
初始化也是一样,传来什么就是什么;
8,operator*()
迭代器解引用取值
ref operator*(){return _node->data;}
ref 其实就是 T&;
9,begin()
找头结点
iterator begin(){return iterator(_head->_next);}
直接返回构造完后的结果;
10,end()
最后一个结点的下一个位置
iterator end(){return iterator(_head);}
然后我们就可以试一下迭代器打印了;
void print_list(const list<int>& lt){list<int>::const_iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;}void test1(){wxd::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);print_list(lt1);}
11,operator->()
迭代器箭头指向取值
ptr operator->(){return &_node->data;}
返回的是 data 的地址,ptr 是 T* ;
12,operator++()
迭代器前置++
sefl& operator++(){_node = _node->_next;return *this;}
sefl 是 _list_iterator<T,ref,ptr>;
13,operator++(int)
迭代器后置++
sefl operator++(int){sefl tmp(*this);_node = _node->_next;return tmp;}
返回的是之前的值,但其实已经改变了;
14,operator--()
迭代器前置 - -
sefl& operator--(){_node = _node->_prev;return *this;}
15,operator--(int)
迭代器后置 - -
sefl operator--(int){sefl tmp(*this);_node = _node->_prev;return tmp;}
16,operator==(const sefl& s)
迭代器判断相等
bool operator==(const sefl& s){return _node == s._node;}
判断迭代器是否相等比较 _node 就可以了;
17,operator!=(const sefl& s)
判断是否不相等
bool operator!=(const sefl& s){return _node != s._node;}
18,_list_const_iterator
然后这个是 const 迭代器版本的,这里我就不一个一个写了;
template<class T>struct _list_const_iterator{typedef list_node<T> node;typedef _list_const_iterator<T> sefl;node* _node;_list_const_iterator(node* n):_node(n){}const T& operator*(){return _node->data;}sefl& operator++(){_node = _node->_next;return *this;}sefl operator++(int){sefl tmp(*this);_node = _node->_next;return tmp;}sefl& operator--(){_node = _node->_prev;return *this;}sefl operator--(int){sefl tmp(*this);_node = _node->_prev;return tmp;}bool operator==(const sefl& s){return _node == s._node;}bool operator!=(const sefl& s){return _node != s._node;}};
其实吧,_list_const_iterator 跟 _list_iterator 就是内部函数参数的返回值不同罢了,我们可以用模板参数来实例化,这样就不用写两个迭代器了;
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 下面这样操作就可以了,普通迭代器模板一个版本,const 迭代器模板内的参数加上const就可以了,等调用的时候编译器会自动匹配的;
19,list(iterator first, iterator last)
迭代器区间构造
template<class iterator>list(iterator first, iterator last){empty_init();while (first != last){push_back(*first);++first;}}
20,begin()const
const 版本取头结点
const_iterator begin()const{return const_iterator(_head->_next);}
21,end()const
const 版本取尾结点的下一个位置
const_iterator end()const{return const_iterator(_head);}
22,list(const list<T>& lt)
拷贝构造
void swap(list<T>& tmp){std::swap(_head, tmp._head);}list(const list<T>& lt){empty_init();list<T> tmp(lt.begin(), lt.end());swap(tmp);}
先把要拷贝的区间信息构造另一个 list ,然后再与 this 指针的 _head哨兵位头结点进行交换即可;
23,operator=(list<T> lt)
赋值
list<T>& operator=(list<T> lt){swap(lt);return *this;}
24,insert(iterator pos, const T& x)
插入
void insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}
定义前一个结点,和本身的结点,然后再进行插入即可;
void test3(){wxd::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);auto pos = find(lt1.begin(), lt1.end(), 3);lt1.insert(pos, 9);print_list(lt1);}
25,erase(iterator pos)
擦除
iterator erase(iterator pos){assert(pos != end());node* next = pos._node->_next;node* tail = pos._node->_prev;tail->_next = next;next->_prev = tail;delete pos._node;return iterator(next);}
先断言一下,哨兵位结点是不能擦除的;
然后找到前一个结点,后一个结点,在进行互相绑定;
在释放要删除的空间;
26,clear()
清除
void clear(){auto it = begin();while (it != end()){it=erase(it);}}
27,~list()
析构函数
~list(){clear();delete _head;_head = nullptr;}
先清空结点,然后再是否哨兵位头结点置空即可;
28,push_front(const T& x)
头插
void push_front(const T& x){insert(begin(), x);}
直接用 insert 插入更加方便;
29,pop_front()
头删
void pop_front(){erase(begin());}
30,pop_back()
尾删
void pop_back(){erase(_head->_prev);}
31,源代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;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> sefl;node* _node;_list_iterator(node* n):_node(n){}ref operator*(){return _node->data;}ptr operator->(){return &_node->data;}sefl& operator++(){_node = _node->_next;return *this;}sefl operator++(int){sefl tmp(*this);_node = _node->_next;return tmp;}sefl& operator--(){_node = _node->_prev;return *this;}sefl operator--(int){sefl tmp(*this);_node = _node->_prev;return tmp;}bool operator==(const sefl& s){return _node == s._node;}bool operator!=(const sefl& 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 push_back(const T& x){node* tail = _head->_prev;node* newnode = new node(x);tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}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);}void swap(list<T>& tmp){std::swap(_head, tmp._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;}void insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}iterator erase(iterator pos){assert(pos != end());node* next = pos._node->_next;node* tail = pos._node->_prev;tail->_next = next;next->_prev = tail;delete pos._node;return iterator(next);}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it != end()){it=erase(it);}}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(_head->_prev);}void pop_front(){erase(begin());}void print(){node* cur = _head->_next;while (cur != _head){cout << cur->data << " ";cur = cur->_next;}}private:node* _head;};
32,总结
我们就先搞一个大概的,其中还有很多分支,比如我们写的是擦除某个数据,其实也可以擦除某个范围,这些就靠大家去摸索,查阅文档了;
list 类的实现就到这里了;
加油!
相关文章:

【C++】手撕 list类(包含迭代器)
目录 1,list的介绍及使用 2,list_node 3,list_node() 3,list 4,list() 5,push_back(const T& x) 6,print() 7,_list_iterator 8,operator*() 9,…...

@Autowired 和 @Resource 的区别是什么?
Java面试题目录 Autowired 和 Resource 的区别是什么? Autowired 是 Spring 提供的注解。默认的注入方式为byType(根据类型进行匹配)。 Resource 是 JDK 提供的注解。默认注入方式为 byName(根据名称进行匹配)。 当一…...

栈和排序.
给你一个1->n的排列和一个栈,入栈顺序给定 你要在不打乱入栈顺序的情况下,对数组进行从大到小排序 当无法完全排序时,请输出字典序最大的出栈序列 输入 第一行一个数n 第二行n个数,表示入栈的顺序,用空格隔开&…...

springboot 多数据源怎么配置在控制台的sql打印日志
程序员的公众号:源1024,获取更多资料,无加密无套路! 最近整理了一波电子书籍资料,包含《Effective Java中文版 第2版》《深入JAVA虚拟机》,《重构改善既有代码设计》,《MySQL高性能-第3版》&…...

【WinForms 窗体】常见的“陷阱”
当涉及到 WinForms 窗体编程时,我们可能会遇到一些常见的问题。在本篇博客中,我将为你提供一些常见问题的解决方案。 跨线程访问控件 在 WinForms 中,当在非UI线程上执行操作并尝试访问 UI 控件时,会引发跨线程访问异常。为了解决…...

Android readelf 工具查找函数符号
ELF(Executable and Linkable Format)是一种执行文件和可链接文件的格式。它是一种通用的二进制文件格式,用于在各种操作系统中存储可执行程序、共享库和内核模块。 Android 开发当中的 so 库本质上就是一种特殊类型的 ELF 文件,…...

MySQL-索引回顾
索引是面试高频问答题,参考百度/CSDN/尚硅谷/黑马程序员/阿里云开发者社区,决定将索引知识回顾一下,忘记时,点开即可,时刻保持更新,事不宜迟,即刻享用。 索引概述 索引(index&#…...

重新认识Elasticsearch-一体化矢量搜索引擎
前言 2023 哪个网络词最热?我投“生成式人工智能”一票。过去一年大家都在拥抱大模型,所有的行业都在做自己的大模型。就像冬日里不来件美拉德色系的服饰就会跟不上时代一样。这不前段时间接入JES,用上好久为碰的RestHighLevelClient包。心血…...

【附源码】基于SSM框架的房屋租赁系统的设计与实现
基于SSM框架的房屋租赁系统的设计与实现 🍅 作者主页 央顺技术团队 🍅 欢迎点赞 👍 收藏 ⭐留言 📝 🍅 文末获取源码联系方式 📝 项目运行 环境配置: Jdk1.8 Tomcat7.0 Mysql HBuilderX&…...

[SpringBoot]如何在一个普通类中获取一个Bean
最近在项目中出现了一个这种情况:我一顿操作猛如虎的写了好几个设计模式,然后在设计模式中的类中想将数据插入数据库,因此调用Mapper持久层,但是数据怎么都写不进去,在我一顿操作猛如虎的查找下,发现在普通…...

[ERROR] 不再支持目标选项 5。请使用 7 或更高版本
在编译spirng boot 3.x版本时,出现了以下错误. 出现这个错误: [ERROR] COMPILATION ERROR : [INFO] -------------------------------------------- [ERROR] 不再支持源选项 5。请使用 7 或更高版本。 [ERROR] 不再支持目标选项 5。请使用 7 或更高版本。 要指定版本: 解决办…...

EasyMR:为 AI 未来赋能,打造弹性大数据引擎的革命
如果要评一个2023科技圈的热搜榜,那么以人工智能聊天机器人 ChatGPT 为代表的 AI大模型 绝对会霸榜整个2023。 ChatGPT 于2022年11月30日发布。产品发布5日,注册用户数就超过100万。推出仅两个月后,它在2023年1月末的月活用户已经突破了1亿&…...

C //练习 4-10 另一种方法是通过getline函数读入整个输入行,这种情况下可以不使用getch与ungetch函数。请运用这一方法修改计算器程序。
C程序设计语言 (第二版) 练习 4-10 练习 4-10 另一种方法是通过getline函数读入整个输入行,这种情况下可以不使用getch与ungetch函数。请运用这一方法修改计算器程序。 注意:代码在win32控制台运行,在不同的IDE环境下…...

竞赛保研 基于深度学习的行人重识别(person reid)
文章目录 0 前言1 技术背景2 技术介绍3 重识别技术实现3.1 数据集3.2 Person REID3.2.1 算法原理3.2.2 算法流程图 4 实现效果5 部分代码6 最后 0 前言 🔥 优质竞赛项目系列,今天要分享的是 基于深度学习的行人重识别 该项目较为新颖,适合…...

Ncast盈可视 高清智能录播系统 IPSetup.php信息泄露+RCE漏洞复现(CVE-2024-0305)
0x01 产品简介 Ncast盈可视 高清智能录播系统是广州盈可视电子科技有限公司一种先进的音视频录制和播放解决方案,旨在提供高质量、高清定制的录播体验。该系统采用先进的摄像和音频技术,结合强大的软件平台,可以实现高清视频录制、多路音频采集、实时切换和混音、定制视频分…...

GO语言Context的作用
文章目录 Context为什么需要Context多任务超时例子Context结构 Context各种使用方法创建contextvalueCtxvalueCtx结构体WithValue cancelCtxcancelCtx结构体withCancel timerCtxWithDeadlineWithTimeout 总结 Context 为什么需要Context Go语言需要Context主要是为了在并发环…...

金和OA C6 upload_json 任意文件上传漏洞
产品介绍 金和网络是专业信息化服务商,为城市监管部门提供了互联网监管解决方案,为企事业单位提供组织协同OA系统开发平台,电子政务一体化平台,智慧电商平台等服务。 漏洞概述 金和 OA C6 upload_json接口处存在任意文件上传漏洞,攻击者可以通过构造特殊请求包上…...

大模型学习第四课
学习目标: XTuner 大模型单卡低成本微调实战 学习内容: Finetune简介XTuner介绍8GB显卡玩转LLM动手实战环节 学习时间: 20240110 学习产出: Finetune简介 增量预训练微调指令跟随微调LoRA,QLoRAXTuner 简介:适配多…...

Code Runner使用外部控制台,运行结束后等待用户输入
问题描述 网上让程序运行结束暂停的方法大多数只有两种: 1.末尾加上system(“pause”) 2.start /k cmd 第一种方法每一个程序都需要在最后加上这条命令很烦; 第二章方法cmd窗口在程序运行结束后不会自动关闭,需要用户手动关闭 我想找到一种…...

IC设计的前端和后端是如何区分的?
一、工作着重点不同 **1、IC前端:**根据芯片规格书完成SOC的设计和集成, 使用仿真验证工具完成SOC的设计验证。 **2、IC后端:**将前端设计产生的门级网表通过EDA设计工具进行布局布线和进行物理验证并最终产生供制造用的GDSII数据 二、工作…...

Unity WebView 中文输入支持
使用版本:Vuplex 3D WebView for Windows v4.4; 测试环境:unity editor 2020.3.40f1c1、Windows; 1、打开脚本CanvasWebVie!wPrefab 2、找到_initCanvasPrefab方法,约略在459行附近 3、添加一行代码: …...

x-cmd pkg | trdsql - 能对 CSV、LTSV、JSON 和 TBLN 执行 SQL 查询的工具
目录 简介首次用户技术特点竞品和相关作品进一步阅读 简介 trdsql 是一个使用 sql 作为 DSL 的强大工具: 采用 SQL 对 CSV、LTSV、JSON 和 TBLN 文件执行查询与 MySQL,Postgresql,Sqlite 的 Driver 协同,可以实现对应数据库的表与文件的 JO…...

Camunda Spin
Spin 常用于在脚本中解析json或者xml使用,S(variable) 表示构造成Spin对象,通过prop(“属性名”)获取属性值,通过stringValue()、numberValue()、boolValue() 等对类型转换。 repositoryService.createDeployment().name("消息事件流程&…...

strlen/Memcpy_s/strncasecmp
strlen 声明:size_t strlen(const char *str) 举例: #include <stdio.h> #include <string.h>int main () {char str[50];int len;strcpy(str, "This is runoob.com");len strlen(str);printf("|%s| 的长度是 |%d|\n"…...

水经微图安卓版APP正式上线!
在水经微图APP(简称“微图APP”)安卓版已正式上线! 在随着IOS版上线约一周之后,安卓版终于紧随其后发布了。 微图安卓版APP下载安装 自从IOS版发布之后,就有用户一直在问安卓版什么时候发布,这里非常感谢…...

数据结构第十二弹---堆的应用
堆的应用 1、堆排序2、TopK问题3、堆的相关习题总结 1、堆排序 要学习堆排序,首先要学习堆的向下调整算法,因为要用堆排序,你首先得建堆,而建堆需要执行多次堆的向下调整算法。 但是,使用向下调整算法需要满足一个前提…...

[NSSRound#3 Team]This1sMysql
[NSSRound#3 Team]This1sMysql 源码 <?php show_source(__FILE__); include("class.php"); $conn new mysqli();if(isset($_POST[config]) && is_array($_POST[config])){foreach($_POST[config] as $key > $val){$value is_numeric($var)?(int)$…...

Android 通知简介
Android 通知简介 1. 基本通知 图1: 基本通知详情 小图标 : 必须提供,通过 setSmallIcon( ) 进行设置.应用名称 : 由系统提供.时间戳 : 由系统提供,也可隐藏时间.大图标(可选) : 可选内容(通常仅用于联系人照片,请勿将其用于应用图标),通过setLargeIcon( ) 进行设置.标题 : 可选…...

QT开发 2024最新版本优雅的使用vscode开发QT
▬▬▬▬▬▶VS开发QT◀▬▬▬▬▬ 🎄先看效果 🎄编辑环境变量 如图添加环境变量!!! 东西全在QT的安装目录!!! 找到的按照我的教程再装一次!!! 点…...

Redis性能大挑战:深入剖析缓存抖动现象及有效应对的战术指南
在实际应用中,你是否遇到过这样的情况,本来Redis运行的好好的,响应也挺正常,但突然就变慢了,响应时间增加了,这不仅会影响用户体验,还会牵连其他系统。 那如何排查Redis变慢的情况呢?…...