-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_1.cc
94 lines (75 loc) · 2.08 KB
/
iterator_1.cc
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
//
// Program
// A simple collection class to be used as a base for iterator implementation.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o iterator_1 iterator_1.cc
//
// Execution
// ./iterator_1
//
#include <iostream>
#define ASSERT(expr) \
if (!(expr)) { \
std::cerr << "Assert error!" << #expr << __FILE__ << __LINE__; \
std::abort(); \
}
template <typename T>
class Collection {
private:
int _capacity;
int _size;
T *_data;
public:
explicit Collection(int capacity):
_capacity(capacity), _size(0),
_data(new T[_capacity]{}) {}
Collection(const Collection&) = delete;
Collection& operator= (const Collection &) = delete;
~Collection() {
delete []_data;
}
bool is_empty() const { return (_size == 0); }
bool is_full() const { return (_size >= _capacity); }
int size() const { return _size; }
int capacity() const { return _capacity; }
void insert(const T& elem) {
if (is_full())
throw std::runtime_error("collection is already full");
_data[_size++] = elem;
}
T remove() {
if (is_empty())
throw std::runtime_error("collection is empty");
return _data[--_size];
}
void show() const {
for (int i = 0; i < _size; ++i)
std::cout << _data[i] << " ";
std::cout << '\n';
}
};
int main() {
std::cout << "---- Iterator implementation - 1 -----" << '\n';
{
Collection<int> list(3);
list.insert(1);
list.insert(2);
list.insert(3);
ASSERT(list.is_full() == true);
std::cout << "Contents of collection: ";
list.show();
std::cout << "Remove contents: ";
std::cout << list.remove() << " ";
std::cout << list.remove() << " ";
std::cout << list.remove() << '\n';
ASSERT(list.is_empty() == true);
}
{
Collection<int> list(1);
ASSERT(list.is_full() == false);
std::cout << "Contents of collection: ";
list.show();
}
return 0;
}