W3cubDocs

/C++

Standard library header <stack>

This header is part of the containers library.

Includes

<initializer_list>(C++11)

Classes

adapts a container to provide stack (LIFO data structure)
(class template)
(C++11)
specializes the std::uses_allocator type trait
(function template)

Functions

lexicographically compares the values in the stack
(function template)
specializes the std::swap algorithm
(function template)

Synopsis

#include <initializer_list>
 
namespace std {
    template <class T, class Container = deque<T> > class stack;
 
    template <class T, class Container>
        bool operator==(const stack<T, Container>& x,const stack<T, Container>& y);
    template <class T, class Container>
        bool operator!=(const stack<T, Container>& x,const stack<T, Container>& y);
 
    template <class T, class Container>
        bool operator< (const stack<T, Container>& x,const stack<T, Container>& y);
    template <class T, class Container>
        bool operator> (const stack<T, Container>& x,const stack<T, Container>& y);
    template <class T, class Container>
        bool operator>=(const stack<T, Container>& x,const stack<T, Container>& y);
    template <class T, class Container>
        bool operator<=(const stack<T, Container>& x,const stack<T, Container>& y);
 
    template <class T, class Container>
        void swap(stack<T, Container>& x, stack<T, Container>& y)
            noexcept(noexcept(x.swap(y)));
 
    template <class T, class Container, class Alloc>
        struct uses_allocator<stack<T, Container>, Alloc> :
             uses_allocator<Container, Alloc>::type { };
}

Class std::stack

template <class T, class Container = std::deque<T> >
class stack {
 public:
    typedef typename Container::value_type value_type;
    typedef typename Container::reference reference;
    typedef typename Container::const_reference const_reference;
    typedef typename Container::size_type size_type;
    typedef Container container_type;
 
 protected:
    Container c;
 
 public:
    explicit stack(const Container&);
    explicit stack(Container&& = Container());
    template <class Alloc> explicit stack(const Alloc&);
    template <class Alloc> stack(const Container&, const Alloc&);
    template <class Alloc> stack(Container&&, const Alloc&);
    template <class Alloc> stack(const stack&, const Alloc&);
    template <class Alloc> stack(stack&&, const Alloc&);
 
    bool empty() const {
        return c.empty();
    }
    size_type size() const {
        return c.size();
    }
    reference top() {
        return c.back();
    }
    const_reference top() const {
        return c.back();
    }
    void push(const value_type& x) {
        c.push_back(x);
    }
    void push(value_type&& x) {
        c.push_back(std::move(x));
    }
    template <class... Args> void emplace(Args&&... args) {
        c.emplace_back(std::forward<Args>(args)...);
    }
    void pop() {
        c.pop_back();
    }
    void swap(stack& s) noexcept(noexcept(swap(c, s.c))) {
        using std::swap;
        swap(c, s.c);
    }
};

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/header/stack