How to Declare a Stack in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to declare a stack in C++. Declaring a Stack in C++ STLThe C++ STL provides a container std::stack that implements stack data structure. To declare a stack, we can use the following syntax Syntax to Declare a Stackstack<dataType> stackName;Here, dataType: It is the type of data that a stack will be storing.C++ Program to Declare A Stack C++ // C++ Program to illustrate how to declare a stack #include <iostream> #include <stack> using namespace std; // Driver Code int main() { // Declaring a stack of integers stack<int> stackData; // Pushing elements in the stack stackData.push(10); stackData.push(20); stackData.push(30); // Printing the top element cout << "Top element of the stack is: " << stackData.top() << endl; // Removing elements from the stack stackData.pop(); // Check if the stack is empty if (stackData.empty()) { cout << "The stack is empty." << endl; } else { cout << "The stack is not empty." << endl; } // Printing Size of the stack cout << "Size of the stack: " << stackData.size() << endl; return 0; } OutputTop element of the stack is: 30 The stack is not empty. Size of the stack: 2 Time Complexity: O(N)Auxiliary Space: O(N), where N is the number of stack elements. Create Quiz Comment A anjalibo6rb0 Follow 0 Improve A anjalibo6rb0 Follow 0 Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like