当前位置: 首页 > 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;但我们…...

反射获取方法和属性

Java反射获取方法 在Java中&#xff0c;反射&#xff08;Reflection&#xff09;是一种强大的机制&#xff0c;允许程序在运行时访问和操作类的内部属性和方法。通过反射&#xff0c;可以动态地创建对象、调用方法、改变属性值&#xff0c;这在很多Java框架中如Spring和Hiberna…...

Swagger和OpenApi的前世今生

Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章&#xff0c;二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑&#xff1a; &#x1f504; 一、起源与初创期&#xff1a;Swagger的诞生&#xff08;2010-2014&#xff09; 核心…...

html-<abbr> 缩写或首字母缩略词

定义与作用 <abbr> 标签用于表示缩写或首字母缩略词&#xff0c;它可以帮助用户更好地理解缩写的含义&#xff0c;尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时&#xff0c;会显示一个提示框。 示例&#x…...

大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计

随着大语言模型&#xff08;LLM&#xff09;参数规模的增长&#xff0c;推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长&#xff0c;而KV缓存的内存消耗可能高达数十GB&#xff08;例如Llama2-7B处理100K token时需50GB内存&a…...

sipsak:SIP瑞士军刀!全参数详细教程!Kali Linux教程!

简介 sipsak 是一个面向会话初始协议 (SIP) 应用程序开发人员和管理员的小型命令行工具。它可以用于对 SIP 应用程序和设备进行一些简单的测试。 sipsak 是一款 SIP 压力和诊断实用程序。它通过 sip-uri 向服务器发送 SIP 请求&#xff0c;并检查收到的响应。它以以下模式之一…...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...

scikit-learn机器学习

# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: # Also add the following code, # so that every time the environment (kernel) starts, # just run the following code: import sys sys.path.append(/home/aistudio/external-libraries)机…...

GO协程(Goroutine)问题总结

在使用Go语言来编写代码时&#xff0c;遇到的一些问题总结一下 [参考文档]&#xff1a;https://www.topgoer.com/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine.html 1. main()函数默认的Goroutine 场景再现&#xff1a; 今天在看到这个教程的时候&#xff0c;在自己的电…...

论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing

Muffin 论文 现有方法 CRADLE 和 LEMON&#xff0c;依赖模型推理阶段输出进行差分测试&#xff0c;但在训练阶段是不可行的&#xff0c;因为训练阶段直到最后才有固定输出&#xff0c;中间过程是不断变化的。API 库覆盖低&#xff0c;因为各个 API 都是在各种具体场景下使用。…...

git: early EOF

macOS报错&#xff1a; Initialized empty Git repository in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/ remote: Enumerating objects: 2691797, done. remote: Counting objects: 100% (1760/1760), done. remote: Compressing objects: 100% (636/636…...