-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmystring
More file actions
440 lines (403 loc) · 7.51 KB
/
mystring
File metadata and controls
440 lines (403 loc) · 7.51 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//.h
#include<iostream>
#include<assert.h>
using namespace std;
namespace myString
{
class string
{
public:
//iterator
typedef char* iterator;
iterator begin();
iterator end();
//默认构造
string(const char* str = "");
//拷贝构造
string(const string& s);
void swap(string& s1);
//赋值运算符重载
//string& operator=(const string& s);
string& operator=(myString::string s);
//析构函数
~string();
//[]重载
char& operator[](size_t pos);
//
void reserve(size_t n);
void resize(size_t n,char ch = '\0');
size_t size();
size_t capacity();
//插入
void push_back(const char ch);
void insert(size_t pos,const char ch);
void insert(size_t pos,const char* str);
void append(const char* str);
void operator+=(const char ch);
void operator+=(const char* str);
//查找
size_t find(char ch, size_t pos = 0);
size_t find(char* str, size_t pos = 0);
//转换成c字符串
const char* c_str() const;
private:
char* _str;
size_t _size;
size_t _capacity;
public:
const static size_t npos;
};
ostream& operator<<(ostream& _cout,myString::string s);
istream& operator>>(istream& _cin, myString::string s);
bool operator>(myString::string& s1, myString::string& s2);
bool operator==(myString::string& s1, myString::string& s2);
bool operator>=(myString::string& s1, myString::string& s2);
bool operator<=(myString::string& s1, myString::string& s2);
bool operator!=(myString::string& s1, myString::string& s2);
};
//string.cpp
#include"string.h"
const size_t myString::string::npos = -1;
//默认构造函数
myString::string::string(const char* str)
{
//不能使用nullptr床架对象
assert(str != nullptr);
_str = new char[strlen(str) + 1];
_size = _capacity = strlen(str);
strcpy(_str,str);
}
//拷贝构造函数-现代写法
//myString::string::string(const string& s)
//:_str(nullptr)
//, _size(0)
//, _capacity(0)
//{
// string tmp(s._str);
// swap(tmp);
//}
//拷贝构造-传统写法
myString::string::string(const string& s)
{
_str = new char[strlen(s._str) + 1];
strcpy(_str,s._str);
_size = _capacity = strlen(s._str);
}
//析构函数
myString::string::~string()
{
delete[] _str;
_size = _capacity = 0;
_str = nullptr;
}
//赋值运算符重载-传统写法
//myString::string& myString::string::operator=(const string& s)
//{
// if (this != &s)
// {
// //开辟新空间
// char* tmp = new char[strlen(s._str) + 1];
// strcpy(tmp, s._str);
// _size = _capacity = s._size;
// //释放原空间
// delete[] _str;
// _str = tmp;
// }
// return *this;
//}
//赋值运算符重载-现代写法
myString::string& myString::string::operator=(myString::string s)
{
swap(s);
return *this;
}
//[]重载
char& myString::string::operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
//交换两个字符串对象
void myString::string::swap(string& s1)
{
std::swap(_str, s1._str);
std::swap(_size,s1._size);
std::swap(_capacity,s1._capacity);
}
void myString::string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n+1];
strcpy(tmp,_str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void myString::string::resize(size_t n, char ch)
{
if (n < _size)
{
_str[_size] = '\0';
_size = n;
}
else
{
if (n > _capacity)
{
reserve(n);
}
//后边补ch
for (size_t i = _size; i < n; i++)
_str[i] = ch;
//加'\0'
_str[n] = '\0';
_size = n;
}
}
size_t myString::string::size()
{
return _size;
}
size_t myString::string::capacity()
{
return _capacity;
}
myString::string::iterator myString::string::begin()
{
return _str;
}
myString::string::iterator myString::string::end()
{
return _str + _size;
}
void myString::string::insert(size_t pos,const char ch)
{
//检查插入位置是否正确
assert(pos <= _size);
//判断是否需要扩容
if (_capacity == 0)
reserve(10);
else if (_capacity == _size)
reserve(2*_capacity);
//将size及其之后的字符后移
for (size_t i = _size; i >= pos; i--)
{
_str[i+1] = _str[i];
}
//pos位置插入字符
_str[pos] = ch;
_size++;
}
void myString::string::insert(size_t pos, const char* str)
{
int len = strlen(str);
//检查插入位置是否正确
assert(pos <= _size);
//判断是否需要扩容
if (_capacity == 0)
reserve(len);
else if (_capacity == _size)
reserve(_size+len);
//移动size之后的字符
for (size_t i = _size; i >= pos; i--)
{
_str[i+len] = _str[i];
}
//插入字符串
strncpy(_str+pos,str,len);
_size += len;
}
void myString::string::push_back(const char ch)
{
insert(_size,ch);
}
void myString::string::append(const char* str)
{
insert(_size,str);
}
void myString::string::operator+=(const char ch)
{
push_back(ch);
}
void myString::string::operator+=(const char* str)
{
append(str);
}
size_t myString::string::find(char ch, size_t pos)
{
assert(pos < _size);
for (size_t i = 0; i < _size; i++)
{
if (_str[i] == ch)
return i;
}
return npos;
}
size_t myString::string::find(char* str, size_t pos)
{
const char* ret = strstr(_str + pos, str);
if (ret == nullptr)
{
return npos;
}
else
{
return ret - _str;
}
}
const char* myString::string::c_str() const
{
return _str;
}
ostream& myString::operator<<(ostream& _cout,myString::string s)
{
for (size_t i = 0; i < s.size(); i++)
{
_cout << s[i];
}
_cout << endl;
return _cout;
}
istream& myString::operator>>(istream& _cin, myString::string s)
{
s.resize(0);
char ch;
while (1)
{
//in>>ch;
_cin.get(ch);
if (ch == ' ' || ch == '\n')
{
break;
}
else
{
s += ch;
}
}
return _cin;
}
bool myString::operator>(myString::string& s1, myString::string& s2)
{
size_t i1 = 0, i2 = 0;
while (i1 < s1.size() && i2 < s2.size())
{
if (s1[i1] > s2[i2])
{
return true;
}
else if (s1[i1] < s2[i2])
{
return false;
}
else
{
++i1;
++i2;
}
}
if (i1 < s1.size())
{
return true;
}
else if (i2 < s2.size())
{
return false;
}
else
{
return false;
}
}
bool myString::operator==(myString::string& s1, myString::string& s2)
{
size_t i1 = 0, i2 = 0;
while (i1 < s1.size() && i2 < s2.size())
{
if (s1[i1] > s2[i2])
{
return false;
}
else if (s1[i1] < s2[i2])
{
return false;
}
else
{
++i1;
++i2;
}
}
if (i1 == s1.size() && i2 == s2.size())
{
return true;
}
else
{
return false;
}
}
bool myString::operator>=(myString::string& s1, myString::string& s2)
{
if (s1 > s2 || s1 == s2)
return true;
return false;
}
bool myString::operator<=(myString::string& s1, myString::string& s2)
{
if (s1 > s2)
return false;
return true;
}
bool myString::operator!=(myString::string& s1, myString::string& s2)
{
if (s1 == s2)
return false;
return true;
}
//test.cpp
#include"string.h"
int main()
{
//测试默认构造函数
myString::string s1;
//myString::string s2 = NULL;
myString::string s3 = "hello world";
//测试拷贝构造
myString::string s4(s3);
//赋值运算符重载
myString::string s5("hello");
s5 = s4;
//[]重载
cout << s5[6] << endl;
//reserve和resize
//reserve在插入的时候会调用
myString::string s6;
s6.resize(5,'x');
//size和capacity
cout << s6.size() << " " << s6.capacity() << endl;//均为5
//iterator
myString::string::iterator it = s6.begin();
while (it < s6.end())
{
cout << *it << " ";
it++;
}
cout << endl;
//插入
myString::string s7("helloworld");
s7.insert(5,' ');
s7.push_back('!');
myString::string s8("12345678");
s8.insert(2,"hello");
s8.append("world!");
//输出
cout << s8;
cout << (s7 == s8) << endl;
myString::string s10 = "hello";
myString::string s11 = "aellobbb";
cout << (s10 > s11) << endl;
return 0;
}