How to Access the Top Element of a Stack in C++? Last Updated : 04 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure in which insertion and deletion are done at the end only. In this article, we will learn how to access the top element of a stack in C++. Example: Input: myStack = {10, 20, 30, 40} Output: Top Element: 40Find the Top Element of a Stack in C++To access the top element of a stack in C++, we can use the std::stack::top() function that retrieves the top element of the stack (if the stack is not empty). This function is mainly used to reference the top element of the stack. C++ Program to Access the Top Element of a Stack C++ // C++ Program to illustrate how to access the top element // of the stack #include <iostream> #include <stack> using namespace std; // Driver code int main() { // Creating a stack of integers stack<int> stackData; // Pushing elements onto the stack stackData.push(10); stackData.push(20); stackData.push(30); stackData.push(40); // Accessing the top element of the stack using top() int res = stackData.top(); // Printing the top element cout << "Top Element: " << res << endl; // Printing the stack cout << "Stack Elements: "; while (!stackData.empty()) { cout << stackData.top() << " "; stackData.pop(); } cout << endl; return 0; } OutputTop Element: 40 Stack Elements: 40 30 20 10 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access the First Element of a Vector in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int 2 min read How to Pop an Element From a Stack in C++? In C++, stacks are used to store a collection of similar types of data in a Last-In-First-Out (LIFO) manner. In this article, we will discuss how to pop an element from a stack in C++. Example:Input: myStack = {10, 34, 12, 90, 1}; Output: myStack = {10, 34, 12, 90};Removing an Element from a Stack i 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 Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Push an Element into Stack in C++? In C++ Stacks are a type of container adaptor with LIFO(Last In First Out) type of work, 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 to push the elements onto a Stack in C++. Example:Input:myStack = {40};Output:myStack: { 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read Like