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

STL-list类

 list的介绍和使用

         list的介绍

list的介绍list的介绍

        list是双向循环链表

          list的使用 

        构造

list(size_t n,const value_type& val = value_type())构造的list中包含n个值为val的元素
list()构造空list
lis(const list& x)拷贝构造函数
list(inputlerator first,inputlterator last)用[first,list)的区间中元素构造list

int main()
{list<int> A;list<int> B(5,1);list<int> C(B);list<int> D(C.begin(),C.end());return 0;
}

        迭代器

begin

返回第一个元素的迭代器

end返回最后一个元素的下一个位置的迭代器
rbegin返回最后一个元素的迭代器
rend返回第一个元素的上一个位置的迭代器

 

 

 

 

begin与end是一组的正向地迭代器 ++向后移动

rbegin与rend是反向迭代器 ++向前移动

int main()
{int num[10] = { 1,2,3,4,5,6,7,8,9,10 };list<int> a(num,num+10);for (auto it = a.begin(); it != a.end(); it++){cout << *it << ' ';}cout << endl;for (auto rit = a.rbegin(); rit != a.rend(); rit++){cout << *rit << ' ';}cout << endl;for (auto ait : a){cout << ait;}return 0;
}
empty检测list是否为空
size返回list中有效节点数
front返回list地第一个节点中值的引用
back返回list最后一个节点中值的引用

 

 

 

 

 

 

push_front头插
pop_front头删
push_back尾插
pop_back尾删
insert在pos位置插入
erase删除pos位置元素
swap交换元素
clear清空有效元素

 

 

 

 

 

 

 

 

 

int main()
{list<int> a;a.push_back(1);a.push_front(0);for (auto it : a)cout << it;a.pop_front();for (auto it : a)cout << it;a.push_front(3);swap(a.front(),a.back());for (auto it : a)cout << it;swap(a.front(), a.back());for (auto it : a)cout << it;a.clear();cout << a.size();return 0;
}

迭代器失效

只有删除某个节点后,迭代器的位置还指向被删除节点时候才失效 

模拟实现 

 

#include<iostream>
using namespace std;namespace mylist
{template<class T>struct ListNode{ListNode(const T& val = T()):_val(val),_pPre(nullptr),_pNext(nullptr){}ListNode<T>* _pPre;ListNode<T>* _pNext;T _val;};template<class T, class Ref, class Ptr>class ListIterator{typedef ListNode<T>* PNode;typedef ListIterator<T, Ref, Ptr> Self;public:PNode node(){return _pNode;}ListIterator(PNode pNode = nullptr):_pNode(pNode){}ListIterator(const Self& l):_pNode(l._pNode){}T& operator*(){return _pNode->_val;}T* operator->(){return _pNode;}Self& operator++(){_pNode = _pNode->_pNext;return *this;}Self operator++(int){Self p = *this;_pNode = _pNode->_pNext;return Self(p);}Self& operator--(){_pNode = _pNode->_pPre;return *this;}Self& operator--(int){Self p = *this;_pNode = _pNode->_pPre;return Self(p);}bool operator!=(const Self& l) const{return _pNode != l._pNode;}bool operator==(const Self& l) const{return _pNode == l._pNode;}private:PNode _pNode;};template<class Iterator, class Ref, class Ptr>struct Reverse_iterator{typedef Reverse_iterator<Iterator, Ref, Ptr> Self;Reverse_iterator(Iterator it):_it(it){}Ref operator*(){Iterator t(_it);return *t;}Ptr operator->(){return &(operator*());}//// 迭代器支持移动Self& operator++(){Self temp(*this);--_it;return temp;}Self operator++(int){Self temp(*this);--_it;return temp;}Self& operator--(){++_it;return *this;}Self operator--(int){Self temp(*this);++_it;return temp;}bool operator!=(const Self& l) const{return _it != l._it;}bool operator==(const Self& l) const{return _it != l._it;}Iterator _it;};template<class T>class list{typedef ListNode<T> Node;typedef Node* PNode;public:typedef ListIterator<T, T&, T*> iterator;typedef ListIterator<T, const T&, const T&> const_iterator;typedef Reverse_iterator<iterator, const T&, const T*> const_Reverse_iterator;typedef Reverse_iterator<iterator,T&,T*> Reverse_iterator;public:list(){CreateHead();}list(int n, const T& value = T()){CreateHead();while(n--)push_front(value);}template <class Iterator>list(Iterator first, Iterator last){CreateHead();while (first != last){push_back(*first);first++;}}list(const list<T>& l){CreateHead();for (auto it : l){push_back(it);}}list<T>& operator=(const list<T>& l){list(l);}~list(){clear();delete _pHead;_pHead = nullptr;}///// List Iteratoriterator begin() { return iterator(_pHead->_pNext); }iterator end() {return iterator(_pHead);};const_iterator begin() const { return const_iterator(_pHead->_pNext); }const_iterator end() const { return const_iterator(_pHead); }Reverse_iterator rbegin(){return Reverse_iterator(--end());}Reverse_iterator rend(){return Reverse_iterator(--begin());}const_Reverse_iterator rbegin()const{return const_Reverse_iterator(--end());}const_Reverse_iterator rend()const{return const_Reverse_iterator(--begin());}///// List Capacitysize_t size()const{size_t s = 0;const_iterator it = this->begin();while (it != this->end()){it++;s++;}return s;}bool empty()const{return _pHead == _pHead->_pNext;}// List AccessT& front() { return _pHead->_pNext->_val; }const T& front()const { return _pHead->_pNext->_val; }T& back() { return _pHead->_pPre->_val; }const T& back()const { return _pHead->_pPre->_val; }// List Modifyvoid push_back(const T& val) { insert(end(), val); }void pop_back() { erase(--end()); }void push_front(const T& val) { insert(begin(), val); }void pop_front() { erase(begin()); }// 在pos位置前插入值为val的节点iterator insert(iterator pos, const T& val){PNode tmp = new Node(val);tmp->_pNext = pos.node();tmp->_pPre = pos.node()->_pPre;pos.node()->_pPre->_pNext = tmp;pos.node()->_pPre = tmp;return iterator(tmp);}// 删除pos位置的节点,返回该节点的下一个位置iterator erase(iterator pos){PNode it = pos.node()->_pNext;PNode p = pos.node();p->_pNext->_pPre = p->_pPre;p->_pPre->_pNext = p->_pNext;delete p;return iterator(it);}void clear(){while (_pHead != _pHead->_pNext){pop_front();}}void swap(list<T>& l){PNode tmp = l._pHead;l._pHead = _pHead;_pHead = tmp;}private:void CreateHead(){_pHead = new Node;_pHead->_pNext = _pHead;_pHead->_pPre = _pHead;}PNode _pHead;};
};

相关文章:

STL-list类

list的介绍和使用 list的介绍 list的介绍list的介绍 list是双向循环链表 list的使用 构造 list(size_t n,const value_type& val value_type())构造的list中包含n个值为val的元素list()构造空listlis(const list& x)拷贝构造函数list(inputlerator first,inputlter…...

Hanlp的学习

参考&#xff1a;HanLP 自然语言处理使用总结-CSDN博客 参考&#xff1a;Sprint Boot 工程中HanLP配置相对路径&#xff0c;始终有问题的解决方案_springboot hanlp-CSDN博客 <!--hanlp 依赖--><dependency><groupId>com.hankcs</groupId><artifa…...

Excel中函数SIGN()的用法

Excel中函数SIGN的用法 1. 函数详细讲解1.1 函数解释1.2 使用格式1.3 参数定义1.4 要点 2. 实用演示示例2.1 函数需求2.2 公式编写 3. 注意事项4. 文档下载5. 其他文章6. 获取全部Excel练习素材快来试试吧&#x1f970; 函数练习素材&#x1f448;点击即可进行下载操作操作注意…...

如何将本地电脑上的文件夹设置为和服务器的共享文件夹

将本地电脑上的文件夹设为与服务器共享的文件夹&#xff0c;通常是在本地开启文件共享&#xff0c;并配置相应的权限&#xff0c;使服务器可以访问该文件夹。以下以 Windows 系统为例说明具体操作步骤&#xff1a; 一、在本地电脑上设置共享文件夹 选择文件夹 找到需要共享的文…...

智能建筑时代的核心选择——基于SAIL-RK3576核心板的AI边缘计算网关方案

随着智能建筑技术的不断发展&#xff0c;建筑设备正日益向“智慧化”迈进。传统的建筑管理系统往往依赖中央服务器和云端平台进行数据处理和控制&#xff0c;但在实时监控、安防及能耗管理等关键环节&#xff0c;延迟和数据安全问题依然存在。此外&#xff0c;物联网设备数量激…...

08、如何预防SQL注入

目录 1、分析及其存在哪些危险 2、预防SQL注入 1、分析及其存在哪些危险 原理: SQL 注入是一种常见的网络攻击手段,攻击者通过在用户输入中插入恶意的 SQL 语句,利用程序对用户输入处理不当的漏洞,使恶意 SQL 语句被数据库服务器执行。 通常发生在应用程序将用户输入直接拼…...

【时时三省】(C语言基础)柔性数组

山不在高&#xff0c;有仙则名。水不在深&#xff0c;有龙则灵。 ----CSDN 时时三省 柔性数组 C99中&#xff0c;结构中的最后一个元素允许是未知大小的数组&#xff0c;这就叫做 柔性数组 成员。 例如&#xff1a; 这里把arr就称为柔性数组 有的编译器上是写成int arr&…...

mongodb详解二:基础操作

基础操作 数据库操作collection操作查看表插入数据查找数据 数据库操作 1.创建数据库 use test_db;如果没有数据库&#xff0c;use命令会新建一个&#xff1b;有的话&#xff0c;会切换到这个数据库 2.查看数据库 show dbs;collection操作 查看表 show tables;插入数据 …...

【数据分享】1929-2024年全球站点的逐月平均气温数据(Shp\Excel\免费获取)

气象数据是在各项研究中都经常使用的数据&#xff0c;气象指标包括气温、风速、降水、湿度等指标&#xff0c;其中又以气温指标最为常用&#xff01;说到气温数据&#xff0c;最详细的气温数据是具体到气象监测站点的气温数据&#xff01;本次我们为大家带来的就是具体到气象监…...

管理口令安全和资源(一)

学习目标 Manage passwords using profiles: 使用配置文件&#xff08;profiles&#xff09;来管理密码。这意味着你应该能够设置和修改密码策略&#xff0c;比如密码的复杂性、有效期、尝试次数限制等。在Oracle数据库中&#xff0c;配置文件是一组可以应用于所有用户的预定义…...

【Linux】【Vim】vim编辑器的用法

一、vim简介 Vim是一款功能强大且高度可定制的文本编辑器&#xff0c;广泛应用于Linux 和 Unix系统中。 它不仅继承了vi编辑器的所有特性&#xff0c;还增加了许多新的功能&#xff0c;如语法高亮、代码折叠、多级撤销等。 Vim有三种主要的工作模式&#xff1a; 命令模式&am…...

Golang Gin系列-3:Gin Framework的项目结构

在Gin教程的第3篇&#xff0c;我们将讨论如何设置你的项目。这不仅仅是把文件扔得到处都是&#xff0c;而是要对所有东西的位置做出明智的选择。相信我&#xff0c;这些东西很重要。如果你做得对&#xff0c;你的项目会更容易处理。当你以后不再为了找东西或添加新功能而绞尽脑…...

LabVIEW实车四轮轮速信号再现系统

开发了一个基于LabVIEW的实车四轮轮速信号再现系统。该系统解决现有电机驱动传感器成本高、重复性差、真实性差和精度低等问题&#xff0c;提供一种高精度、低成本的轮速信号再现解决方案。 项目背景 ABS轮速传感器在现代汽车安全系统中发挥着至关重要的作用。为保证其准确性和…...

2025.1.16——六、BabySQL 双写绕过|联合注入

题目来源&#xff1a;buuctf [极客大挑战 2019]BabySQL 1 目录 一、打开靶机&#xff0c;分析已知信息 二、手工注入解题 step 1&#xff1a;万能密码 step 2&#xff1a;正常注入&#xff0c;判断字段数 step 3&#xff1a;绕过 step 4&#xff1a;查数据库 step 5&am…...

Spring Boot 下的Swagger 3.0 与 Swagger 2.0 的详细对比

先说结论&#xff1a; Swgger 3.0 与Swagger 2.0 区别很大&#xff0c;Swagger3.0用了最新的注释实现更强大的功能&#xff0c;同时使得代码更优雅。 就个人而言&#xff0c;如果新项目推荐使用Swgger 3.0&#xff0c;对于工具而言新的一定比旧的好&#xff1b;对接于旧项目原…...

【已解决】git clone报错:Failed to connect to github.com port 443: Timed out

1.问题原因1 报错信息1&#xff1a; fatal: unable to access https://github.com/microsoft/xxx/: Failed to connect to github.com port 443: Timed out 报错信息2&#xff1a; fatal: unable to access https://github.com/xxx/xx/: OpenSSL SSL_read: Connection was …...

Qt 程序 DPI 适配方法归纳

方案1&#xff1a;通过 Windows api 处理 缺点&#xff1a;放大之后界面会模糊。 通过调用api实现 #include <ShellScalingAPI.h> #pragma comment(lib, "Shcore.lib")HRESULT hr SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE);或者使用qt.conf 实现 在…...

AI刷题-小R的随机播放顺序、不同整数的计数问题

目录 一、小R的随机播放顺序 问题描述 测试样例 解题思路&#xff1a; 问题理解 数据结构选择 算法步骤 最终代码&#xff1a; 运行结果&#xff1a; 二、 不同整数的计数问题 问题描述 测试样例 解题思路&#xff1a; 问题理解 数据结构选择 算法步骤 最终…...

windows 极速安装 Linux (Ubuntu)-- 无需虚拟机

1. 安装 WSL 和 Ubuntu 打开命令行&#xff0c;执行 WSL --install -d ubuntu若报错&#xff0c;则先执行 WSL --update2. 重启电脑 因安装了子系统&#xff0c;需重启电脑才生效 3. 配置 Ubuntu 的账号密码 打开 Ubuntu 的命令行 按提示&#xff0c;输入账号&#xff0c;密…...

【影刀_常规任务计划_API调用】

影刀_常规任务计划 1、在常规任务计划被关闭或者设置了定时任务的情况下&#xff08;非手动执行&#xff09;&#xff0c;通过API的方式启动任务&#xff0c;任务仍然可以被正常执行。 2、如果在常规任务计划里面应用中填写的参数的话&#xff0c; 如果通过api执行&#xff…...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

遍历 Map 类型集合的方法汇总

1 方法一 先用方法 keySet() 获取集合中的所有键。再通过 gey(key) 方法用对应键获取值 import java.util.HashMap; import java.util.Set;public class Test {public static void main(String[] args) {HashMap hashMap new HashMap();hashMap.put("语文",99);has…...

Bean 作用域有哪些?如何答出技术深度?

导语&#xff1a; Spring 面试绕不开 Bean 的作用域问题&#xff0c;这是面试官考察候选人对 Spring 框架理解深度的常见方式。本文将围绕“Spring 中的 Bean 作用域”展开&#xff0c;结合典型面试题及实战场景&#xff0c;帮你厘清重点&#xff0c;打破模板式回答&#xff0c…...

【UE5 C++】通过文件对话框获取选择文件的路径

目录 效果 步骤 源码 效果 步骤 1. 在“xxx.Build.cs”中添加需要使用的模块 &#xff0c;这里主要使用“DesktopPlatform”模块 2. 添加后闭UE编辑器&#xff0c;右键点击 .uproject 文件&#xff0c;选择 "Generate Visual Studio project files"&#xff0c;重…...

Android写一个捕获全局异常的工具类

项目开发和实际运行过程中难免会遇到异常发生&#xff0c;系统提供了一个可以捕获全局异常的工具Uncaughtexceptionhandler&#xff0c;它是Thread的子类&#xff08;就是package java.lang;里线程的Thread&#xff09;。本文将利用它将设备信息、报错信息以及错误的发生时间都…...

macOS 终端智能代理检测

&#x1f9e0; 终端智能代理检测&#xff1a;自动判断是否需要设置代理访问 GitHub 在开发中&#xff0c;使用 GitHub 是非常常见的需求。但有时候我们会发现某些命令失败、插件无法更新&#xff0c;例如&#xff1a; fatal: unable to access https://github.com/ohmyzsh/oh…...

如何通过git命令查看项目连接的仓库地址?

要通过 Git 命令查看项目连接的仓库地址&#xff0c;您可以使用以下几种方法&#xff1a; 1. 查看所有远程仓库地址 使用 git remote -v 命令&#xff0c;它会显示项目中配置的所有远程仓库及其对应的 URL&#xff1a; git remote -v输出示例&#xff1a; origin https://…...

Redis——Cluster配置

目录 分片 一、分片的本质与核心价值 二、分片实现方案对比 三、分片算法详解 1. ‌范围分片&#xff08;顺序分片&#xff09;‌ 2. ‌哈希分片‌ 3. ‌虚拟槽分片&#xff08;Redis Cluster 方案&#xff09;‌ 四、Redis Cluster 分片实践要点 五、经典问题解析 C…...

项目研究:使用 LangGraph 构建智能客服代理

概述 本教程展示了如何使用 LangGraph 构建一个智能客服代理。LangGraph 是一个强大的工具&#xff0c;可用于构建复杂的语言模型工作流。该代理可以自动分类用户问题、分析情绪&#xff0c;并根据需要生成回应或升级处理。 背景动机 在当今节奏飞快的商业环境中&#xff0c…...

Unity VR/MR开发-开发环境准备

视频讲解链接&#xff1a; 【XR马斯维】UnityVR/MR开发环境准备【UnityVR/MR开发教程--入门】_哔哩哔哩_bilibili...