-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathE2.h
63 lines (51 loc) · 1.52 KB
/
E2.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
#ifndef E2_H
#define E2_H
#include <initializer_list>
#include <vector>
#include <string>
#include <iostream>
#include <memory>
#include <exception>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::shared_ptr;
using std::make_shared;
using std::initializer_list;
using std::out_of_range;
class StrBlob{
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(initializer_list<string> il);
size_type size() const {return data->size();}
bool empty() const { return data->empty(); }
// void push_back(const string &s) { return data->push_back(s);}
void push_back(const string &s) const { return data->push_back(s);}
void pop_back();
// string &front() const { cout << "const front "; return data->front(); }
// string &back() const { cout << "const back "; return data->back(); }
string &front() { cout << "nonconst front "; return data->front(); }
string &back() { cout << "nonconst back "; return data->back(); }
const string &front() const {
check(0, "front on empty StrBlob!");
return data->front();
}
const string& back() const {
check(0, "bacm on empty StrBlob!");
return data->back();
}
private:
shared_ptr<vector<string>> data;
void check(size_type i, const string &msg) const;
};
StrBlob::StrBlob():data(make_shared<vector<string>>()) {}
StrBlob::StrBlob(initializer_list<string> il):data(make_shared<vector<string>>(il)) {}
void StrBlob::check(size_type i, const string &msg) const {
if(i >= data->size()){
throw out_of_range(msg);
}
}
#endif