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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
#include <algorithm>
#include <utility>
#include <stdexcept>
template <typename T>
class Array
{
private:
size_t m_size {};
T* m_data {};
public:
Array() {}
Array(size_t size) : m_size(size), m_data(new T[size]) { }
Array(const Array& other) : m_size(other.m_size), m_data(new T[other.m_size])
{
std::copy_n(other.m_data, m_size, m_data);
}
Array(Array&& other) : m_size(other.m_size), m_data(std::exchange(other.m_data, nullptr)) {}
Array& operator=(const Array& other)
{
if (this != &other) {
if (size != other.size) {
delete[] m_data;
m_data = nullptr;
m_size = 0; // preserve invariants (in case next line throws(
m_data = new T[other.m_size];
m_size = other.m_size;
}
std::copy_n(other.m_data, other.size, m_data);
}
return *this;
}
Array& operator=(Array&& other)
{
if (this != &other) {
delete[] m_data;
m_data = std::exchange(other.m_data, nullptr);
m_size = std::exchange(other.size, 0);
}
return *this;
}
~Array() { delete[] m_data; }
size_t size() const { return m_size; }
T& operator[](size_t index) { return m_data[index]; }
const T& operator[](size_t index) const { return m_data[index]; }
friend std::ostream& operator<<(std::ostream& out, const Array<T>& obj)
{
out << '[';
for (size_t i = 0; i < obj.m_size; ++i)
out << (i ? "," : "") << obj.m_data[i];
return out << ']';
}
};
template <typename T>
class Stack
{
private:
size_t m_top {};
Array<T> m_array {};
public:
struct StackError : public std::runtime_error
{
StackError(const std::string& what) : runtime_error(what) { }
};
Stack() {}
explicit Stack(size_t size) : m_array(size) { }
Stack(const Stack& s) : m_top(s.m_top), m_array(s.m_array) {}
Stack(Stack&& s) : m_top(s.m_top), m_array(std::move(s.m_array)) {}
Stack& operator=(Stack other)
{
std::swap(m_top, other.m_top);
std::swap(m_array, other.m_array);
return *this;
}
void push(const T& value)
{
if (m_top == m_array.size())
throw StackError("overflow");
m_array[m_top++] = value;
}
T pop()
{
if (empty())
throw StackError("underflow");
return m_array[--m_top];
}
bool empty() const { return m_top == 0; }
size_t size() const { return m_top; }
friend std::ostream& operator<<(std::ostream& out, const Stack<T>& obj)
{
if (obj.empty())
return out << "<empty>";
for (size_t i = obj.m_top; i-- > 0; )
out << obj.m_array[i] << ' ';
return out;
}
};
int main()
{
using std::cout;
Stack<int> s(10);
cout << "Pushing:\n";
for (int i = 0; i < 9; ++i)
{
cout << i << ' ';
s.push(i);
}
cout << "\nPopping:\n";
for (int i = 0; i < 6; ++i)
cout << s.pop() << ' ';
cout << "\nCopying to s1";
auto s1 {s};
cout << "\nPrinting:\n";
cout << s1 << '\n';
}
|