forked from huangmingchuan/Cpp_Primer_Answers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise12_2.h
71 lines (58 loc) · 1.13 KB
/
exercise12_2.h
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
#include <vector>
#include <memory>
#include <string>
#include <initializer_list>
#include <exception>
using std::vector; using std::string;
class StrBlob
{
public:
using size_type = vector<string>::size_type;
StrBlob() :data(std::make_shared<vector<string>>()) {}
StrBlob(std::initializer_list<string> il) : data(std::make_shared<vector<string>>(il)) {}
size_type size() const
{
return data->size();
}
bool empty() const
{
return data->empty();
}
void push_back(const string& s) const
{
data->push_back(s);
}
void pop_back() const
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
string& front()
{
check(0, "front on empty StrBlob");
return data->front();
}
string& back()
{
check(0, "back on empty StrBlob");
return data->back();
}
const string& front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
const string& back() const
{
check(0, "back on empty StrBlob");
return data->back();
}
private:
void check(size_type i, const string& msg) const
{
if (i >= data->size())
throw std::out_of_range(msg);
}
private:
std::shared_ptr<vector<string>> data;
};