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

浅谈 React Hooks

React Hooks 是 React 16.8 引入的一组 API&#xff0c;用于在函数组件中使用 state 和其他 React 特性&#xff08;例如生命周期方法、context 等&#xff09;。Hooks 通过简洁的函数接口&#xff0c;解决了状态与 UI 的高度解耦&#xff0c;通过函数式编程范式实现更灵活 Rea…...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

c++ 面试题(1)-----深度优先搜索(DFS)实现

操作系统&#xff1a;ubuntu22.04 IDE:Visual Studio Code 编程语言&#xff1a;C11 题目描述 地上有一个 m 行 n 列的方格&#xff0c;从坐标 [0,0] 起始。一个机器人可以从某一格移动到上下左右四个格子&#xff0c;但不能进入行坐标和列坐标的数位之和大于 k 的格子。 例…...

微信小程序 - 手机震动

一、界面 <button type"primary" bindtap"shortVibrate">短震动</button> <button type"primary" bindtap"longVibrate">长震动</button> 二、js逻辑代码 注&#xff1a;文档 https://developers.weixin.qq…...

页面渲染流程与性能优化

页面渲染流程与性能优化详解&#xff08;完整版&#xff09; 一、现代浏览器渲染流程&#xff08;详细说明&#xff09; 1. 构建DOM树 浏览器接收到HTML文档后&#xff0c;会逐步解析并构建DOM&#xff08;Document Object Model&#xff09;树。具体过程如下&#xff1a; (…...

HTML前端开发:JavaScript 常用事件详解

作为前端开发的核心&#xff0c;JavaScript 事件是用户与网页交互的基础。以下是常见事件的详细说明和用法示例&#xff1a; 1. onclick - 点击事件 当元素被单击时触发&#xff08;左键点击&#xff09; button.onclick function() {alert("按钮被点击了&#xff01;&…...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...

安卓基础(aar)

重新设置java21的环境&#xff0c;临时设置 $env:JAVA_HOME "D:\Android Studio\jbr" 查看当前环境变量 JAVA_HOME 的值 echo $env:JAVA_HOME 构建ARR文件 ./gradlew :private-lib:assembleRelease 目录是这样的&#xff1a; MyApp/ ├── app/ …...

【堆垛策略】设计方法

堆垛策略的设计是积木堆叠系统的核心&#xff0c;直接影响堆叠的稳定性、效率和容错能力。以下是分层次的堆垛策略设计方法&#xff0c;涵盖基础规则、优化算法和容错机制&#xff1a; 1. 基础堆垛规则 (1) 物理稳定性优先 重心原则&#xff1a; 大尺寸/重量积木在下&#xf…...