forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.2.cpp
118 lines (97 loc) · 2.84 KB
/
12.2.cpp
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
#include <memory>
#include <string>
#include <vector>
#include <initializer_list>
#include <iostream>
class StrBlob {
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const std::string &s);
void pop_back();
std::string &front();
const std::string &front() const;
std::string &back();
const std::string &back() const;
private:
std::shared_ptr<std::vector<std::string>> data;
void check(size_type pos, const std::string &msg) const;
};
StrBlob::StrBlob() : data(std::make_shared<std::vector<std::string>>()) {}
StrBlob::StrBlob(std::initializer_list<std::string> il)
: data(std::make_shared<std::vector<std::string>>(il)) {}
void StrBlob::check(size_type pos, const std::string &msg) const {
if (pos >= data->size())
throw std::out_of_range(msg);
}
inline void StrBlob::push_back(const std::string &s) {
data->push_back(s);
}
void StrBlob::pop_back() {
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
std::string &StrBlob::front() {
check(0, "front on empty StrBlob");
return data->front();
}
const std::string &StrBlob::front() const {
check(0, "front on empty StrBlob");
return data->front();
}
std::string &StrBlob::back() {
check(0, "back on empty StrBlob");
return data->back();
}
const std::string &StrBlob::back() const {
check(0, "back on empty StrBlob");
return data->back();
}
void testStrBlob(StrBlob &sb) {
try {
sb.push_back("abc");
sb.push_back("def");
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
sb.pop_back();
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
sb.pop_back();
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
void testStrBlob(const StrBlob &sb) {
try {
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
int main() {
StrBlob sb1;
StrBlob sb2{"Hello", "World"};
StrBlob sb3 = {"Bye", "World"};
const StrBlob csb1;
const StrBlob csb2{"This", "Blob"};
testStrBlob(sb1);
std::cout << std::endl;
testStrBlob(sb2);
std::cout << std::endl;
testStrBlob(sb3);
std::cout << std::endl;
testStrBlob(csb1);
std::cout << std::endl;
testStrBlob(csb2);
std::cout << std::endl;
testStrBlob({"ppp", "qqq"});
std::cout << std::endl;
//testStrBlob({"mm", 1}); // Error
return 0;
}