STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
stl_stack.h File Reference
#include <bits/concept_check.h>
#include <debug/debug.h>

Go to the source code of this file.

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers. Do not attempt to use it directly. {stack}

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

A standard container giving FILO behavior.

Template Parameters
_TpType of element.
_SequenceType of underlying sequence, defaults to deque<_Tp>.

Meets many of the requirements of a container, but does not define anything to do with iterators. Very few of the other standard container interfaces are defined.

This is not a true container, but an adaptor. It holds another container, and provides a wrapper interface to that container. The wrapper is what enforces strict first-in-last-out stack behavior.

The second template parameter defines the type of the underlying sequence/container. It defaults to std::deque, but it can be any type that supports back, push_back, and pop_front, such as std::list, std::vector, or an appropriate user-defined type.

Members not found in normal containers are container_type, which is a typedef for the second Sequence parameter, and push, pop, and top, which are standard stack/FILO operations.

Default constructor creates no elements.

Returns true if the stack is empty.

Returns the number of elements in the stack.

Returns a read/write reference to the data at the first element of the stack.

Returns a read-only (constant) reference to the data at the first element of the stack.

Add data to the top of the stack.

Parameters
__xData to be added.

This is a typical stack operation. The function creates an element at the top of the stack and assigns the given data to it. The time complexity of the operation depends on the underlying sequence.

Removes first element.

This is a typical stack operation. It shrinks the stack by one. The time complexity of the operation depends on the underlying sequence.

Note that no data is returned, and if the first element's data is needed, it should be retrieved before pop() is called.

Stack equality comparison.

Parameters
__xA stack.
__yA stack of the same type as __x.
Returns
True iff the size and elements of the stacks are equal.

This is an equivalence relation. Complexity and semantics depend on the underlying sequence type, but the expected rules are: this relation is linear in the size of the sequences, and stacks are considered equivalent if their sequences compare equal.

Stack ordering relation.

Parameters
__xA stack.
__yA stack of the same type as x.
Returns
True iff x is lexicographically less than __y.

This is an total ordering relation. Complexity and semantics depend on the underlying sequence type, but the expected rules are: this relation is linear in the size of the sequences, the elements must be comparable with <, and std::lexicographical_compare() is usually used to make the determination.

Based on operator==

Based on operator<

Based on operator<

Based on operator<

63 {
64 _GLIBCXX_BEGIN_NAMESPACE_VERSION
65 
95  template<typename _Tp, typename _Sequence = deque<_Tp> >
96  class stack
97  {
98  // concept requirements
99  typedef typename _Sequence::value_type _Sequence_value_type;
100  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
101  __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
102  __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
103 
104  template<typename _Tp1, typename _Seq1>
105  friend bool
106  operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
107 
108  template<typename _Tp1, typename _Seq1>
109  friend bool
110  operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
111 
112  public:
113  typedef typename _Sequence::value_type value_type;
114  typedef typename _Sequence::reference reference;
115  typedef typename _Sequence::const_reference const_reference;
116  typedef typename _Sequence::size_type size_type;
117  typedef _Sequence container_type;
118 
119  protected:
120  // See queue::c for notes on this name.
121  _Sequence c;
122 
123  public:
124  // XXX removed old def ctor, added def arg to this one to match 14882
128 #if __cplusplus < 201103L
129  explicit
130  stack(const _Sequence& __c = _Sequence())
131  : c(__c) { }
132 #else
133  explicit
134  stack(const _Sequence& __c)
135  : c(__c) { }
136 
137  explicit
138  stack(_Sequence&& __c = _Sequence())
139  : c(std::move(__c)) { }
140 #endif
141 
145  bool
146  empty() const
147  { return c.empty(); }
148 
150  size_type
151  size() const
152  { return c.size(); }
153 
158  reference
159  top()
160  {
162  return c.back();
163  }
164 
169  const_reference
170  top() const
171  {
173  return c.back();
174  }
175 
185  void
186  push(const value_type& __x)
187  { c.push_back(__x); }
188 
189 #if __cplusplus >= 201103L
190  void
191  push(value_type&& __x)
192  { c.push_back(std::move(__x)); }
193 
194  template<typename... _Args>
195  void
196  emplace(_Args&&... __args)
197  { c.emplace_back(std::forward<_Args>(__args)...); }
198 #endif
199 
211  void
212  pop()
213  {
215  c.pop_back();
216  }
217 
218 #if __cplusplus >= 201103L
219  void
220  swap(stack& __s)
221  noexcept(noexcept(swap(c, __s.c)))
222  {
223  using std::swap;
224  swap(c, __s.c);
225  }
226 #endif
227  };
228 
241  template<typename _Tp, typename _Seq>
242  inline bool
243  operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
244  { return __x.c == __y.c; }
245 
259  template<typename _Tp, typename _Seq>
260  inline bool
261  operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
262  { return __x.c < __y.c; }
263 
265  template<typename _Tp, typename _Seq>
266  inline bool
267  operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
268  { return !(__x == __y); }
269 
271  template<typename _Tp, typename _Seq>
272  inline bool
273  operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
274  { return __y < __x; }
275 
277  template<typename _Tp, typename _Seq>
278  inline bool
279  operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
280  { return !(__y < __x); }
281 
283  template<typename _Tp, typename _Seq>
284  inline bool
285  operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
286  { return !(__x < __y); }
287 
288 #if __cplusplus >= 201103L
289  template<typename _Tp, typename _Seq>
290  inline void
291  swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
292  noexcept(noexcept(__x.swap(__y)))
293  { __x.swap(__y); }
294 
295  template<typename _Tp, typename _Seq, typename _Alloc>
296  struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
297  : public uses_allocator<_Seq, _Alloc>::type { };
298 #endif
299 
300 _GLIBCXX_END_NAMESPACE_VERSION
301 } // namespace
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define __glibcxx_class_requires(_a, _b)
Definition: concept_check.h:48
#define __glibcxx_requires_nonempty()
Definition: debug.h:77
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
#define __glibcxx_class_requires2(_a, _b, _c)
Definition: concept_check.h:49
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160