【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;
 } 
相关推荐
charlie1145141914 小时前
如何快速在 VS2026 上使用 C++ 模块 — 完整上手指南
开发语言·c++·笔记·学习·现代c++
报错小能手5 小时前
STL_unordered_map
开发语言·c++·哈希算法
历程里程碑5 小时前
C++ 9 stack_queue:数据结构的核心奥秘
java·开发语言·数据结构·c++·windows·笔记·算法
仰泳的熊猫6 小时前
1108 Finding Average
数据结构·c++·算法·pat考试
炽烈小老头6 小时前
【每天学习一点算法 2025/12/15】环形链表
学习·算法·链表
AA陈超6 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
wxin_VXbishe6 小时前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php
ULTRA??7 小时前
归并排序算法实现,kotlin,c++,python
c++·python·kotlin
deng-c-f7 小时前
C/C++内置库函数(5):值/引用传递、移动构造、以及常用的构造技巧
开发语言·c++
qq_310658517 小时前
mediasoup源码走读(十)——producer
服务器·c++·音视频