devxlogo

How to Access the Topmost Element of a Stack

How to Access the Topmost Element of a Stack

You can access the topmost element (the one pushed the latest) of a stack container by using the member function top():

 #include #include #include using namespace std;void main() {  stack  strstack;  strstack.push("Bjarne");  strstack.push("Stroustrup");  string topmost = strstack.top(); // topmost element is "Stroustrup"  cout<< "topmost element is: "<< topmost << endl;}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist