<stack>


Include the STL standard header <stack> to define the template class stack and several supporting templates.

namespace std {
template<class Ty, class Container>
    class stack;

        // TEMPLATE FUNCTIONS
template<class Ty, class Container>
    bool operator==(const stack<Ty, Container>& left,
        const stack<Ty, Container>& right);
template<class Ty, class Container>
    bool operator!=(const stack<Ty, Container>& left,
        const stack<Ty, Container>& right);
template<class Ty, class Container>
    bool operator<(const stack<Ty, Container>& left,
        const stack<Ty, Container>& right);
template<class Ty, class Container>
    bool operator>(const stack<Ty, Container>& left,
        const stack<Ty, Container>& right);
template<class Ty, class Container>
    bool operator<=(const stack<Ty, Container>& left,
        const stack<Ty, Container>& right);
template<class Ty, class Container>
    bool operator>=(const stack<Ty, Container>& left,
        const stack<Ty, Container>& right);

template<class Ty, class Container>
    bool swap(stack<Ty, Container>& left,
        stack<Ty, Container>&)
            noexcept(noexcept(left.swap(right))); [added with C++11]

template<class Ty, class Container, class Alloc>
    struct uses_allocator<stack<Ty, Container>, alloc>; [added with C++11]
}  // namespace std

operator!=

template<class Ty, class Container>
    bool operator!=(const stack <Ty, Container>& left,
        const stack <Ty, Container>& right);

The template function returns !(left == right).

operator==

template<class Ty, class Container>
    bool operator==(const stack <Ty, Container>& left,
        const stack <Ty, Container>& right);

The template function overloads operator== to compare two objects of template class stack. The function returns left.c == right.c.

operator<

template<class Ty, class Container>
    bool operator<(const stack <Ty, Container>& left,
        const stack <Ty, Container>& right);

The template function overloads operator< to compare two objects of template class stack. The function returns left.c < right.c.

operator<=

template<class Ty, class Container>
    bool operator<=(const stack <Ty, Container>& left,
        const stack <Ty, Container>& right);

The template function returns !(right < left).

operator>

template<class Ty, class Container>
    bool operator>(const stack <Ty, Container>& left,
        const stack <Ty, Container>& right);

The template function returns right < left.

operator>=

template<class Ty, class Container>
    bool operator>=(const stack <Ty, Container>& left,
        const stack <Ty, Container>& right);

The template function returns !(left < right).

stack

template<class Ty,
    class Container = deque<Ty> >
    class stack {
public:
    typedef Container container_type;
    typedef typename Container::value_type value_type;
    typedef typename Container::size_type size_type;
    typedef typename Container::reference reference;
    typedef typename Container::const_reference const_reference;

    stack();
    explicit stack(const container_type& cont);

    stack(stack&& right)
        noexcept(is_nothrow_move_constructible<Container>::value); [added with C++11]
    explicit stack(container_type&& cont); [added with C++11]
    template<class Allloc>
        explicit stack(const Alloc& al); [added with C++11]
    template<class Allloc>
        explicit stack(const container_type& cont, const Alloc& al); [added with C++11]
    template<class Allloc>
        explicit stack(const stack& right, const Alloc& al); [added with C++11]
    template<class Allloc>
        explicit stack(stack&& right, const Alloc& al); [added with C++11]

    stack operator=(stack&& right)
        noexcept(is_nothrow_move_assignable<Container>::value); [added with C++11]
    void swap(stack& right)
        noexcept(noexcept(swap(c, right.c))); [added with C++11]

    bool empty() const;
    size_type size() const;

    reference top();
    const_reference top() const;
    void push(const value_type& val);
    void push(value_type&& val); [added with C++11]
    template<class... Ty>
        void emplace(Ty&&... val); [added with C++11]
    void pop();

protected:
    Container c;
    };

The template class describes an object that controls a varying-length sequence of elements. The object allocates and frees storage for the sequence it controls through a protected object named c, of class Container. The type Ty of elements in the controlled sequence must match value_type.

An object of class Container must supply several public members defined the same as for deque, list, and vector (all of which are suitable candidates for class Container). The required members are:

    typedef Ty value_type;
    typedef T0 size_type;
    typedef T1 reference;
    typedef T2 const_reference;

    Container();
    bool empty() const;
    size_type size() const;
    value_type& back();
    const value_type& back() const;
    void push_back(const value_type& val);
    template<class... Ty>
        void emplace_back(const Ty&... val); [added with C++11]
    void pop_back();

    bool operator==(const Container& cont) const;
    bool operator!=(const Container& cont) const;
    bool operator<(const Container& cont) const;
    bool operator>(const Container& cont) const;
    bool operator<=(const Container& cont) const;
    bool operator>=(const Container& cont) const;

Here, T0, T1, and T2 are unspecified types that meet the stated requirements.

Moreover, beginning with C++11, it must be possible to swap two Container objects by calling the standard function std::swap.

stack::const_reference

typedef typename Container::const_reference const_reference;

The type is a synonym for Container::const_reference.

stack::container_type

typedef Container container_type;

The type is a synonym for the template parameter Container.

stack::empty

stack::emplace

template<class... Ty>
    void emplace(Ty&&... val); [added with C++11]

The member function executes c.emplace_back(forward<Ty>(val)...).

bool empty() const;

The member function returns true for an empty controlled sequence.

stack::operator=

stack> operator=(stack&& right)
    noexcept(is_nothrow_move_assignable<Container>::value); [added with C++11]

The operator moves right to *this.

stack::pop

void pop();

The member function removes the last element of the controlled sequence, which must be non-empty.

stack::push

void push(const Ty& val);
void push(value_type&& val); [added with C++11]

The first member function inserts an element with value val at the end of the controlled sequence.

The second member functions is the same as the first, but with an rvalue reference.

stack::reference

typedef typename Container::reference reference;

The type is a synonym for Container::reference.

stack::size

size_type size() const;

The member function returns the length of the controlled sequence.

stack::size_type

typedef typename Container::size_type size_type;

The type is a synonym for Container::size_type.

stack::stack

stack();
explicit stack(const container_type& cont);
stack(stack&& right)
    noexcept(is_nothrow_move_constructible<Container>::value); [added with C++11]
explicit stack(container_type&& cont); [added with C++11]

template<class Allloc>
    explicit stack(const Alloc& al); [added with C++11]
template<class Allloc>
    explicit stack(const container_type& cont, const Alloc& al); [added with C++11]
template<class Allloc>
    explicit stack(const stack& right, const Alloc& al); [added with C++11]
template<class Allloc>
    explicit stack(stack&& right, const Alloc& al); [added with C++11]

The first constructor initializes the stored object with c(), to specify an empty initial controlled sequence. The second constructor initializes the stored object with c(cont), to specify an initial controlled sequence that is a copy of the sequence controlled by cont.

The third constructor is a move constructor, and the fourth is the same as the second but with an rvalue reference.

The remaining group behave the same as earlier constructors, but with an added allocator argument. Unless uses_allocator<Container, Alloc> holds true, these constructors do not participate in overload resolution.

stack::swap

void swap(stack& right)
    noexcept(noexcept(swap(c, right.c))); [added with C++11]

The template function swaps right with *this, using std::swap.

stack::top

reference top();
const_reference top() const;

The member function returns a reference to the last element of the controlled sequence, which must be non-empty.

stack::value_type

typedef typename Container::value_type value_type;

The type is a synonym for Container::value_type.

swap

template<class Ty, class Container>
    bool swap(stack<Ty, Container>& left,
        stack<Ty, Container>&)
            noexcept(noexcept(left.swap(right))); [added with C++11]

The template function executes left.swap(right).

uses_allocator

template<class Ty, class Container, class Alloc>
    struct uses_allocator<stack<Ty, Container>, alloc>; [added with C++11]

The specialization holds true only if uses_allocator<Container, Alloc> holds true.


See also the Table of Contents and the Index.

Copyright © 1992-2013 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.