C++语法基础之栈和队列

头文件 < stack >

实例化

stack在内部默认使用std::deque存储数据,但是可以指定使用vector或者list存储数据
示例:

std::stack <int> numsInStack;
std::stack <double, vector <double>> doublesStackedInVec;//指定使用vector实现

可以使用一个栈初始化另一个

成员函数
  • push()
  • pop()
  • empty()
  • size()
  • top()
  • emplace()
  • swap()
    Exchanges the contents of the container adaptor by those of x. parameter:Another queue container adaptor of the same type (i.e., instantiated with the same template parameters, T and Container). Sizes may differ.

注意:不能修改栈中元素

队列

头文件 < queue >

实例化

默认使用deque存储数据,可设置为list,vector
实例化与初始化的方法与栈类似

成员函数
  • push()
  • pop() 删除队首元素
  • front()
  • back()
  • empty()
  • size()
  • emplace(), swap()
    队列不支持迭代器

优先级队列

实例化

可以指定元素类型、存储数据的集合类、二元谓词确定队首元素
默认std::less
实例化和初始化示例

priority_queue <int> numsInPrioQ;
priority_queue <int, deque <int>, greater<int>> numsInDescendingQ;
priority_queue <int> copyQ(numsInPrioQ);
成员函数
  • push()
  • pop()
  • top()
  • empty()
  • size()
  • emplace() swap()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值