Implement a Stack Using Vectors in C++ Last Updated : 15 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A stack is a data structure that follows the LIFO (Last In First Out) property means the element that is inserted at last will come out first whereas vectors are dynamic arrays. In this article, we will learn how to implement a stack using vectors in C++. Implementing a Stack Using Vectors in C++Vectors offer dynamic resizing capabilities, allowing the std::stack to grow or shrink as needed. Moreover, the std::vector allows the insertion and deletion at the end in O(1) time complexity. The stack has the following basic operations which we will implement these operations in our program: push(): Adds an element to the top of the stack.pop(): Removes and returns the top element.top(): Returns the top element without removing it.isEmpty(): Checks if the stack is empty.C++ Program to Implement Stack Using VectorThe below example demonstrates how we can implement stack using vectors in C++. C++ // C++ program to implement stack using vector #include <iostream> #include <vector> using namespace std; class Stack { private: // The vector to store stack elements vector<int> v; public: // Function to push an element onto the stack void push(int data) { v.push_back(data); cout << "Pushed: " << data << endl; } // Function to pop an element from the stack int pop() { if (isEmpty()) { cout << "Stack is empty. Cannot pop.\n"; return -1; } int top = v.back(); v.pop_back(); cout << "\nPopped: " << top << endl; return top; } // Function to get the top element of the stack without // removing it int top() { if (isEmpty()) { cout << "Stack is empty. No top element.\n"; return -1; } return v.back(); } // Function to check if the stack is empty bool isEmpty() { return v.empty(); } }; int main() { Stack myStack; // Push elements onto the stack myStack.push(10); myStack.push(20); myStack.push(30); myStack.push(40); // Pop an element from the stack and print it myStack.pop(); // Get the top element of the stack and print it int topElement = myStack.top(); if (topElement != -1) { cout << "\nTop element: " << topElement << endl; } return 0; } OutputPushed: 10 Pushed: 20 Pushed: 30 Pushed: 40 Popped: 40 Top element: 30 All the set operations that are provided here will be able to be executed in O(1) time. Comment More infoAdvertise with us Next Article Implement a Stack Using Vectors in C++ S shravanngoswamii Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Reverse a Vector Using a Stack in C++? In C++, the stack container follows the LIFO (Last In First Out) order of operation. It means that the element first inserted will come out at last while the element that was last inserted will come out first. In this article, we will learn how we can use stack to reverse a vector in C++ Example Inp 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read How to Insert Elements into 2D Vector in C++? In C++, 2D vectors provide several built-in methods to insert elements. The efficiency of the insertion depends on where the insertion occurs. In this article, we will explore different ways to insert elements into a 2D vector in C++ and their specific use cases.The simplest way to add elements to a 3 min read How to Create a Stack of Strings in C++? In C++, Stacks are a type of container adaptor with a 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 create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read Like