---
name: "std::stack"
section: "3cxx"
platform: "linux"
package: "libstdc++-12-doc"
version: "12.4.0-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/std::stack/3cxx"
---

# std::stack(3cxx)

A standard container giving FILO behavior.

## NAME

std::stack\< \_Tp, \_Sequence \> - A standard container giving FILO behavior.

## SYNOPSIS

`#include <stack>`

### Public Types

**typedef** \_Sequence::const\_reference **const\_reference**\
 **typedef** \_Sequence **container\_type**\
 **typedef** \_Sequence::reference **reference**\
 **typedef** \_Sequence::size\_type **size\_type**\
 **typedef** \_Sequence::value\_type **value\_type**\

### Public Member Functions

template\<**typename** **\_Seq**  = \_Sequence, **typename** \_Requires  = typename enable\_if\<is\_default\_constructible\<\_Seq\>::value\>::type\> **stack** ()\
 Default constructor creates no elements.  **stack** (\_Sequence &&\_\_c)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **stack** (\_Sequence &&\_\_c, const \_Alloc &\_\_a)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **stack** (const \_Alloc &\_\_a)\
 **stack** (const \_Sequence &\_\_c)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **stack** (const \_Sequence &\_\_c, const \_Alloc &\_\_a)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **stack** (const **stack** &\_\_q, const \_Alloc &\_\_a)\
 template\<**typename** \_Alloc , **typename** \_Requires  = \_Uses\<\_Alloc\>\> **stack** (**stack** &&\_\_q, const \_Alloc &\_\_a)\
 template\<typename... \_Args\> **decltype**(**auto**) **emplace** (**\_Args** &&... **\_\_args**)\
 bool **empty** () const\
 **void** **pop** ()\
 Removes first element.  **void** **push** (const value\_type &\_\_x)\
 Add data to the top of the stack.  **void** **push** (value\_type &&\_\_x)\
 size\_type **size** () const\
 **void** **swap** (**stack** &**\_\_s**) noexcept(**\_\_is\_nothrow\_swappable**\< \_Sequence \>::value)\
 reference **top** ()\
 const\_reference **top** () const\

### Protected Attributes

\_Sequence **c**\

### Friends

template\<**typename** **\_Tp1** , **typename** **\_Seq1** \> bool **operator\<** (const **stack**\< **\_Tp1**, **\_Seq1** \> &, const **stack**\< **\_Tp1**, **\_Seq1** \> &)\
 template\<**typename** **\_Tp1** , **typename** **\_Seq1** \> bool **operator==** (const **stack**\< **\_Tp1**, **\_Seq1** \> &, const **stack**\< **\_Tp1**, **\_Seq1** \> &)\

## Detailed Description

### template\<typename \_Tp, typename \_Sequence = deque\<\_Tp\>\>

class std::stack\< \_Tp, \_Sequence \>"A standard container giving FILO behavior.

**Template Parameters**

*\_Tp* Type of element. \
 *\_Sequence* Type of underlying sequence, defaults to deque\<\_Tp\>.

Meets many of the requirements of a `container`, but does not define anything to do with iterators. Very few of the other standard container interfaces are defined.

This is not a true container, but an *adaptor*. It holds another container, and provides a wrapper interface to that container. The wrapper is what enforces strict first-in-last-out stack behavior.

The second template parameter defines the type of the underlying sequence/container. It defaults to std::deque, but it can be any type that supports `back`, `push_back`, and `pop_back`, such as std::list, std::vector, or an appropriate user-defined type.

Members not found in *normal* containers are `container_type`, which is a typedef for the second Sequence parameter, and `push`, `pop`, and `top`, which are standard stack/FILO operations.

## Constructor & Destructor Documentation

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> template\<typename \_Seq = \_Sequence, typename \_Requires = typename enable\_if\<is\_default\_constructible\<\_Seq\>::value\>::type\> std::stack\< \_Tp, \_Sequence \>::stack ()` [inline]`

Default constructor creates no elements.

## Member Function Documentation

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> bool std::stack\< \_Tp, \_Sequence \>::empty () const` [inline]`

Returns true if the stack is empty.

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> void std::stack\< \_Tp, \_Sequence \>::pop ()` [inline]`

Removes first element. This is a typical stack operation. It shrinks the stack by one. The time complexity of the operation depends on the underlying sequence.

Note that no data is returned, and if the first element's data is needed, it should be retrieved before pop() is called.

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> void std::stack\< \_Tp, \_Sequence \>::push (const value\_type & \_\_x)` [inline]`

Add data to the top of the stack.

**Parameters**

*\_\_x* Data to be added.

This is a typical stack operation. The function creates an element at the top of the stack and assigns the given data to it. The time complexity of the operation depends on the underlying sequence.

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> size\_type std::stack\< \_Tp, \_Sequence \>::size () const` [inline]`

Returns the number of elements in the stack. \

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> reference std::stack\< \_Tp, \_Sequence \>::top ()` [inline]`

Returns a read/write reference to the data at the first element of the stack.

### template\<typename \_Tp , typename \_Sequence = deque\<\_Tp\>\> const\_reference std::stack\< \_Tp, \_Sequence \>::top () const` [inline]`

Returns a read-only (constant) reference to the data at the first element of the stack.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
