Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
stack4.hpp
Go to the documentation of this file.
1 #pragma once
2 
8 #include <stdexcept>
9 
13 template <typename T, int MAXSIZE>
14 class Stack
15 {
16 public:
19  bool empty() const { return numElems == 0; }
20 
23  bool full() const { return numElems == MAXSIZE; }
24 
26  void pop();
27 
30  void push(const T& e);
31 
34  T top() const;
35 
36 private:
37  T elems[MAXSIZE];
38  unsigned int numElems{0};
39 };
40 
41 // -------------------- Implementations ----------------------------------------
42 
43 template <typename T, int MAXSIZE>
45 {
46  if (numElems <= 0) {
47  throw std::out_of_range("Stack<>::pop(): empty stack");
48  }
49  --numElems;
50 }
51 
52 template <typename T, int MAXSIZE>
53 void Stack<T, MAXSIZE>::push(T const& elem)
54 {
55  if (numElems == MAXSIZE) {
56  throw std::out_of_range("Stack<>::push(): stack is full");
57  }
58  elems[numElems] = elem;
59  ++numElems;
60 }
61 
62 template <typename T, int MAXSIZE>
63 T Stack<T, MAXSIZE>::top() const
64 {
65  if (numElems <= 0) {
66  throw std::out_of_range("Stack<>::top(): empty stack");
67  }
68  return elems[numElems - 1];
69 }
void pop()
Pop element off the stack.
Definition: stack1.hpp:43
bool full() const
Check wheter stack is full.
Definition: stack4.hpp:23
void push(const T &e)
Pushes element onto stack.
Definition: stack1.hpp:52
std::vector< T > elems
Container for the stack elements.
Definition: stack1.hpp:37
bool empty() const
Check whether stack is empty.
Definition: stack4.hpp:19
Stack class using vector as element container.
Definition: stack1.hpp:15
T top() const
Return top element of the stack (but not pop-ing it).
Definition: stack1.hpp:58
unsigned int numElems
Current number of elements.
Definition: stack4.hpp:38