-
Notifications
You must be signed in to change notification settings - Fork 1
/
12-1.cpp
189 lines (145 loc) · 3.74 KB
/
12-1.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <memory>
#include <algorithm>
#include <cstring>
using std::allocator;
using std::cout;
using std::cin;
using std::endl;
using std::max;
using std::uninitialized_copy;
using std::uninitialized_fill;
using std::ptrdiff_t;
using std::back_inserter;
using std::istream;
using std::ostream;
class Str {
friend std::istream& operator>>(istream&, Str&);
public:
typedef char* iterator;
typedef const char* const_iterator;
typedef size_t size_type;
typedef char value_type;
typedef char& reference;
typedef const char& const_reference;
Str() { create(); } // default constructor
Str(size_type n, char c) { create(n,c); }
Str(const char* cp) {
create(cp, cp + strlen(cp));
}
Str& operator=(const Str& s) {
uncreate();
create(s.begin(), s.end());
};
Str& operator+=(const Str& s) {
for (const_iterator it = s.begin(); it != s.end(); it++) {
grow();
const_reference itcr = *it;
push_back(itcr);
}
return *this;
}
~Str() {uncreate(); }
char& operator[](size_type i) { return data[i]; }
const char& operator[](size_type i) const { return data[i]; }
void push_back(const char& t) {
if (avail == limit)
grow();
unchecked_append(t);
}
size_type size() const { return avail - data; }
iterator begin() { return data; }
const_iterator begin() const { return data; }
iterator end() { return avail; }
const_iterator end() const { return avail; }
private:
iterator data;
iterator avail;
iterator limit;
allocator<char> alloc;
void create();
void create(size_type, const char&);
void create(const_iterator, const_iterator);
void uncreate();
void grow();
void unchecked_append(const char&);
};
void Str::create()
{
data = avail = limit = 0;
}
void Str::create(size_type n, const char& val)
{
data = alloc.allocate(n);
limit = avail = data + n;
uninitialized_fill(data, limit, val);
}
void Str::create(const_iterator i, const_iterator j)
{
data = alloc.allocate(j - i);
limit = avail = uninitialized_copy(i, j, data);
}
void Str::uncreate()
{
if (data) {
iterator it = avail;
while (it != data)
alloc.destroy(--it);
alloc.deallocate(data, limit - data);
}
data = limit = avail = 0;
}
void Str::grow()
{
size_type new_size = max(2 * (limit - data), ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
void Str::unchecked_append(const char& val)
{
alloc.construct(avail++, val);
}
Str operator+(const Str& s, const Str& t)
{
Str r = s;
r += t;
return r;
}
ostream& operator<<(ostream& os, const Str& s) {
for (Str::size_type i = 0; i != s.size(); i++)
os << s[i];
return os;
}
istream& operator>>(istream& is, Str& s)
{
// obliterate existing value(s)
s.uncreate();
// read and discard leading whitespace
char c;
while (is.get(c) && isspace(c)) ; // nothing to do
// if still something to read, do so until next whitespace character
if (is) {
do s.push_back(c);
while (is.get(c) && !isspace(c));
// if we read whitespace, then put it back on the stream
if (is)
is.unget();
}
return is;
}
int main() {
Str first, last, full;
cout << "Enter your first name: " << endl;
cin >> first;
cout << "Enter your last name: " << endl;
cin >> last;
full = first;
full += " ";
full += last;
cout << "Thank you, " << full << endl;
return 0;
}