/
12-11.cpp
388 lines (319 loc) · 8.01 KB
/
12-11.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// test iterator constructor
// build substr member function
#include <iostream>
#include <memory>
#include <algorithm>
#include <cstring>
#include <iterator>
#include <list>
using std::allocator;
using std::cout;
using std::cin;
using std::endl;
using std::min;
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;
using std::ostream_iterator;
using std::string;
using std::vector;
using std::list;
using std::back_inserter;
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, value_type c) { create(n,c); }
Str(const_iterator cp) {
create(cp, cp + strlen(cp));
}
Str(const_iterator cp, const_iterator cp2) {
create(cp, cp2);
}
Str(const Str& other)
{
create(other.begin(), other.end());
}
template <class In> Str(In b, In e) {
for (In it = b; it != e; it++) {
push_back(*it);
}
}
explicit operator bool() const {
if (size() == 0)
return false;
else
return true;
}
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& operator+=(const char* c) {
for (size_type i = 0; i != strlen(c); i++) {
grow();
push_back(c[i]);
}
return *this;
}
~Str() {uncreate(); }
char& operator[](size_type i) { return data[i]; }
const char& operator[](size_type i) const { return data[i]; }
bool operator==(Str& s) {
Str temp = *this;
size_type minLen = min(this->size(), s.size());
for (size_type i = 0; i != minLen; i++) {
if (temp[i] != s[i])
return false;
}
return true;
}
bool operator!=(Str& s) {
Str temp = *this;
size_type minLen = min(this->size(), s.size());
for (size_type i = 0; i != minLen; i++) {
if (temp[i] != s[i])
return true;
}
return false;
}
bool operator<(Str& s) {
size_type l1, l2;
l1 = this->size();
l2 = s.size();
if (l1 < l2)
return true;
else
return false;
}
bool operator<=(Str& s) {
size_type l1, l2;
l1 = this->size();
l2 = s.size();
if (l1 <= l2)
return true;
else
return false;
}
bool operator>(Str& s) {
size_type l1, l2;
l1 = this->size();
l2 = s.size();
if (l1 > l2)
return true;
else
return false;
}
bool operator>=(Str& s) {
size_type l1, l2;
l1 = this->size();
l2 = s.size();
if (l1 >= l2)
return true;
else
return false;
}
void push_back(const char& t) {
if (avail == limit)
grow();
unchecked_append(t);
}
const_iterator c_str() {
char temp[strlen(begin())+1];
size_type j = 0;
for (iterator it = begin(); it != end(); it++) {
temp[j] = *it;
j++;
}
temp[j] = '\0';
const_iterator t_it = temp;
return t_it;
}
const_iterator _data() {
char temp[strlen(begin())];
size_type j = 0;
for (iterator it = begin(); it != end(); it++) {
temp[j] = *it;
j++;
}
const_iterator t_it = temp;
return t_it;
}
void copy(char* p, size_type n) {
size_type j = 0;
for (iterator it = begin(); it != begin() + n; it++) {
p[j] = *it;
j++;
}
}
istream& getline (istream& is, Str& s) {
// obliterate existing value(s)
s.uncreate();
// check whether is exists and read the line
char c;
while(is.get(c) && c != '\n') {
s.push_back(c);
}
return is;
}
istream& getline (istream& is, Str& s, char delim) {
// obliterate existing value(s)
s.uncreate();
// check whether is exists and read the line
char c;
while(is.get(c) && c != '\n' && c != delim) {
s.push_back(c);
}
return is;
}
Str substr(size_type i, size_type d) const {
const_iterator cStart, cTerm;
cStart = this->begin() + i;
cTerm = this->begin() + i + d;
Str sub(cStart, cTerm);
return sub;
}
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;
}
Str operator+(const Str& s, const char* c)
{
Str r = s;
r += c;
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;
}
vector<Str> split(const Str& s)
{
typedef Str::size_type string_size;
string_size i = 0;
//Str retStr;
vector<Str> ret;
while (i < s.size()) {
while (i < s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
while (j < s.size()) {
if (!isspace(s[j]))
++j;
else
break;
}
if (i != j) {
// copy from s starting at i and taking j - i chars
Str temp = s.substr(i, j - i);
ret.push_back(temp);
i = j;
}
i = j;
}
return ret;
}
int main() {
Str greeting = "Hello, world!";
vector<Str> result;
result = split(greeting);
for (int i = 0; i != result.size(); i++) {
cout << result[i] << endl;
}
return 0;
}