
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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