标准库头文件 <stack>

来自cppreference.com
< cpp‎ | header
 
 
 

此头文件是容器库的一部分。

包含

<initializer_list>(C++11)

适配一个容器以提供栈(LIFO 数据结构)
(类模板)
特化 std::uses_allocator 类型特性
(函数模板)

函数

按照字典顺序比较 stack 中的值
(函数模板)
特化 std::swap 算法
(函数模板)

概要

#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 { };
}

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);
    }
};