【STL】stack栈容器与list链表容器

目录

1.栈stack

2.list链表容器


1.栈stack

栈具有先进后出的特性,最先进入的数据压在最底下,最后出来

2.list链表容器

list链表容器是一种双向链表,两端都可插入与删除,是双向访问迭代器,与vertor随机访问迭代器有不同的区别

reverse()函数可以将元素反转过来

cpp 复制代码
#include<iostream>
#include<list>//头文件 
#include<algorithm> 
using namespace std;
void printlist(list<int> &s)
{
	list<int>::iterator it=s.begin();
	for( ;it!=s.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
}
int main()
{
	list<int> s;
	s.push_back(10);//尾插 
	s.push_back(20);
	s.push_back(30);
	s.push_front(40);//头插 
	s.push_front(50);
	s.push_front(60);
	printlist(s);
	
	list<int>::iterator it=s.begin();
	it++;
	it++;
	//双向迭代器不支持 +2  
	//s.insert(s.begin()+2,3,100); 错误写法
	s.insert(it,3,100);//插入 
	printlist(s); 
	
	//STL提供的算法只支持随机访问迭代器,而list是双向访问迭代器,标准算法不支持 
	//sort(s.begin(),s.end()); 错误写法
	
	s.sort(); //链表类模板提供了sort() 
	printlist(s);
	
	s.reverse();// 将元素反转 
	printlist(s);
	return 0;
 } 
相关推荐
AC赳赳老秦3 小时前
采集行为合规自检:OpenClaw 自动校验 robots 协议与采集频率,规避违规采集风险
java·开发语言·c++·python·php·deepseek·openclaw
无名猿4 小时前
C++ 查找算法:std::find 与容器成员 find 的性能差距
c++·性能优化·stl·标准库
程序猿编码4 小时前
工业视觉多任务方案:可编程梯度信息视觉框架,训练评估 + C++ 部署完整实战
开发语言·c++·视觉·推理引擎
朝朝辞暮i5 小时前
C++ 第 21 课:struct —— 把一组相关数据打包在一起
开发语言·c++·算法
SHARK_pssm6 小时前
【C++——类和对象(下)】
开发语言·c++·经验分享·笔记
汉克老师7 小时前
GESP2026年9月认证C++二级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程
沫璃染墨7 小时前
《从零入门Linux系统篇(五十四):线程篇·七——互斥锁底层原理:从原子交换到线程竞争与锁实现》
linux·运维·服务器·开发语言·c++·驱动开发·系统架构
可乐ea9 小时前
GitHub Security Lab 开源 Fuzzing Taskflow:让 LLM 智能体自己跑完 C/C++ 模糊测试
jvm·c++·github·模糊测试·mcp·代码agent·智能体自动化
朝朝辞暮i9 小时前
C++ 第 13 课:值传递 —— 为什么函数里改了,外面却没变?
java·c++·算法
无忧.芙桃10 小时前
C++语言原理与实践(十):vector类的底层实现
开发语言·c++·算法