Jump to content

Stack (C++)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Theunamedguy (talk | contribs) at 23:54, 21 April 2013 (Started the page, provided a sample interface). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Stack (C++)

A stack is a standard C++ container, which is simply a interface/wrapper to the type passed to it as a template argument, which defaults to a deque[1]. It is so simple, that it can be described just by an 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() } };

[2]

References

  1. ^ Stroustrup, Bjarne (1997). C++ Programming Language. Addison-Wesley. pp. 475–476. ISBN 0201889544.
  2. ^ Stroustrup, Bjarne (1997). C++ Programming Language. Addison-Wesley. p. 475. ISBN 0201889544.