Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
stack2.hpp
Go to the documentation of this file.
1 #pragma once
2 
8 // BEGIN_SNIPPET{FullSource}
9 #include "stack1.hpp"
10 #include <deque>
11 #include <stdexcept>
12 #include <string>
13 
15 template <>
16 class Stack<std::string>
17 {
18 public:
20  Stack() : elems(std::deque<std::string>()) {}
21 
24  bool empty() const { return elems.empty(); }
25 
27  void pop();
28 
31  void push(const std::string& s);
32 
35  std::string top() const;
36 
37 private:
38  std::deque<std::string> elems;
39 };
40 
41 // -------------------- Implementations ----------------------------------------
42 
44 {
45  if (elems.empty()) {
46  throw std::out_of_range("pop(): empty stack");
47  }
48  elems.pop_back();
49 }
50 
51 void Stack<std::string>::push(std::string const& elem) { elems.push_back(elem); }
52 
53 std::string Stack<std::string>::top() const
54 {
55  if (elems.empty()) {
56  throw std::out_of_range("top(): empty stack");
57  }
58  return elems.back();
59 }
60 // END_SNIPPET{FullSource}
void pop()
Pop element off the stack.
Definition: stack1.hpp:43
Stack class template.
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
Stack()
Default constructor.
Definition: stack2.hpp:20
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
bool empty() const
Check whether stack is empty.
Definition: stack2.hpp:24
std::deque< std::string > elems
Container for the stack elements.
Definition: stack2.hpp:38