Stack (C++)

A stack is a standard C++ container adapter, designed to be used in a LIFO context,[1] and is implemented with an interface/wrapper to the type passed to it as a template argument, which defaults to a deque.[2] It is so simple, that it can be described with just a sample interface:

template<class T, Class C = deque<T> >
class std::stack {

    protected:

        C c;

    public:

        typedef typename C::value_type value_type;

        typedef typename C::size_type size_type;

        typedef C container_type;

        explicit stack(const C& a = C()) : c(a){} // Inherit the constructor

        bool empty() const { return c.empty(); }

        size_type size() const { return c.size(); }

        value_type& top() const { return c.back(); }

        const value_type& top() const { return c.back(); }

        void push(const value_type& n) { c.push_back(n); }

        void pop() { c.pop_back(); }

};

[3]

Overview of Functions

FunctionDescription
Element AccesstopGet a reference to the top element of the stack, does not pop it
ModifierspushPush an element onto the stack
popPop off the top element of the stack
SizesizeGet number of elements
gollark: Okay, let me rephrase this again: there would still be a cost, but it would be smaller so people would probably be okay with it.
gollark: I mean, it wouldn't make it cheaper to include it vs not include it.
gollark: It wouldn't make it cheaper, it would just be a less significant cost.
gollark: There will probably be some gradual buildup to convince people it's fine.
gollark: Probably, and it'll be for "national security" or something.

References

  1. "stack - C++ Reference". cplusplus.com. Retrieved 25 April 2013.
  2. Stroustrup, Bjarne (1997). C++ Programming Language. Addison-Wesley. pp. 475–476. ISBN 0201889544.
  3. Stroustrup, Bjarne (1997). C++ Programming Language. Addison-Wesley. pp. 475. ISBN 0201889544.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.