C++ std::stack 基本用法

#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;
}

  

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