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

list_

 1.对象创建

//
// Created by 徐昌真 on 2024/12/12.
//
#include <iostream>
#include <list>using namespace std;void Print(list<int> &my_list) {for ( list<int>::iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {//默认构造函数list<int> list_1;cout << "list_1: ";Print(list_1);//初始化列表list<int> list_2_1 = {1,2,3,4,5};  //有等号cout << "list_2_1: ";Print(list_2_1);list<int> list_2_2 ( {1,2,3,4,6} );  //没等号cout << "list_2_2: ";Print(list_2_2);//迭代器list<int> list_3( list_2_1.begin(), list_2_1.end() );cout << "list_3: ";Print(list_3);//全0初始化( 指定初始化大小 )list<int> list_4(10);cout << "list_4: ";Print(list_4);//a个blist<int> list_5(8, 6);cout << "list_5: ";Print(list_5);//拷贝构造函数list<int> list_6(list_5);cout << "list_6: ";Print(list_6);return 0;
}

2.赋值操作

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( const list<int> &my_list ){for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {9,8,5,2,1,1};cout << "list_1: ";Print(list_1);// 1. 等号 = 赋值list<int> list_2;list_2 = list_1;cout << "list_2: ";Print(list_2);// 2. assign(迭代器)list<int> list_3;list_3.assign(list_1.begin(), list_1.end() );cout << "list_3: ";Print(list_3);// 3. 初始化列表list<int> list_4;list_4.assign({1,2,3,4,5});cout << "list_4: ";Print(list_4);// 4. 初始化a 个 blist<int> list_5;list_5.assign(6,8);cout << "list_5: ";Print(list_5);//assign一下之前的东西看看list_5.assign(6,7);  //直接重置cout << "list_5: ";Print(list_5);return 0;
}

输出

3.大小操作

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( list<int>& my_list){cout << "list_1: ";for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}// 1. empty
// 2. size
// 3. resize
int main() {list<int> list_1;cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;cout << endl;list_1 = {1,2,3,4,5};cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;cout << endl;list_1.resize(20);  //全0扩容cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;Print(list_1);cout << endl;list_1.resize(22,985);  //指定数字扩容cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;Print(list_1);cout << endl;list_1.resize(3);  //缩容cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;Print(list_1);return 0;
}

输出

4.数据插入

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>
#include <vector>using namespace std;void Print(const list<int>& my_list){for (list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ) {cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1;vector<int> vec= {6,6,6,6,6,6};// 1. push_frontlist_1.push_front(3);list_1.push_front(2);list_1.push_front(1);Print(list_1);  //1 2 3// 2. push_backlist_1.push_back(4);list_1.push_back(5);list_1.push_back(6);Print(list_1);  //1 2 3 4 5 6// 3. insert// (1)  迭代器( 迭代器 + 值)list<int>::iterator iter = list_1.begin();//iter++;        //允许//iter += 1;  //不允许list_1.insert( iter, 0);Print(list_1);  //0 1 2 3 4 5 6// (2)  迭代器( 迭代器 + 数量 + 值)iter = list_1.end();list_1.insert( iter, 8,8);Print(list_1);  //0 1 2 3 4 5 6 8 8 8 8 8 8 8 8// (3)  迭代器( 迭代器 + 迭代器起始位置 + 迭代器末尾位置)     在iter这个迭代器里面 插入从 迭代器起始位置 + 迭代器末尾位置的值 这个迭代器可以来自别的list   居然也可以来自vector 太强了iter = list_1.end();list_1.insert( iter, vec.begin(), vec.end());Print(list_1);  //0 1 2 3 4 5 6 8 8 8 8 8 8 8 8 0 1 2 3 4 5 6 8 8 8 8 8 8 8 8return 0;
}

不理解

输出

5.删除操作

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( const list<int>& my_list ){for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {-1,9,8,5,2,1,1,-1};Print(list_1);  //-1 9 8 5 2 1 1 -1// 1. pop_frontlist_1.pop_front();Print(list_1);  //9 8 5 2 1 1 -1// 2. pop_backlist_1.pop_back();Print(list_1);  //9 8 5 2 1 1//erase//(1)  传入删除的位置(迭代器)list<int>::iterator iter = list_1.begin();list<int>::iterator ret = list_1.erase( iter );Print(list_1);  //8 5 2 1 1cout << *ret << endl;  //8//(2) 传入删除区域(迭代器)iter = list_1.end();  //指向末尾元素的下一个--iter;--iter;ret = list_1.erase( iter, list_1.end() );Print(list_1);  //8 5 2 1cout << *ret << endl;  //3  为什么 ???return 0;
}

输出

6.数据访问

我的疑问 : 是不是顺序表存储元素时分配的内存是连续的 而链表是分开的 所以链表不支持随机访问

回答:

是的,你的理解是正确的:

  1. 顺序表(如 vector:它存储元素的内存是连续的,这意味着每个元素都紧接着前一个元素。由于内存是连续的,vector 可以通过索引直接访问任意位置的元素,因此支持随机访问,访问时间是常数时间 O(1)

  2. 链表(如 list:它是由一系列节点组成的,每个节点包含数据和指向下一个节点的指针。由于内存不一定是连续的,所以不能像 vector 那样直接通过索引快速访问某个位置的元素。要访问链表中的某个元素,必须从头节点开始遍历,直到找到目标元素,因此链表不支持随机访问,访问时间是线性的 O(n)

总结:

  • 顺序表:支持随机访问,内存连续,访问速度快O(1)
  • 链表:不支持随机访问,内存不连续,访问速度慢O(n)
//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( list<int>& my_list){  //输出cout << "list_1: ";for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int VisitItemByIndex( list<int>& my_list, int index ){list<int>::const_iterator iter = my_list.begin();while ( index ){++iter;--index;}return *iter;
}int main() {list<int> list_1 = {1,2,3,4,5,6};Print(list_1);  //1 2 3 4 5 6 //frontcout << "list_1.front(): " << list_1.front() << endl;//backcout << "list_1.back(): " << list_1.back() << endl;//迭代器cout << VisitItemByIndex( list_1, 2 );return 0;
}

7.逆序

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( const list<int>& my_list ){for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {1,2,3,4,5};Print(list_1);list_1.reverse();Print(list_1);return 0;
}

8.排序(原理 归并排序)

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( list<int>& my_list){  //输出cout << "list_1: ";for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {1,2,3,4,2,4,56,3,2};list_1.sort();Print(list_1);return 0;
}

输出

相关文章:

list_

1.对象创建 // // Created by 徐昌真 on 2024/12/12. // #include <iostream> #include <list>using namespace std;void Print(list<int> &my_list) {for ( list<int>::iterator iter my_list.begin(); iter ! my_list.end(); iter ){cout <…...

电机驱动,为什么不需要变速器?

在现代汽车和工业应用中&#xff0c;电机驱动的技术愈发成熟&#xff0c;其核心优势之一是能够省去传统机械变速器的需求。 一、电机驱动的基本原理 电机驱动又被称为电动机驱动&#xff0c;其基本原理是将电能转化为机械能。通过控制电机的输入电压和电流&#xff0c;电机能…...

how to write 述职pptx as a tech manager

As a technical manager, crafting an effective 述职 (performance review) PPT requires you to highlight your leadership, team accomplishments, technical contributions, challenges faced, and future plans. Heres a structured approach to design your PPT: 1. Cov…...

关于QMessageBox的一些使用总结和避坑指南

参考学习 Qt中QMessageBox的用法—看这一篇就够了 Qt&#xff1a;使用QMessageBox弹出标准对话框 QMessageBox模态与非模态及QT中的exec() 如何调整QMessageBox的大小 QSS 自定义QMessageBox python QMessageBox设置标签和按钮居中、中文按钮 使用建议 经过查看多方的资料&…...

C语言预处理详解

1.预定义符号 C语言设置了一些预定义符号&#xff0c;可以直接使用&#xff0c;预定义符号也是在预处理期间处理的 __FILE__ //进⾏编译的源⽂件 __LINE__ //⽂件当前的⾏号 __DATE__ //⽂件被编译的⽇期 __TIME__ //⽂件被编译的时间 __STDC__ //如果编译器遵循ANSI C&#…...

大语言模型画图(流程图、框架图)

第一步&#xff1a;向随意大语言模型&#xff0c;描述内容&#xff0c;推荐豆包 豆包 加上下面Prompt 通过Mermaid语法&#xff0c;描述上面流程图 第二步&#xff1a;将生成Mermaid输入流程图生成网站 中文Mermaid - 流程图、关系图在线画图、生成和编辑器...

2024年API接口发展趋势:智能化、自动化引领潮流

随着信息技术的飞速发展&#xff0c;应用程序编程接口&#xff08;API&#xff09;已成为现代软件开发的核心组成部分。API作为不同系统之间的桥梁&#xff0c;使得数据、功能和服务能够在各种平台和设备之间无缝流动。在2024年&#xff0c;API接口正经历着一系列显著的变革和发…...

数据挖掘与机器学习DMML(part 8)K近邻(KNN)

K Nearest Neighbours KNN Definition KNN 是一种简单的算法&#xff0c;它存储所有可用案例&#xff0c;并根据相似度量对新案例进行分类。 KNN 不同名称&#xff1a; K-Nearest Neighbors • Memory-Based Reasoning基于记忆的推理 • Example-Based Reasoning基于实例的…...

Fortify 24.2.0版本最新版 win/mac/linux

工具介绍&#xff1a; Fortify SCA作为一款业内主流的静态代码扫描工具&#xff0c;被广泛应用于白盒测试中。与其他静态代码扫描工具相比&#xff0c;Fortify SCA的突出优势主要在于更加广泛地支持的语言和开发平台、更全面和权威的安全规则库使扫描更加全面、更加智能化的自定…...

突破时间与空间限制的富媒体百宝箱——智能工具箱:让云上内容生产更easy

“这是你的同款日常吗&#xff1f;老是在赶deadline&#xff0c;苦练PS还未出师&#xff0c;premiere、达芬奇真的好难&#xff0c;学python脑容量确实不够~打工人太难了~~” 来试试智能工具箱吧&#xff01;即来即用&#xff0c;一键实现办公自由。图片工具、视频工具、音频工…...

MacOs使用Wine 安装UaExpert与UaExpert的使用

要在 macOS 上使用 Wine 安装和运行 UaExpert&#xff0c;可以按照以下步骤操作&#xff1a; 安装 Wine 在 macOS 上&#xff0c;你可以通过 Homebrew 来安装 Wine。如果你还没有安装 Homebrew&#xff0c;可以先安装 Homebrew&#xff0c;然后使用它来安装 Wine。 bash /bin…...

【Prompt Engineering】3.文本概括

一、引言 文本信息量大&#xff0c;LLM在文本概括任务上展现出强大能力。本章介绍如何通过编程方式调用API接口实现文本概括功能。 首先&#xff0c;我们需要引入 zhipuAI 包&#xff0c;加载 API 密钥&#xff0c;定义 getCompletion 函数。 from zhipuai import ZhipuAIke…...

力扣-图论-14【算法学习day.64】

前言 ###我做这类文章一个重要的目的还是给正在学习的大家提供方向和记录学习过程&#xff08;例如想要掌握基础用法&#xff0c;该刷哪些题&#xff1f;&#xff09;我的解析也不会做的非常详细&#xff0c;只会提供思路和一些关键点&#xff0c;力扣上的大佬们的题解质量是非…...

redis 架构详解

Redis架构详解可以从以下几个方面进行阐述&#xff1a; 一、部署架构 Redis有多种部署架构&#xff0c;适用于不同的应用场景和需求&#xff0c;主要包括以下几种&#xff1a; 单机模式&#xff08;Standalone Mode&#xff09; 特点&#xff1a;部署简单&#xff0c;配置方便…...

多分类交叉熵与稀疏分类交叉熵

总结: 标签为 One-hot 编码的多分类问题,用分类交叉熵对于标签为整数的多分类问题,用稀疏分类交叉熵稀疏分类交叉熵内部会将整数标签转换为 One-hot 编码,而如果标签已经是 One-hot 编码的形式,再使用稀疏分类交叉熵就会多此一举。 算例 假设我们有三个类别:A、B 和 C。…...

PHP 8新特性深度解析与实战应用

引言 PHP作为一种广泛使用的开源脚本语言&#xff0c;以其在Web开发领域的卓越性能而闻名。随着PHP 8的发布&#xff0c;这门语言再次迎来了许多令人兴奋的新特性和改进。本文将深入探讨PHP 8的新特性&#xff0c;并提供实战应用示例&#xff0c;帮助开发者更好地理解和使用PH…...

【C语言】UDP通信

udp使用的是数据报传输。可以一对一&#xff0c;一对多进行传输&#xff0c;用于快速&#xff0c;实时性高的场景 服务器端&#xff1a; 使用步骤&#xff1a; 1.创建socket 2.bind绑定可接收的客户端 3.while{ recv接收数据 send发送数据 } #include <stdio.h> #inclu…...

用Keytool和OpenSSL生成和签发数字证书

一)keytool生成私钥文件(.key)和签名请求文件(.csr),openssl签发数字证书 J2SDK在目录%JAVA_HOME%/bin提供了密钥库管理工具Keytool,用于管理密钥、证书和证书链。Keytool工具的命令在JavaSE6中已经改变,不过以前的命令仍然支持。Keytool也可以用来管理对称加密算法中…...

算法-动态数组-62.不同路径

一、题目 二、思路解析 1.思路&#xff1a; 对于找到目的地它的来源主要来源于目的地的上一格和目的地的左一格 2.常用方法&#xff1a; 无 3.核心逻辑&#xff1a; 1.处理边界&#xff1a; a.只向右移动&#xff0c;至始至终只有一条路径 for(int i0;i<m;i){dp[i][0]1; } …...

Y3编辑器教程5:触发器进阶使用(镜头、UI、表格、函数库、排行榜、游戏不同步)

文章目录 一、游戏声音设计二、 游戏镜头设计2.1 镜头的基本参数2.2 镜头时间轴动画 三、界面编辑3.1 界面编辑器设置3.2 添加按钮事件3.3 触发编写 四、 表格编辑器&#xff08;实现对话UI&#xff09;4.1 一维表和多维表4.2 数据验证、搜索、保存与撤销4.3 Excel导入导出4.4 …...

19c补丁后oracle属主变化,导致不能识别磁盘组

补丁后服务器重启&#xff0c;数据库再次无法启动 ORA01017: invalid username/password; logon denied Oracle 19c 在打上 19.23 或以上补丁版本后&#xff0c;存在与用户组权限相关的问题。具体表现为&#xff0c;Oracle 实例的运行用户&#xff08;oracle&#xff09;和集…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

如何将联系人从 iPhone 转移到 Android

从 iPhone 换到 Android 手机时&#xff0c;你可能需要保留重要的数据&#xff0c;例如通讯录。好在&#xff0c;将通讯录从 iPhone 转移到 Android 手机非常简单&#xff0c;你可以从本文中学习 6 种可靠的方法&#xff0c;确保随时保持连接&#xff0c;不错过任何信息。 第 1…...

相机Camera日志分析之三十一:高通Camx HAL十种流程基础分析关键字汇总(后续持续更新中)

【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:有对最普通的场景进行各个日志注释讲解,但相机场景太多,日志差异也巨大。后面将展示各种场景下的日志。 通过notepad++打开场景下的日志,通过下列分类关键字搜索,即可清晰的分析不同场景的相机运行流程差异…...

Spring AI 入门:Java 开发者的生成式 AI 实践之路

一、Spring AI 简介 在人工智能技术快速迭代的今天&#xff0c;Spring AI 作为 Spring 生态系统的新生力量&#xff0c;正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务&#xff08;如 OpenAI、Anthropic&#xff09;的无缝对接&…...

实现弹窗随键盘上移居中

实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

C++.OpenGL (14/64)多光源(Multiple Lights)

多光源(Multiple Lights) 多光源渲染技术概览 #mermaid-svg-3L5e5gGn76TNh7Lq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3L5e5gGn76TNh7Lq .error-icon{fill:#552222;}#mermaid-svg-3L5e5gGn76TNh7Lq .erro…...

mac 安装homebrew (nvm 及git)

mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用&#xff1a; 方法一&#xff1a;使用 Homebrew 安装 Git&#xff08;推荐&#xff09; 步骤如下&#xff1a;打开终端&#xff08;Terminal.app&#xff09; 1.安装 Homebrew…...

深入理解Optional:处理空指针异常

1. 使用Optional处理可能为空的集合 在Java开发中&#xff0c;集合判空是一个常见但容易出错的场景。传统方式虽然可行&#xff0c;但存在一些潜在问题&#xff1a; // 传统判空方式 if (!CollectionUtils.isEmpty(userInfoList)) {for (UserInfo userInfo : userInfoList) {…...