stack::top() ফাংশন স্ট্যাকের টপ এলিমেন্টের রেফারেন্স রিটার্ন করে। কোনো প্যারামিটার নেই।
সিনট্যাক্স
stack_name.top(); উদাহরণ
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> st;
st.push(1); st.push(2); st.push(3); st.push(4);
cout << "Top: " << st.top() << endl; // 4
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
// Output: 4 3 2 1
}
আউটপুট
The top element in stack using TOP(): 4 Elements in stack are: 4 3 2 1