当前位置: 首页 > news >正文

C++初阶——简单实现list

目录

1、前言

2、List.h

3、Test.cpp


1、前言

1. 简单实现std::list,重点:迭代器,类模板,运算符重载。

2. 并不是,所有的类,都需要深拷贝,像迭代器类模板,只是用别的类的资源,不需要深拷贝。

3. 高度相似 -> 模板。

4. 迭代器的种类

功能:iterator,reverse_iterator,const_iterator,const_reverse_iterator。

按结构(性质):决定可以使用什么算法

单向(Forward):forward_list/unordered_map/unordered_set    ++

双向(Bidirectional):list/map/set    ++/--

随机(Random Access):vector/string/deque    ++/--/+/-

2、List.h

#pragma once#include <iostream>
#include <list>
#include <assert.h>using namespace std;namespace Lzc
{template<class T>struct list_node{typedef list_node<T> Node;T _data;Node* _next;Node* _prev;list_node(const T& data = T()):_data(data), _next(nullptr), _prev(nullptr){}};//template<class T>//struct list_iterator//{//	typedef list_node<T> Node;//	typedef list_iterator<T> iterator;//	Node* _node;//	list_iterator(Node* node)//		:_node(node)//	{}//	T& operator*() const//	{//		return _node->_data;//	}//	T* operator->() const//	{//		return &_node->_data;//	}//	iterator& operator++() // 前置++//	{//		_node = _node->_next;//		return *this;//	}//	iterator operator++(int) // 后置++//	{//		iterator tmp(_node);//		_node = _node->_next;//		return tmp;//	}//	iterator& operator--() // 前置--//	{//		_node = _node->_prev;//		return *this;//	}//	iterator operator--(int) // 后置--//	{//		iterator tmp(_node);//		_node = _node->_prev;//		return tmp;//	}//	bool operator!=(const iterator& it) const//	{//		return _node != it._node;//	}//  bool operator==(const iterator& it) const//	{//		return _node == it._node;//	}//};//template<class T>//struct list_const_iterator//{//	typedef list_node<T> Node;//	typedef list_const_iterator<T> const_iterator;//	Node* _node;//	list_const_iterator(Node* node)//		:_node(node)//	{//	}//	const T& operator*() const//	{//		return _node->_data;//	}//	const T* operator->() const//	{//		return &_node->_data;//	}//	const_iterator& operator++() // 前置++//	{//		_node = _node->_next;//		return *this;//	}//	const_iterator operator++(int) // 后置++//	{//		iterator tmp(_node);//		_node = _node->_next;//		return tmp;//	}//	const_iterator& operator--() // 前置--//	{//		_node = _node->_prev;//		return *this;//	}//	const_iterator operator--(int) // 后置--//	{//		iterator tmp(_node);//		_node = _node->_prev;//		return tmp;//	}//	bool operator!=(const const_iterator& it) const//	{//		return _node != it._node;//	}// 	bool operator==(const const_iterator& it) const//	{//		return _node == it._node;//	}//};// 高度相似->模板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*() const{return _node->_data;}Ptr operator->() const{return &_node->_data;}Self& operator++() // 前置++{_node = _node->_next;return *this;}Self operator++(int) // 后置++{Self tmp(_node);_node = _node->_next;return tmp;}Self& operator--() // 前置--{_node = _node->_prev;return *this;}Self operator--(int) // 后置--{Self tmp(_node);_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; // 只有list类的成员函数或者友元才能使用这个类型别名public:typedef list_iterator<T, T&, T*> iterator;typedef list_iterator<T, const T&, const T*> const_iterator;//typedef list_iterator<T> iterator;//typedef list_const_iterator<T> const_iterator;iterator begin(){return _head->_next; // 隐式类型转换}iterator end(){return _head;}const_iterator begin() const{return _head->_next;}const_iterator end() const{return _head;}void empty_initialize(){_head = new Node;_head->_next = _head->_prev = _head;_size = 0;}list(){empty_initialize();}list(initializer_list<T> lt){empty_initialize();for (auto& e : lt){push_back(e);}}list(const list& lt){// list();构造函数不能被直接调用empty_initialize();for (auto& e : lt){push_back(e);}}void swap(list& tmp){std::swap(_head, tmp._head);std::swap(_size, tmp._size);}list& operator=(const list& lt){list tmp(lt);swap(tmp);return *this;}void clear(){while (!empty()){pop_front();}}~list(){clear();delete _head;_head = nullptr;_size = 0;}size_t size() const{return _size;}bool empty() const{return _size == 0;}void push_back(const T& val){insert(end(), val);}void push_front(const T& val){insert(begin(), val);}iterator insert(iterator pos, const T& val);void pop_front(){erase(begin());}void pop_back(){erase(_head->_prev);}iterator erase(iterator pos);private:Node* _head;size_t _size;};template<class T>typename list<T>::iterator list<T>::insert(iterator pos, const T& val){Node* newNode = new Node(val);Node* cur = pos._node;Node* prev = cur->_prev;prev->_next = newNode;newNode->_prev = prev;newNode->_next = cur;cur->_prev = newNode;++_size;return newNode;}template<class T>typename list<T>::iterator list<T>::erase(iterator pos){assert(pos != _head);Node* cur = pos._node;Node* next = cur->_next;Node* prev = cur->_prev;prev->_next = next;next->_prev = prev;delete cur;--_size;return next;}template<class Container>void print_Container(const Container& con){for (auto& e : con){cout << e << " ";}cout << endl;}
}

3、Test.cpp

#include "List.h"namespace Lzc
{struct AA{int _a1 = 1;int _a2 = 1;};void test_list1(){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;print_Container(lt);list<AA> lta;lta.push_back(AA());lta.push_back(AA());lta.push_back(AA());lta.push_back(AA());list<AA>::iterator ita = lta.begin();while (ita != lta.end()){//cout << (*ita)._a1 << ":" << (*ita)._a2 << endl;cout << ita->_a1 << ":" << ita->_a2 << endl;// 特殊处理,本来应该是两个->才合理,为了可读性,省略了一个->// cout << ita.operator->()->_a1 << ":" << ita.operator->()->_a2 << endl;++ita;}cout << endl;}void test_list2(){list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);// insert后,it还指向begin(),没有扩容的概念,不失效list<int>::iterator it = lt.begin();lt.insert(it, 10); *it += 100;print_Container(lt);// erase后,it为野指针,及时更新// 删除所有的偶数it = lt.begin();while (it != lt.end()){if (*it % 2 == 0){it = lt.erase(it);}else{++it;}}print_Container(lt);}void test_list3(){list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);list<int> lt2(lt1);print_Container(lt1);print_Container(lt2);list<int> lt3;lt3.push_back(10);lt3.push_back(20);lt3.push_back(30);lt3.push_back(40);lt1 = lt3;print_Container(lt1);print_Container(lt3);}void func(const list<int>& lt){print_Container(lt);}void test_list4(){// 直接构造list<int> lt0({ 1,2,3,4,5,6 });// 隐式类型转换list<int> lt1 = { 1,2,3,4,5,6,7,8 };const list<int>& lt3 = { 1,2,3,4,5,6,7,8 };func(lt0);func({ 1,2,3,4,5,6 });print_Container(lt1);// template<class T> class initializer_list;// { 10, 20, 30 }是一种initializer_list<int>类型//auto il = { 10, 20, 30 };//initializer_list<int> il = { 10, 20, 30 };//cout << typeid(il).name() << endl;//cout << sizeof(il) << endl;}
}int main()
{Lzc::test_list1();Lzc::test_list2();Lzc::test_list3();Lzc::test_list4();return 0;
}

注意:声明和定义分离时,为什么定义时,是list<T>::iterator,不是iterator,因为iterator在list<T>中typedef了,就属于list<T>的成员变量了。

相关文章:

C++初阶——简单实现list

目录 1、前言 2、List.h 3、Test.cpp 1、前言 1. 简单实现std::list&#xff0c;重点&#xff1a;迭代器&#xff0c;类模板&#xff0c;运算符重载。 2. 并不是&#xff0c;所有的类&#xff0c;都需要深拷贝&#xff0c;像迭代器类模板&#xff0c;只是用别的类的资源&am…...

C/C++后端开发面经

字节跳动 客户端开发 实习 一面(50min) 自我介绍是否愿意转语言,是否只愿意搞后端选一个项目来详细谈谈HTTP和HTTPS有什么区别?谈一下HTTPS加密的具体过程&#xff1a; 非对称加密 对称加密 证书认证的方式 非对称加密是为了保证对称密钥的安全性。 对称…...

linux 编辑器

1.三种模式 2.图例 3.wq 4.光标的使用...

【事件驱动框架OSAL】二.消息的管理机制

OSAL消息管理机制 二、消息管理机制2.1 消息的数据结构2.2 消息内存分配2.3 消息的接收和销毁2.3 源码链接地址 二、消息管理机制 在上一篇文中提到&#xff0c;系统消息事件&#xff08;SYS_EVENT_MSG&#xff09;用于任务间传递数据&#xff0c;而消息队列是这种机制的基础&…...

《论多源数据集成及应用》审题技巧 - 系统架构设计师

论多源数据集成及应用写作框架 一、考点概述 本论题“论多源数据集成及应用”主要考察的是计算机软件测试工程师在数据管理和集成方面的专业知识与实践能力。论题聚焦于信息爆炸时代企业、组织和个人所面临的数据挑战&#xff0c;特别是如何有效地收集、整理和清洗来自不同渠…...

【企业微信开发工具,获取位置】

微信开发者工具获取位置失败 报错原因解决方案 报错原因 getLocation:fail, the permission value is offline verifying解决方案 在开发工具栏输入链接&#xff0c;进行位置获取获取成功后&#xff0c;重新进入调用获取位置的页面即可如下图&#xff1a;...

HTML之JavaScript DOM编程获取元素的方式

HTML之JavaScript DOM编程获取元素的方式 1.获得document DOM树window.document(是window的属性)2.从document中获取要操作的元素1.直接获取var aaa document.getElementById("username") // 根据元素的id值获取页面上的唯一一个元素,有同名的则返回找到的第一个var…...

如何安装vm和centos

以下是在VMware中安装CentOS的一般步骤&#xff1a; 一、安装VMware 以下是在 Windows 系统中安装 VMware 软件的详细步骤&#xff1a; 1. 下载 VMware 软件&#xff1a; - 访问 VMware 官方网站&#xff08;https://www.vmware.com/&#xff09;。 - 根据您的操作系统选择合…...

docker 安装redis 7.4.2并挂载配置文件以及设置密码

文章目录 docker 安装redis 7.4.2下载 redis如果你喜欢使用最新的版本创建挂载redis 配置文件创建容器 docker 安装redis 7.4.2 截至2025年2月21日&#xff0c;Redis的最新稳定版本是 7.4.2。 下载 redis 如果你想拉取Redis的特定版本&#xff08;例如最新的稳定版本 7.4.2&a…...

计算机毕业设计SpringBoot+Vue.js在线教育系统(源码+文档+PPT+讲解)

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…...

Linux-C-函数栈-SP寄存器

sp&#xff08;Stack Pointer&#xff0c;栈指针&#xff09;是计算机体系结构中一个非常重要的寄存器&#xff0c;下面将详细介绍其作用和原理。 作用 1. 管理栈内存 栈是一种后进先出&#xff08;LIFO&#xff0c;Last In First Out&#xff09;的数据结构&#xff0c;在程…...

vi的基本使用

vi 是 Unix/Linux 系统中最常用的文本编辑器之一&#xff0c;功能强大但学习曲线较陡。以下是 vi 的基本使用方法&#xff1a; --- ### **1. vi 的两种模式** - **命令模式&#xff08;Command Mode&#xff09;**&#xff1a; - 默认进入的模式&#xff0c;用于执行命令&a…...

clickhouse--表引擎的使用

表引擎决定了如何存储表的数据。包括&#xff1a; 数据的存储方式和位置&#xff0c;写到哪里以及从哪里读取数据。(默认是在安装路径下的 data 路径)支持哪些查询以及如何支持。&#xff08;有些语法只有在特定的引擎下才能用&#xff09;并发数据访问。索引的使用&#xff0…...

LeetCode刷题零碎知识点整理

系列博客目录 文章目录 系列博客目录 数组变量有length属性&#xff0c;String类的对象有length()方法。String s; s.split("\\s");不能去除头部空格&#xff0c;需要使用s s.trim();String类的对象有toCharArray()方法&#xff0c;List<>类型有toArray()方法…...

GLTFLoader.js和OrbitControls.js两个 JavaScript 文件都是 Three.js 生态系统中的重要组成部分

GLTFLoader.js和OrbitControls.js两个 JavaScript 文件都是 Three.js 生态系统中的重要组成部分&#xff1a; 1. GLTFLoader.js 作用 GLTFLoader.js 是 Three.js 库中的一个辅助加载器脚本&#xff0c;其主要功能是加载 GLB 或 GLTF 格式的 3D 模型。GLTF&#xff08;GL Tra…...

大厂数据仓库数仓建模面试题及参考答案

目录 什么是数据仓库,和数据库有什么区别? 数据仓库的基本原理是什么? 数据仓库架构是怎样的? 数据仓库分层(层级划分),每层做什么?分层的好处是什么?数据分层是根据什么?数仓分层的原则与思路是什么? 数仓建模常用模型有哪些?区别、优缺点是什么?星型模型和雪…...

angular简易计算器

说明&#xff1a; 用angular实现计算器效果&#xff0c;ui风格为暗黑 效果图&#xff1a; step1: C:\Users\Administrator\WebstormProjects\untitled4\src\app\calnum\calnum.component.ts import { Component } from angular/core;Component({selector: app-calnum,import…...

谈谈 ES 6.8 到 7.10 的功能变迁(3)- 查询方法篇

上一篇咱们了解了 ES 7.10 相较于 ES 6.8 新增的字段类型&#xff0c;这一篇我们继续了解新增的查询方法。 Interval 间隔查询&#xff1a; 功能介绍 Interval 查询&#xff0c;词项间距查询&#xff0c;可以根据匹配词项的顺序、间距和接近度对文档进行排名。主要解决的查询…...

16、Python面试题解析:python中的浅拷贝和深拷贝

在 Python 中&#xff0c;浅拷贝&#xff08;Shallow Copy&#xff09; 和 深拷贝&#xff08;Deep Copy&#xff09; 是处理对象复制的两种重要机制&#xff0c;它们的区别主要体现在对嵌套对象的处理方式上。以下是详细解析&#xff1a; 1. 浅拷贝&#xff08;Shallow Copy&a…...

游戏引擎学习第119天

仓库:https://gitee.com/mrxiao_com/2d_game_3 上一集回顾和今天的议程 如果你们还记得昨天的进展&#xff0c;我们刚刚完成了优化工作&#xff0c;目标是让某个程序能够尽可能快速地运行。我觉得现在可以说它已经快速运行了。虽然可能还没有达到最快的速度&#xff0c;但我们…...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力

引言&#xff1a; 在人工智能快速发展的浪潮中&#xff0c;快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型&#xff08;LLM&#xff09;。该模型代表着该领域的重大突破&#xff0c;通过独特方式融合思考与非思考…...

(二)原型模式

原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

C# 类和继承(抽象类)

抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

大模型多显卡多服务器并行计算方法与实践指南

一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

第7篇:中间件全链路监控与 SQL 性能分析实践

7.1 章节导读 在构建数据库中间件的过程中&#xff0c;可观测性 和 性能分析 是保障系统稳定性与可维护性的核心能力。 特别是在复杂分布式场景中&#xff0c;必须做到&#xff1a; &#x1f50d; 追踪每一条 SQL 的生命周期&#xff08;从入口到数据库执行&#xff09;&#…...

抽象类和接口(全)

一、抽象类 1.概念&#xff1a;如果⼀个类中没有包含⾜够的信息来描绘⼀个具体的对象&#xff0c;这样的类就是抽象类。 像是没有实际⼯作的⽅法,我们可以把它设计成⼀个抽象⽅法&#xff0c;包含抽象⽅法的类我们称为抽象类。 2.语法 在Java中&#xff0c;⼀个类如果被 abs…...

认识CMake并使用CMake构建自己的第一个项目

1.CMake的作用和优势 跨平台支持&#xff1a;CMake支持多种操作系统和编译器&#xff0c;使用同一份构建配置可以在不同的环境中使用 简化配置&#xff1a;通过CMakeLists.txt文件&#xff0c;用户可以定义项目结构、依赖项、编译选项等&#xff0c;无需手动编写复杂的构建脚本…...

Ubuntu系统多网卡多相机IP设置方法

目录 1、硬件情况 2、如何设置网卡和相机IP 2.1 万兆网卡连接交换机&#xff0c;交换机再连相机 2.1.1 网卡设置 2.1.2 相机设置 2.3 万兆网卡直连相机 1、硬件情况 2个网卡n个相机 电脑系统信息&#xff0c;系统版本&#xff1a;Ubuntu22.04.5 LTS&#xff1b;内核版本…...

GraphRAG优化新思路-开源的ROGRAG框架

目前的如微软开源的GraphRAG的工作流程都较为复杂&#xff0c;难以孤立地评估各个组件的贡献&#xff0c;传统的检索方法在处理复杂推理任务时可能不够有效&#xff0c;特别是在需要理解实体间关系或多跳知识的情况下。先说结论&#xff0c;看完后感觉这个框架性能上不会比Grap…...