Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
stack1.hpp
Go to the documentation of this file.
1 #pragma once
2 
8 // BEGIN_SNIPPET{FullSource}
9 #include <stdexcept>
10 #include <vector>
11 
14 template <typename T>
15 class Stack
16 {
17 public:
19  Stack() : elems(std::vector<T>()) {}
20 
23  bool empty() const { return elems.empty(); }
24 
26  void pop();
27 
30  void push(const T& e);
31 
34  T top() const;
35 
36 private:
37  std::vector<T> elems;
38 };
39 
40 // -------------------- Implementations ----------------------------------------
41 
42 template <typename T>
44 {
45  if (elems.empty()) {
46  throw std::out_of_range("pop(): empty stack");
47  }
48  elems.pop_back();
49 }
50 
51 template <typename T>
52 void Stack<T>::push(T const& elem)
53 {
54  elems.push_back(elem);
55 }
56 
57 template <typename T>
58 T Stack<T>::top() const
59 {
60  if (elems.empty()) {
61  throw std::out_of_range("top(): empty stack");
62  }
63  return elems.back();
64 }
65 // END_SNIPPET{FullSource}
void pop()
Pop element off the stack.
Definition: stack1.hpp:43
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: stack1.hpp:23
Stack class using vector as element container.
Definition: stack1.hpp:15
Stack()
Default constructor.
Definition: stack1.hpp:19
T top() const
Return top element of the stack (but not pop-ing it).
Definition: stack1.hpp:58