STL值list

news2024/9/25 11:21:20

 list容器

头文件:#include<list>

- list是一个双向链表容器,可高效地进行插入删除元素

- list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符

注:list使用迭代器访问数据时可以一步一步走自增自减(即it++)不允许跨太多步去访问元素

        list容器是可以进行遍历的,即进行数据访问时不会进行删除操作

list头尾的添加移除操作

- list.push back(elem); //在容器尾部加入一个元素

- list.pop _back(); //删除容器中最后一个元素

- list.push front(elem); //在容器开头插入一个元素

- list.pop_front(); //从容器开头移除第一个元素

list的数据存取

 list.front();//返回第一个元素。

list.back(); //返回最后一个元素。

list与迭代器

- list 容器的迭代器是“双向迭代器”:双向迭代器从两个方向读写容器。除了提供前向迭代器的全部操作之外,双向迭代器还提供前置和后置的自减运算。

- list.begin(); //返回容器中第一个元素的选代器

- list.end(); //返回容器中最后一个元素之后的迭代器

- list.rbegin(); //返回容器中倒数第一个元素的选代器

- list.rend(); //返回容器中倒数最后一个元素的后面的选代器

示例1:list与正向迭代器

#include<iostream>
#include<list>
using namespace std;
int main() {
	list<int> lst;
	lst.push_back(10);
	lst.push_front(20);

	list<int>::iterator it;
	for (it = lst.begin(); it != lst.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;


	int a = lst.front();
	cout << "front:" << a << endl;

	int b = lst.back();
	cout << "back:" << b << endl;
	
	//修改容器末尾和首部的值
	lst.front() = 100;
	lst.back() = 200;
	a = lst.front();
	cout << "front:" << a << endl;

	b = lst.back();
	cout << "back:" << b << endl;
}

示例2:list与反向迭代器

#include<iostream>
#include<list>
using namespace std;
int main() {
	list<int> lst;
	lst.push_back(10);
	lst.push_front(20);
	list<int>::reverse_iterator it1;
	for (it1 = lst.rbegin(); it1 != lst.rend(); it1++) {
		cout << *it1 << ' ';
	}
	cout << endl;
	
}

list对象的带参数构造

- list(n,elem); //构造函数将n个elem拷贝给本身

- list(beg,end); //构造函数将[beg,end)区间中的元素拷贝给本身

- list(const list &lst); //拷贝构造函数。

#include<iostream>
#include<list>
using namespace std;
int main() {
	list<int>::iterator it;
	list<int> lst(3, 5);
	for (it = lst.begin(); it != lst.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;

	list<int> lst2(lst.begin(),lst.end());
	//list<int> lst2(lst.begin(), lst.begin()+5);错误
	for (it = lst2.begin(); it != lst2.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;

	int a[] = { 1,2,3,4,5 };
	list<int> lst3(a, a + 5);
	for (it = lst3.begin(); it != lst3.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;

	list<int> lst4(lst);
	for (it = lst4.begin(); it != lst4.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;
}

list的赋值

- list.assign(beg,end); //将[beg,end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。

- list.assign(n,elem); //将n个elem拷贝赋值给本身

- list& operator=(const list &lst); //重载等号操作符

- list.swap(lst); // 将lst与本身的元素互换。

示例:

#include<iostream>
#include<list>
using namespace std;
int main() {
	
	list<int> lst1,lst2;
	list<int> lst3 = { 1,2,3,4,5 };
	list<int>::iterator it = lst3.end();
	list<int>::iterator it2;
	lst1.assign(lst3.begin(), it);
	for (it2 = lst1.begin(); it2 != lst1.end(); it2++) {
		cout << *it2 << ' ';
	}
	cout << endl;
	cout << endl;

	lst2.assign(3, 5);
	for (it2 = lst2.begin(); it2 != lst2.end(); it2++) {
		cout << *it2 << ' ';
	}
	cout << endl;
	cout << endl;

	lst2 = lst1;
	for (it2 = lst1.begin(); it2 != lst1.end(); it2++) {
		cout << *it2 << ' ';
	}
	cout << endl;
	cout << endl;

	lst1.swap(lst2);
	for (it2 = lst1.begin(); it2 != lst1.end(); it2++) {
		cout << *it2 << ' ';
	}
	cout << endl;
	for (it2 = lst2.begin(); it2 != lst2.end(); it2++) {
		cout << *it2 << ' ';
	}
	cout << endl;

}

list的大小

- list.size(); //返回容器中元素的个数

- list.empty(); //判断容器是否为空

- list.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

- list.resize(num,elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

list的插入

- list.insert(pos,elem)//在pos位置插入一个elem元素的拷贝,返回新数据的位置

- list.insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值

- list.insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值

注:list进行数据的插入时是没有空间的释放和位置的移动,因此不会出现迭代器失效的情况

list的删除

- list.clear(); /X移除容器的所有数据

- list.erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。

- list.erase(pos); //删除pos位置的数据,返回下一个数据的位置。

- lst.remove(elem); //删除容器中所有与elem值匹配的元素。

list的反序排列

- lst.reverse(); //反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素

Iist迭代器失效

- 删除结点导致迭代器失效

删除即是将相应数据位置的元素地址释放掉,即返还给内部系统,在某些编译器中,该被释放的地址是可以进行访问并有明确复制(系统内部赋值),该系统地址是没有访问权限的,俗称野指针

示例:

#include<iostream>
#include<list>
using namespace std;
int main() {
	list<int> lst = { 1,2,1,4,5 };
	list<int>::iterator it;
	for (it = lst.begin(); it != lst.end();) {
		if (*it == 1) {
			it = lst.erase(it);
		}
		else it++;
	}
	for (it = lst.begin(); it != lst.end(); it++) cout << *it<<' ';
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2138149.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

誉龙视音频综合管理平台 RelMedia/FindById SQL注入漏洞复现

0x01 产品简介 誉龙视音频综合管理平台是深圳誉龙数字技术有限公司基于多年的技术沉淀和项目经验,自主研发的集视音频记录、传输、管理于一体的综合解决方案。该平台支持国产化操作系统和Windows操作系统,能够接入多种类型的记录仪,实现高清实时图传、双向语音对讲、AI应用…

CTFHub技能树-SQL注入-整数型注入

一、手动注入 思路&#xff1a;注入点->库->表->列->数据 首先使用order by探测有几列 http://challenge-215beae2f0b99b12.sandbox.ctfhub.com:10800/?id1 order by 2 我们发现order by 2 的时候有回显&#xff0c;到了order by 3 的时候就没有回显了&#xf…

npm install报错,gyp verb `which` failed Error: not found: python

主要错误 gyp verb which failed Error: not found: python2 gyp ERR! configure error gyp ERR! stack Error: Cant find Python executable "python", you can set the PYTHON env variable. npm ERR! node-sass4.14.1 postinstall: node scripts/build.js 全部错…

Apisix离线安装

上传离线包 #ll apisix-3.2.2-0.el7.x86_64.rpm apisix-base-1.21.4.1.8-0.el7.x86_64.rpm apisix-dashboard-3.0.1-0.el7.x86_64.rpm cyrus-sasl-2.1.26-24.el7_9.x86_64.rpm cyrus-sasl-devel-2.1.26-24.el7_9.x86_64.rpm cyrus-sasl-gssapi-2.1.26-24.el7_9.x86_64.rpm cyr…

【H2O2|全栈】关于CSS(1)CSS基础(一)

目录 CSS基础知识 前言 准备工作 啥是CSS&#xff1f; 如何引用CSS&#xff1f; 选择器 通配符选择器 类名&#xff08;class&#xff09;选择器 id选择器 CSS解析顺序&#xff08;优先级&#xff09; 常见CSS标签&#xff08;一&#xff09; 字体属性 font-style…

spring模块(六)spring event事件(3)广播与异步问题

发布事件和监听器之间默认是同步的&#xff1b;监听器则是广播形式。demo&#xff1a; event&#xff1a; package com.listener.demo.event;import com.listener.demo.dto.UserLogDTO; import org.springframework.context.ApplicationEvent;public class MyLogEvent extends…

C#命令行参数解析库System.CommandLine介绍

命令行参数 平常在日常的开发过程中&#xff0c;会经常用到命令行工具。如cmd下的各种命令。 以下为sc命令执行后的截图&#xff0c;可以看到&#xff0c;由于没有输入任何附带参数&#xff0c;所以程序并未执行任何操作&#xff0c;只是输出了描述和用法。 系统在创建一个新…

电脑怎么恢复原来的ip地址:全面指南与注意事项

在使用电脑连接网络时&#xff0c;有时可能会因为某些原因需要更改IP地址。然而&#xff0c;在某些情况下&#xff0c;我们可能希望将电脑的IP地址恢复到原来的设置。本文将详细介绍如何恢复电脑原来的IP地址&#xff0c;并提供一些注意事项。 一、了解IP地址的分配方式 在恢复…

Linux-LVM逻辑卷管理

一、背景 Linux运维过程中大家有没有想过生产环境服务器磁盘分区如果数据量越来越膨胀(这些都是重要数据&#xff0c;不能删除)&#xff0c;那么此时如何来应对这个问题呢? 既要不影响正在运行的程序&#xff0c;同时也不能中断关机等操作。 这么一想就很蛋疼了。假设你运行…

力扣-96.不同的二叉搜索树 题目详解

题目: 给你一个整数 n &#xff0c;求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种&#xff1f;返回满足题意的二叉搜索树的种数。 二叉搜索树介绍: 二叉搜索树是一个有序树&#xff1a; 若它的左子树不空&#xff0c;则左子树上所有结点的值均小于它…

凸优化学习(3)——对偶方法、KKT条件、ADMM

&#x1f345; 写在前面 &#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;这里是hyk写算法了吗&#xff0c;一枚致力于学习算法和人工智能领域的小菜鸟。 &#x1f50e;个人主页&#xff1a;主页链接&#xff08;欢迎各位大佬光临指导&#xff09; ⭐️近…

【pyenv】pyenv安装版本超时的解决方案

目录 1、现象 2、分析现象 3、手动下载所需版本 4、存放到指定路径 5、重新安装 6、pip失败&#xff08;做个记录&#xff0c;未找到原因&#xff09; 7、方法二修改环境变量方法 7.1 设置环境变量 7.2 更新 7.3 安装即可 8、方法三修改XML文件 前言&#xff1a;研…

【Android】Room—数据库的基本操作

引言 在Android开发中&#xff0c;数据持久化是一个不可或缺的部分。随着应用的复杂度增加&#xff0c;选择合适的数据存储方式变得尤为重要。Room数据库作为Android Jetpack架构组件之一&#xff0c;提供了一种抽象层&#xff0c;使得开发者能够以更简洁、更安全的方式操作SQ…

PCIe进阶之TL:First/Last DW Byte Enables Rules Traffic Class Field

1 First/Last DW Byte Enables Rules & Attributes Field 1.1 First/Last DW Byte Enables Rules Byte Enable 包含在 Memory、I/O 和 Configuration Request 中。本文定义了相应的规则。Byte Enable 位于 header 的 byte 7 。对于 TH 字段值为 1 的 Memory Read Request…

【算法篇】哈希类(笔记)

目录 一、常见的三种哈希结构 二、LeetCode 练习 1. 有效的字母异位词 2. 两个数组的交集 3. 快乐数 4. 两数之和 5. 四数相加II 6. 赎金信 7. 三数之和 8. 四数之和 一、常见的三种哈希结构 当想使用哈希法来解决问题的时候&#xff0c;一般会选择如下三种数据…

java中的注解原理是什么?

Java中的注解&#xff08;Annotations&#xff09;是一种用于提供元数据的机制。它可以通过在代码中添加注解的形式&#xff0c;将一些额外的信息嵌入到代码里。注解本质上不会改变程序的实际逻辑行为&#xff0c;但是可以帮助开发工具、编译器、框架等获取这些元数据&#xff…

短信验证码倒计时 (直接复制即可使用) vue3

需求&#xff1a; 要实现一个获取验证码的需求&#xff0c;点击获取验证码60秒内不可以重复点击&#xff0c;方式有两种可以直接复制使用&#xff1b; 效果图 实现方案 方案1 (单个文件内使用比较推荐) <el-button :disabled"codeDisabled" click.stop"h…

SQL进阶的技巧:如何实现某列的累计乘积?

0 场景描述 在做数据处理的时候,尤其是复利累积的时候,有时候会有这样一场景,通过某种条件找到一列数据[X1,X2,X3...Xn],然后想要求y=X1X2X3...Xn。下面给出一个具体案例来详细解释这一问题,如下图所示,每个组的name值只有2个(2个A/B/C),当name=A or C时,price为value…

鸡蛋检测系统源码分享

鸡蛋检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vision …

python webapi上传文件

一、安装 pip install Flask 二、 编写上传文件接口webapi.py http://127.0.0.1:5000/upload from flask import Flask,request from werkzeug.utils import secure_filename import uuidapp Flask(__name__)app.route(/) def hello_world():return Hello, World!app.post(…