当前位置: 首页 > news >正文

STL stack 和 queue

文章目录

  • 一、stack 类和 queue 类的模拟实现

在这里插入图片描述
在这里插入图片描述

stack 只允许在一端进行插入删除,是一个后进先出(LIFO)的结构,可以存储任意类型

queue 只允许在一端进行插入,另一端进行删除,是一个先进先出(FIFO)的结构,可以存储任意类型

模板参数 T 表示存储元素的类型,Container 表示底层使用的容器

一、stack 类和 queue 类的模拟实现

stack 类和 queue 类常用接口模拟实现:

//test.cpp
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <list>using namespace std;#include "stack.h"
#include "queue.h"int main()
{starrycat::test_stack();starrycat::test_queue();return 0;
}//stack.h
#pragma oncenamespace starrycat
{template<class T, class Container = vector<T>>class stack{public:void push(const T& x){_con.push_back(x);}void pop(){_con.pop_back();}const T& top() const{return _con.back();}bool empty() const{return _con.empty();}size_t size() const{return _con.size();}private:Container _con;};void test_stack(){//stack<int> s;stack<int, list<int>> s;s.push(1);s.push(2);s.push(3);s.push(4);while (!s.empty()){cout << s.top() << " ";s.pop();}cout << endl;}
}//queue.h
#pragma oncenamespace starrycat
{template<class T, class Container = list<T>>class queue{public:void push(const T& x){_con.push_back(x);}void pop(){_con.pop_front();}const T& front() const{return _con.front();}const T& back() const{return _con.back();}bool empty() const{return _con.empty();}size_t size() const{return _con.size();}private:Container _con;};void test_queue(){queue<int> q;q.push(1);q.push(2);q.push(3);q.push(4);while (!q.empty()){cout << q.front() << " ";q.pop();}cout << endl;}
}
http://www.lryc.cn/news/166184.html

相关文章:

  • 阈值回归模型(Threshold Regression Model)及R实现
  • 无人机通信协议MAVLink简介
  • 【办公自动化】用Python批量从上市公司年报中获取主要业务信息
  • 【sizeof()的使用方式】简洁明了初识C语言
  • 10. 正则表达式匹配
  • [Unity]GPU Instancing 无效的原因
  • 2023 年前端编程 NodeJs 包管理工具 npm 安装和使用详细介绍
  • ptmalloc源码分析 - Top chunk的扩容函数sysmalloc实现(09)
  • [BJDCTF2020]ZJCTF,不过如此 preg_replace /e模式漏洞
  • C++day4
  • 【LeetCode-简单题】541. 反转字符串 II
  • Linux服务使用宝塔面板搭建网站,并发布公网访问
  • 代码随想录算法训练营19期第48天
  • 【校招VIP】产品项目分析之竞品分析
  • 【JavaScript内置对象】Date对象,从零开始
  • idea启动缓慢解决办法
  • App测试中ios和Android有哪些区别呢?
  • Flink JobManager的高可用配置
  • 为什么Token手动添加到请求的Header中,通常使用“Authorization“字段?
  • 国际生态数据获取网络
  • 爬虫逆向实战(34)-某视综数据(MD5、AES)
  • 数据分析三剑客之Matplotlib
  • Python Opencv实践 - LBP特征提取
  • Docker 搭建Redis Cluster 集群
  • 解决谷歌浏览器会http网站自动变成https的问题
  • go小知识2
  • zabbix监控H3C设备
  • 国产化改造之Mysql迁移方案:Mysql Galera Cluster
  • bootstrap表单类型
  • 第一章 SQL Server 数据库部署