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