This repository was archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex13_47.cpp
117 lines (94 loc) · 2.35 KB
/
ex13_47.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
#include "headers.h"
class String {
friend void print(String& s);
public:
// Normal constructors
String() : begin(nullptr), end(nullptr){};
String(const string& s);
String(const char* pc);
// Copy constructor
String(const String& s);
// Copy assgnment operator
String& operator=(const String& rhs);
// destructor
~String();
size_t length() const;
private:
allocator<char> alloc;
char* begin;
char* end;
void free();
pair<char*, char*> allocate_n_copy(const char* begin, const char* end);
};
String::String(const string& s) {
auto size = s.length();
begin = alloc.allocate(size);
end = uninitialized_copy(s.begin(), s.end(), begin);
}
String::String(const char* pc) {
int len = 0;
auto p = pc;
while (*p != '\0') {
len++;
p++;
}
auto pair = allocate_n_copy(pc, pc + len);
begin = pair.first;
end = pair.second;
}
String::String(const String& s) {
cout << "String(const String& s)" << endl;
auto pair = allocate_n_copy(s.begin, s.end);
begin = pair.first;
end = pair.second;
}
String& String::operator=(const String& rhs) {
cout << "operator=(const String & rhs)" << endl;
// construction
auto new_begin = alloc.allocate(rhs.length());
auto new_end = uninitialized_copy(rhs.begin, rhs.end, new_begin);
// destuction
free();
begin = new_begin;
end = new_end;
return *this;
}
String::~String() {
cout << "~String()" << endl;
free();
}
size_t String::length() const { return end - begin; }
void String::free() {
for_each(begin, end, [this](char& c) { alloc.destroy(&c); });
alloc.deallocate(begin, length());
}
pair<char*, char*> String::allocate_n_copy(const char* b, const char* e) {
auto size = e - b;
auto begin = alloc.allocate(size);
auto end = uninitialized_copy(b, e, begin);
return {begin, end};
}
void print(String& s) {
cout << "\"";
for (auto p = s.begin; p != s.end; p++) {
cout << *p;
}
cout << "\", " << s.length() << endl;
}
int main() {
char cstyle[] = "Hello World!";
String s0(cstyle);
print(s0);
cout << endl;
String s1("AAA");
String s2(s1);
print(s1);
print(s2);
cout << endl;
// String s3("BBB");
// print(s3);
// s1 = s3;
// print(s1);
// print(s2);
// print(s3);
}