Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Strings C++ Math C++ Booleans C++ If...Else C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Access Specifiers C++ Encapsulation C++ Inheritance C++ Polymorphism C++ Files C++ Exceptions C++ Date

C++ Data Structures

C++ Data Structures & STL C++ Vectors C++ List C++ Stacks C++ Queues C++ Deque C++ Sets C++ Maps C++ Iterators C++ Algorithms

C++ How To

C++ Add Two Numbers C++ Random Numbers

C++ Reference

C++ Reference C++ Keywords C++ <iostream> C++ <fstream> C++ <cmath> C++ <string> C++ <cstring> C++ <ctime> C++ <vector> C++ <algorithm>

C++ Examples

C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Syllabus C++ Certificate


C++ Stacks


C++ Stack

A stack stores multiple elements in a specific order, called LIFO.

LIFO stands for Last in, First Out. To vizualise LIFO, think of a pile of pancakes, where pancakes are both added and removed from the top. So when removing a pancake, it will always be the last one you added. This way of organizing elements is called LIFO in computer science and programming.

Unlike vectors, elements in the stack are not accessed by index numbers. Since elements are added and removed from the top, you can only access the element at the top of the stack.

To use a stack, you have to include the <stack> header file:

// Include the stack library
#include <stack>

Create a Stack

To create a stack, use the stack keyword, and specify the type of values it should store within angle brackets <> and then the name of the stack, like: stack<type> stackName.

// Create a stack of strings called cars
stack<string> cars;

Note: The type of the stack (string in our example) cannot be changed after its been declared.

Note: You cannot add elements to the stack at the time of declaration, like you can with vectors:

stack<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};

Add Elements

To add elements to the stack, use the .push() function, after declaring the stack:

Example

// Create a stack of strings called cars
stack<string> cars;

// Add elements to the stack
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");

The stack will look like this (remember that the last element added is the top element):

Mazda (top element)
Ford
BMW
Volvo


Access Stack Elements

You cannot access stack elements by referring to index numbers, like you would with arrays and vectors.

In a stack, you can only access the top element, which is done using the .top() function:

Example

// Access the top element
cout << cars.top();  // Outputs "Mazda"
Try it Yourself »

Change the Top Element

You can also use the .top function to change the value of the top element:

Example

// Change the value of the top element
cars.top() = "Tesla";

 // Access the top element
cout << cars.top();  // Now outputs "Tesla" instead of "Mazda"
Try it Yourself »

Remove Elements

You can use the .pop() function to remove an element from the stack.

This will remove the last element that was added to the stack:

Example

// Create a stack of strings called cars
stack<string> cars;

// Add elements to the stack
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");

// Remove the last added element (Mazda)
cars.pop();

// Access the top element (Now Ford)
cout << cars.top();
Try it Yourself »

Get the Size of the Stack

To find out how many elements a stack has, use the .size() function:

Example

cout << cars.size();
Try it Yourself »

Check if the Stack is Empty

Use the .empty() function to find out if the stack is empty or not.

The .empty() function returns 1 (true) if the stack is empty and 0 (false) otherwise:

Example

stack<string> cars;
cout << cars.empty(); // Outputs 1 (The stack is empty)
Try it Yourself »

Example

stack<string> cars;

cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");

cout << cars.empty();  // Outputs 0 (not empty)
Try it Yourself »

Stacks and Queues

Stacks are often mentioned together with Queues, which is a similar data structure described on the next page.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.