【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;
 } 
相关推荐
ysa0510305 分钟前
迷宫传送[最短路径]
c++·笔记·算法·深度优先
左左右右左右摇晃6 分钟前
数据结构——链表
数据结构·链表
计算机安禾6 分钟前
【数据结构与算法】第5篇:线性表(一):顺序表(ArrayList)的实现与应用
c语言·开发语言·数据结构·c++·算法·visual studio code·visual studio
仰泳的熊猫8 分钟前
题目2584:蓝桥杯2020年第十一届省赛真题-数字三角形
数据结构·c++·算法·蓝桥杯
2401_8649592810 分钟前
C++与Python混合编程实战
开发语言·c++·算法
2501_9454248012 分钟前
C++与硬件交互编程
开发语言·c++·算法
2301_8184190112 分钟前
C++中的表达式模板
开发语言·c++·算法
Roselind_Yi13 分钟前
排查Visual C++堆损坏(HEAP CORRUPTION)错误:从报错到解决的完整复盘
java·开发语言·c++·spring·bug·学习方法·远程工作
cm65432028 分钟前
C++中的原型模式变体
开发语言·c++·算法
☆56632 分钟前
C++中的策略模式进阶
开发语言·c++·算法