Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

C++: STL Stack

📅 2010-Sep-13 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, stacks, stl ⬩ 📚 Archive

The C++ STL provides a simple stack. This is not a STL container, but is rather called a container adapter. That is, it is a simple interface built upon a real STL container.

Using STL stack is super-easy. Push elements using push(). To get the top element call top() and to pop the top element (without returning it) use pop(). So, typically top() and pop() would be used together. empty() is used to check if stack is empty.

#include <stack>
using namespace std;

stack<int> istack;
istack.push( 10 );
int i = istack.top(); // 10
istack.pop();
bool isEmpty = istack.empty(); // true

© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email