Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
stack6.hpp
Go to the documentation of this file.
1 #pragma once
2 
8 #include <deque>
9 #include <stdexcept>
10 
14 template <typename T, typename CONT = std::deque<T>>
15 class Stack
16 {
17 public:
19  Stack() : elems(CONT()) {}
20 
24  template <typename T2, typename CONT2>
26 
29  bool empty() const { return elems.empty(); }
30 
32  void pop();
33 
36  void push(const T& e);
37 
40  T top() const;
41 
42 private:
43  CONT elems;
44 };
45 
46 // -------------------- Implementations ----------------------------------------
47 
48 template <typename T, typename CONT>
49 template <typename T2, typename CONT2>
51 {
52  if ((void*)this == (void*)&op2) {
53  return *this;
54  }
55 
56  Stack<T2, CONT2> tmp(op2);
57  elems.clear();
58  while (!tmp.empty()) {
59  elems.push_front(tmp.top());
60  tmp.pop();
61  }
62  return *this;
63 }
64 
65 template <typename T, typename CONT>
66 void Stack<T, CONT>::push(T const& elem)
67 {
68  elems.push_back(elem);
69 }
70 
71 template <typename T, typename CONT>
73 {
74  if (elems.empty()) {
75  throw std::out_of_range("Stack<>::pop(): empty stack");
76  }
77  elems.pop_back();
78 }
79 
80 template <typename T, typename CONT>
81 T Stack<T, CONT>::top() const
82 {
83  if (elems.empty()) {
84  throw std::out_of_range("Stack<>::top(): empty stack");
85  }
86  return elems.back();
87 }
void pop()
Pop element off the stack.
Definition: stack1.hpp:43
void push(const T &e)
Pushes element onto stack.
Definition: stack1.hpp:52
Stack< T > & operator=(const Stack< T2 > &rhs)
Assignment of one stack to another.
Definition: stack5.hpp:49
std::vector< T > elems
Container for the stack elements.
Definition: stack1.hpp:37
bool empty() const
Check whether stack is empty.
Definition: stack6.hpp:29
Stack class using vector as element container.
Definition: stack1.hpp:15
Stack()
Default constructor.
Definition: stack6.hpp:19
T top() const
Return top element of the stack (but not pop-ing it).
Definition: stack1.hpp:58