首页 > 编程语言 > 详细

C++ std::stack 基本用法

时间:2019-12-22 16:44:06      阅读:90      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string>
#include <stack>

// https://zh.cppreference.com/w/cpp/container/stack

// std::stack 类是容器适配器,它给予程序员栈的功能——特别是 FILO (先进后出)数据结构。
// 该类模板表现为底层容器的包装器——只提供特定函数集合。栈从被称作栈顶的容器尾部推弹元素。
// 标准容器 std::vector 、 std::deque 和 std::list 满足这些要求。
// 若不为特定的 stack 类特化指定容器类,则使用标准容器 std::deque 。

using namespace std;
int main()
{
	stack<int> sta({1,2,3});

	//////////////////////////////////////////////////////////////////////////

	sta.push(10); // 1 2 3 10
	sta.emplace(11); // 1 2 3 10 11

	int vTop = sta.top(); // 11 栈顶是最后一个进来的元素
	
	bool isEmpty = sta.empty();
	int size = sta.size();

	sta.pop();

	stack<int> sta2({ 1,2});
	sta.swap(sta2);

	return 0;
}

  

 

C++ std::stack 基本用法

原文:https://www.cnblogs.com/alexYuin/p/12080111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!