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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM