blob: bcdafc0de4826a6095d5bc83fcce1021ce5d05c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#pragma once
#include "stack.hpp"
#include <iostream>
#include <stdexcept>
template <typename Item>
Stack<Item>::Stack(uint64_t capacity)
{
_items.reserve(capacity);
}
template <typename Item>
void Stack<Item>::push(Item item)
{
if (_items.size() == _items.capacity())
{
throw std::overflow_error("Tried to push when stack is full");
}
_items.push_back(item);
}
template <typename Item>
void Stack<Item>::pop()
{
if (_items.empty())
{
throw std::underflow_error("Tried to pop when stack size is 0");
}
_items.pop_back();
}
template <typename Item>
Item Stack<Item>::peek()
{
if (_items.empty())
{
throw std::underflow_error("Tried to peek when stack size is 0");
}
return _items.back();
}
|