C++ Stack::top() Function



The C++ function std::stack::top() returns the top element of the stack. In a stack, the top element is the element that is inserted at the last or most recently inserted element since stacks are LIFO containers (last-in-first-out). We need to perform the pop operation to continuously get the top element from the stack.

The top() function does not accept any parameter. Additionally, it's important to ensure that the top() function cannot be called on an empty stack, if we try to do so it will throw an exception.

Syntax

Following is the syntax for std::stack::top() function −

stack_name.top()

Parameters

This function does not accept any parameter.

Return value

Returns the top element of the stack.

Example 1

The following example shows the usage of the std::stack::top() function. Here we are creating an empty stack of integers and inserting five elements into it using the emplace() function. We are then retrieving and removing the elements in LIFO order using the top() and pop() functions respectively until the stack becomes empty.

#include <iostream>
#include <stack>
using namespace std;

int main(void) {
   stack<int> s;
   for (int i = 0; i < 5; ++i)
      s.emplace(i + 1);
   while (!s.empty()) {
      cout << s.top() << endl;
      s.pop();
   }
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

5
4
3
2
1

Example 2

In the following example, we are creating a stack 's' and inserting three integer values into the stack. Then we are retrieving the top element of the stack using the top() function −

#include <iostream>
#include <stack>
using namespace std;

int main(){
   stack<int> s;
   s.push(7);
   s.push(32);
   s.push(95);
   cout << "The top element in s1 is:" << endl;
   cout << s.top();
   return 0;
}

Output

If we run the above code it will generate the following output −

The top element in s1 is:
95

Example 3

Here, we are creating a stack 's' of integers and inserting two values into the stack (3 and 5). Then we are modifying the top element of the stack to '7' using the top() function.

#include <iostream>
#include <stack>
using namespace std;

int main(){
   stack<int> s;
   s.push(3);
   s.push(5);
   s.top() = 7;
   cout << "Top element of the stack: " << s.top() << endl;
   return 0;
}

Output

Following is an output of the above code −

Top element of the stack: 7

Example 4

Now, we are creating an empty stack of a character data type with the name s1 and inserting the characters 't', 'r', 'u', 'c', and 'k' into the stack in that order. Then we are retrieving the top element of the stack using the top() function −

#include <iostream>
#include <stack>
using namespace std;

int main(void) {
   stack <char> s1;
   s1.push('t');
   s1.push('r');
   s1.push('u');
   s1.push('c');
   s1.push('k');
   cout << "The top element in s1 is:" << endl;
   cout << s1.top();
   return 0;
}

Output

Output of the above code is as follows −

The top element in s1 is:
k
Advertisements