Standard library header <stack> - cppreference.com

From cppreference.com

This header is part of the

containers

library.

Includes

<compare>

(C++20)

Three-way comparison operator

support

[edit]

<initializer_list>

(C++11)

std::initializer_list

class template

[edit]

Classes

stack

adapts a container to provide stack (LIFO data structure)
(class template)

[edit]

std::uses_allocator<std::stack>

(C++11)

specializes the

std::uses_allocator

type trait
(class template specialization)

[edit]

Functions

operator==operator!=operator<operator<=operator>operator>=operator<=>

(C++20)

lexicographically compares the values of two stacks
(function template)

[edit]

std::swap(std::stack)

(C++11)

specializes the

std::swap

algorithm
(function template)

[edit]

Synopsis

#include<compare>#include<initializer_list>namespacestd{// class template stacktemplate<classT,classContainer=deque<T>>classstack;template<classT,classContainer>constexprbooloperator==(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,classContainer>constexprbooloperator!=(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,classContainer>constexprbooloperator<(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,classContainer>constexprbooloperator>(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,classContainer>constexprbooloperator<=(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,classContainer>constexprbooloperator>=(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,three_way_comparableContainer>constexprcompare_three_way_result_t<Container>operator<=>(conststack<T,Container>&x,conststack<T,Container>&y);template<classT,classContainer>constexprvoidswap(stack<T,Container>&x,stack<T,Container>&y)noexcept(noexcept(x.swap(y)));template<classT,classContainer,classAlloc>structuses_allocator<stack<T,Container>,Alloc>;// formatter specialization for stacktemplate<classCharT,classT,formattable<CharT>Container>structformatter<stack<T,Container>,CharT>;template<classT,classContainer>constexprboolenable_nonlocking_formatter_optimization<stack<T,Container>>=false;}Class template

std::stack

namespacestd{template<classT,classContainer=deque<T>>classstack{public:usingvalue_type=Container::value_type;usingreference=Container::reference;usingconst_reference=Container::const_reference;usingsize_type=Container::size_type;usingcontainer_type=Container;protected:Containerc;public:constexprstack():stack(Container()){}constexprexplicitstack(constContainer&);constexprexplicitstack(Container&&);template<classInputIter>constexprstack(InputIterfirst,InputIterlast);template<container-compatible-range<T>R>constexprstack(from_range_t,R&&rg);template<classAlloc>constexprexplicitstack(constAlloc&);template<classAlloc>constexprstack(constContainer&,constAlloc&);template<classAlloc>constexprstack(Container&&,constAlloc&);template<classAlloc>constexprstack(conststack&,constAlloc&);template<classAlloc>constexprstack(stack&&,constAlloc&);template<classInputIter,classAlloc>constexprstack(InputIterfirst,InputIterlast,constAlloc&);template<container-compatible-range<T>R,classAlloc>constexprstack(from_range_t,R&&rg,constAlloc&);constexprboolempty()const{returnc.empty();}constexprsize_typesize()const{returnc.size();}constexprreferencetop(){returnc.back();}constexprconst_referencetop()const{returnc.back();}constexprvoidpush(constvalue_type&x){c.push_back(x);}constexprvoidpush(value_type&&x){c.push_back(std::move(x));}template<container-compatible-range<T>R>constexprvoidpush_range(R&&rg);template<class...Args>constexprdecltype(auto)emplace(Args&&...args){returnc.emplace_back(std::forward<Args>(args)...);}constexprvoidpop(){c.pop_back();}constexprvoidswap(stack&s)noexcept(is_nothrow_swappable_v<Container>){usingstd::swap;swap(c,s.c);}};template<classContainer>stack(Container)->stack<typenameContainer::value_type,Container>;template<classInputIter>stack(InputIter,InputIter)->stack</*iter-value-type*/<InputIter>>;template<ranges::input_rangeR>stack(from_range_t,R&&)->stack<ranges::range_value_t<R>>;template<classContainer,classAllocator>stack(Container,Allocator)->stack<typenameContainer::value_type,Container>;template<classInputIter,classAllocator>stack(InputIter,InputIter,Allocator)->stack</*iter-value-type*/<InputIter>,deque</*iter-value-type*/<InputIter>,Allocator>>;template<ranges::input_rangeR,classAllocator>stack(from_range_t,R&&,Allocator)->stack<ranges::range_value_t<R>,deque<ranges::range_value_t<R>,Allocator>>;template<classT,classContainer,classAlloc>structuses_allocator<stack<T,Container>,Alloc>:uses_allocator<Container,Alloc>::type{};}