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

拈花ヽ惹草 提交于 2020-02-08 08:26:49

头文件 < 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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!