【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;
 } 
相关推荐
Je1lyfish6 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#3 - QueryExecution
linux·c语言·开发语言·数据结构·数据库·c++·算法
Brilliantwxx6 小时前
【C++】 vector(代码实现+坑点讲解)
开发语言·c++·笔记·算法
叼烟扛炮7 小时前
C++第三讲:类和对象(中)
开发语言·c++·类和对象
KuaCpp7 小时前
C++新特性学习
c++·学习
墨染千千秋8 小时前
C/C++ Keywords
c语言·c++
ximu_polaris8 小时前
设计模式(C++)-行为型模式-中介者模式
c++·设计模式·中介者模式
CSCN新手听安9 小时前
【Qt】Qt窗口(八)QFontDialog字体对话框,QInputDialog输入对话框的使用,小结
开发语言·c++·qt
tumu_C10 小时前
用std::function减缓C++模板代码膨胀和编译压力的一个场景
开发语言·c++
Hical6111 小时前
C++17 实战心得:那些真正改变我写代码方式的特性
c++
Hical6111 小时前
实测:C++20 协程 vs Go Gin vs Rust Actix,谁的 Web 性能更强?
c++