STL——stack容器、queue容器、list容器

news2024/10/6 4:13:59

初识STL

  • **stack容器**
    • **stack容器——基本概念**
    • **stack容器——常用接口**
  • **queue容器**
    • **queue容器——基本概念**
    • **queue容器——常用接口**
  • **list容器**
    • **list容器基本概念**
    • **list容器——构造函数**
    • **list容器——赋值和交换**
    • **List容器——大小操作**
    • **list容器——插入和删除**
    • **list容器——数据存取**
    • **list容器——反转和排序**
    • **list容器——排序案例**

stack容器

stack容器——基本概念

就是栈
栈不允许有遍历行为
是先进后出的数据结构
在这里插入图片描述

stack容器——常用接口

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
using namespace std;

//栈stack容器

void test01()
{
	//特点:符合先进后出数据结构
	stack<int>s;

	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	cout << "栈的大小:" << s.size() << endl;
	//只有栈不为空,查看栈顶,并且执行出栈操作
	while (!s.empty())
	{
		//查看栈顶元素
		cout << "栈顶元素为:" << s.top() << endl;
		//出栈
		s.pop();
	}
	cout << "栈的大小:" << s.size() << endl;
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

queue容器

queue容器——基本概念

是先进先出的数据结构
在这里插入图片描述
**队列容器允许从一端新增元素,从另一端移除元素
队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
队列中进数据称为—入队push
队列中出数据称为—出队pop
**
在这里插入图片描述

queue容器——常用接口

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <string>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

//队列 Queue
void test01()
{
	//创建队列
	queue<Person>q;

	//准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 30);
	Person p3("猪八戒", 30);
	Person p4("沙僧", 30);

	//入队
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

	cout << "队列的大小为:" << q.size() << endl;

	//判断只要队列不为空,查看对头,查看队尾,出队
	while (!q.empty())
	{
		//查看对头元素
		cout << "对头元素 --- 姓名:" << q.front().m_Name << " 对头元素 --- 年龄:" << q.front().m_Age << endl;
		//查看对尾元素 
		cout << "对尾元素 --- 姓名:" << q.back().m_Name << " 对尾元素 --- 年龄:" << q.back().m_Age << endl;
		q.pop();
	}
	cout << "队列的大小为:" << q.size() << endl;
}
int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

list容器

list容器基本概念

在这里插入图片描述
在STL中这个链表是双向链表
在这里插入图片描述
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点∶
采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list的缺点:
链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

list和vector是经常被使用的容器

list容器——构造函数

在这里插入图片描述


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;

void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//加上const防止修改代码
		//容器中的数据不可以修改了
		//*it = 100; err
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	list<int>L1;//默认构造
	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历容器
	printList(L1);

	//区间方式构造
	list<int>L2(L1.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int>L3(10, 1000);
	printList(L3);

	//n个elem
	list<int>L4(L3);
	printList(L4);
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

list容器——赋值和交换

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;
	
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	list<int>L2;
	L2 = L1;
	printList(L2);

	list<int>L3;
	L3.assign(L2.begin(), L2.end());
	printList(L3);

	list<int>L4;
	L4.assign(10, 100);
	printList(L4);
}

//交换
void test02()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 100);
	cout << "交换前: " << endl;
	printList(L1);
	printList(L2);

	L1.swap(L2);
	cout << "交换后:" << endl;
	printList(L1);
	printList(L2);
}

int main()
{
	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

List容器——大小操作

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	//判断容器是否为空
	if (L1.empty())
	{
		cout << "L1为空" << endl;
	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的元素个数为:" << L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10,10000);
	printList(L1);

	L1.resize(2);
	printList(L1);
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

list容器——插入和删除

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L;
	
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);

	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	
	printList(L);

	//尾删
	L.pop_back();
	printList(L);

	//头删
	L.pop_front();
	printList(L);

	//insert插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	printList(L);

	//list容器是双向迭代器  不能跳跃移动
	//L.insert(L.begin() + 1, 100000);//err
	//printList(L);

	//删除
	it = L.begin();
	L.erase(++it);
	printList(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	printList(L);
	L.remove(10000);//按值删除  删除所有的10000
	printList(L);

	//清空
	L.clear();
	printList(L);
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}


在这里插入图片描述

list容器——数据存取

list容器底层是链表 存储的不是连续的空间 不支持随机访问 不能用[]或者at直接进行访问

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//L1[0]不可以用 []访问list容器中的元素
	//L1[0];err
	//L1.at(0);err
	//原因是list本质是链表,不是用连续的线性空间存储的,迭代器也是不支持随机访问的

	cout << "第一个元素为: " << L1.front() << endl;
	cout << "最后一个元素为:" << L1.back() << endl;

	//验证迭代器是不支持随机访问的
	list<int>::iterator it = L1.begin();
	it++;//right 支持双向++  --
	it--;//right
	//it = it + 1;//err
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

list容器——反转和排序

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;

	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);

	cout << "反转前:" << endl;
	printList(L1);

	//反转
	L1.reverse();
	cout << "反转后:" << endl;
	printList(L1);
}

//如果返回为true,则让两个参数进行交换,如果返回false则不动
bool myCompare(int v1, int v2)
{
	//降序  就让第一个数 > 第二个数
	return v1 > v2;
}

//排序
void test02()
{
	list<int>L1;

	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);
	
	//排序
	cout << "排序前: " << endl;
	printList(L1);

	//所有不支持随机访问迭代器的容器,不可以用标准的算法
	//不支持随机访问迭代器的容器,内部会提供对应的一些算法
	//sort(L1.begin(),L1.end());//err
	L1.sort();//默认排序规则 从小到大 升序
	cout << "排序后: " << endl;
	printList(L1);

	L1.sort(myCompare);
	printList(L1);
}

int main()
{
	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

list容器——排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高排序
规则:按照年龄进行升序,如果年龄相同按照身高进行降序

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <string>
using namespace std;

class Person
{
public:
	Person(string name,int age,int height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	string m_Name;
	int m_Age;
	int m_Height;
};

//指定排序规则
bool comparePerson(Person &p1,Person &p2)
{
	if (p1.m_Age == p2.m_Age)
	{
		//年龄相同  按照身高降序
		return p1.m_Height > p2.m_Height;
	}
	else
	{
		//按照年龄  升序
		return p1.m_Age < p2.m_Age;
	}
}

void test01()
{
	list<Person>L1;

	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("关羽", 35, 160);
	Person p6("张飞", 35, 200);

	L1.push_back(p1);
	L1.push_back(p2);
	L1.push_back(p3);
	L1.push_back(p4);
	L1.push_back(p5);
	L1.push_back(p6);

	for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
	}

	//排序
	cout << "-----------------------------" << endl;
	cout << "排序后:" << endl;

	L1.sort(comparePerson);
	for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
	}
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

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

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

相关文章

碳排放预测模型 | Python实现基于机器回归分析的碳排放预测模型——数据可视化和探索

文章目录 效果一览文章概述研究内容环境准备源码设计学习总结参考资料效果一览 文章概述 碳排放预测模型 | Python实现基于机器回归分析的碳排放预测模型——数据可视化和探索 目标是测试所选特征对分析的重要性,检测异常值的存在并准备数据以供进一步分析。 </

PCA主成分分析

PCA降维算法 目前图像特征的提取主要有两种方法&#xff1a;传统图像特征提取方法 和 深度学习方法。 传统的特征提取方法&#xff1a;基于图像本身的特征进行提取&#xff08;PCA&#xff09;&#xff1b;深度学习方法&#xff1a;基于样本自动训练出区分图像的特征分类器&a…

【算法与数据结构】19、LeetCode删除链表的倒数第 N 个结点

文章目录 一、题目二、双指针法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、双指针法 思路分析&#xff1a;这道题使用双指针一次遍历就能删除目标节点。快慢指针同一位置出发&#xff08;虚节点&#x…

博采众长!我全都要!Allen AI推出集成主流大语言模型的LLM-BLENDER框架

深度学习自然语言处理 原创作者&#xff1a;wkk 随着大语言模型(LLM)的迅速发展&#xff0c;众多开源的LLM性能参差不齐。今天分享的是由Allen AI实验室联合南加大和浙江大学的最新研究论文&#xff0c;发表在ACL上。本文提出了一个集成框架(LLM-BLENDER)&#xff0c;旨在通过利…

6.11下周黄金行情分析及开盘多空交易策略

近期有哪些消息面影响黄金走势&#xff1f;下周黄金多空该如何研判&#xff1f; ​黄金消息面解析&#xff1a;金价周五(6月8日)收低&#xff0c;但在美元整体走软的支撑下&#xff0c;本周录得连续第二周上升。美市尾盘&#xff0c;现货黄金收报1960.83美元/盎司&#xff0c;…

分布式项目 16 购物车系统,dubbo框架(重点是拦截器),优化userId,配合拦截器

01.创建jt-cart项目 第一步&#xff1a; 第二步&#xff1a; 第三步&#xff1a; 第四步&#xff1a; 在pom.xml文件中添加jt-common的依赖&#xff0c;如图所示&#xff1a; 第五步&#xff1a; 添加插件 <build> <plugins> <!--跳过测试类打包 --> <…

支撑臂爬楼履带车实现爬楼梯功能

1. 功能说明 本文示例将实现R018a支撑臂爬楼履带车爬楼梯的功能。 2. 结构说明 支撑臂爬楼履带车主要是由 小型平行履带底盘 和2个 舵机关节模组 组成。 3. 电子硬件 在这个示例中&#xff0c;我们采用了以下硬件&#xff0c;请大家参考&#xff1a; 主控板 Basra主控板&#…

MIT 6.S081 (BOOK-RISCV-REV1)教材第二章内容

MIT 6.S081 第二章内容 引言操作系统架构抽象系统资源用户态&#xff0c;核心态&#xff0c;以及系统调用内核组织代码&#xff08;XV6架构篇&#xff09;进程概述代码&#xff08;启动XV6和第一个进程&#xff09;真实世界 引言 MIT 6.S081 2020 操作系统 本文为MIT 6.S081课…

用饭店来形象比喻线程池的工作原理

一、线程池解决的问题&#xff1f; 使用线程池主要解决在程序中频繁创建和销毁线程导致的资源浪费&#xff0c;线程池可以维护一定量的线程来执行所需要的任务&#xff0c;维护的线程也可以重复使用。 二、用形象的饭店来解释工作原理 线程池就相当于一家饭店&#xff0c; 任…

SpringBoot框架的学生宿舍管理系统

项目介绍 主要功能&#xff1a; 管理员登录权限&#xff1a; ①学生管理&#xff1a;根据编号姓名搜索、可以新增修改删除、导入导出 ②楼宇管理&#xff1a;根据楼宇搜索、可以新增修改删除、导入导出 ③宿舍管理&#xff1a;根据宿舍编号搜索、可以新增修改删除、导入导出 ④…

HCIA-RS实验-配置FTP 业务

FTP简单说明 FTP&#xff08;File Transfer Protocol&#xff09;是一种用于文件传输的协议&#xff0c;可以在计算机之间进行文件的上传和下载。FTP使用客户端-服务器模型&#xff0c;客户端通过FTP客户端软件连接到服务器端的FTP服务端口&#xff0c;进行文件传输和管理。 F…

在fpga上开发音视频是一种什么体验?

前言&#xff1a; 今天周末回公司解决了解码播放问题&#xff0c;最近周末也没啥事情&#xff0c;一般周六都会过去公司学习音视频开源项目(过去公司&#xff0c;主要是住的近&#xff0c;所以很方便&#xff01;)&#xff0c;待在家里也是无聊&#xff0c;所以就回去看开源项目…

硬件设计电源系列文章-LDO设计

文章目录 概要整体架构流程技术名词解释技术细节小结 概要 本文主要分享LDO的相关设计&#xff0c;尤其是LDO的并联设计 整体架构流程 提示&#xff1a;这里可以添加技术整体架构 主要是讲述LDO的并联&#xff1b;并联以增加输出驱动能力&#xff0c;其具体框架如下&#x…

详解Ribbon

目录 1.概述 2.使用 2.1.引入 2.2.启用 2.3.切换负载均衡算法 3.负载均衡源码分析 3.1.接口 3.2.抽象类 3.3.选择服务器 3.4.原子性 4.自定义负载均衡算法 1.概述 Ribbon是Netflix开源的一个客户端负载均衡库&#xff0c;也是Spring Cloud Netflix项目的核心组件之…

常见网络服务器并发模型

近些年&#xff0c;随着互联网的大发展&#xff0c;高并发服务器技术也快速进步&#xff0c;从简单的循环服务器模型处理少量网络并发请求&#xff0c;演进到解决C10K&#xff0c;C10M问题的高并发服务器模型。本文主要以TCP为例&#xff0c;总结了几种常见的网络服务器模型的实…

巧用文件批量改名高手删除子文件夹一例

比如有很多商品文件夹&#xff0c;里面又分出主图、细节图等&#xff0c;现在因工作需要把主图、细节图这些子文件夹去掉&#xff0c;把子文件夹里面的文件放在商品名称的父文件夹中&#xff0c;如图&#xff1a; 打开主图文件夹&#xff0c;我们可以看到文件名结构为数字编号的…

Git学习 - 2023-06-08

2023暑期学习 Git基础Git Fetch VS Git Pullgit pull --rebase VS git pull几种merge的方法Fork VS Clone CS Branch如何把master的内容更新到分支上详尽介绍 git fetch VS git pull其他命令 Git基础 git branch branch-name # 创建一个新的分支git checkout branch-name # 切…

Golang | Web开发之Gin框架快速入门基础实践

欢迎关注「全栈工程师修炼指南」公众号 点击 &#x1f447; 下方卡片 即可关注我哟! 设为「星标⭐」每天带你 基础入门 到 进阶实践 再到 放弃学习&#xff01; 专注 企业运维实践、网络安全、系统运维、应用开发、物联网实战、全栈文章 等知识分享 “ 花开堪折直须折&#xf…

Learning C++ No.30 【lambda表达式实战】

引言&#xff1a; 北京时间&#xff1a;2023/6/9/9:13&#xff0c;今天8:15起床&#xff0c;可能是最近课非常少&#xff0c;导致写博客没什么压力&#xff0c;什么时间都能写&#xff0c;导致7点起不来&#xff0c;哈哈哈&#xff0c;习惯睡懒觉了&#xff0c;但是问题不大&a…

【二十七】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能

springboot篇章整体栏目&#xff1a; 【一】springboot整合swagger&#xff08;超详细 【二】springboot整合swagger&#xff08;自定义&#xff09;&#xff08;超详细&#xff09; 【三】springboot整合token&#xff08;超详细&#xff09; 【四】springboot整合mybatis…