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
|
头文件
"String.h"
#include <iostream>
#include <assert.h>
using
namespace
std;
class
String
{
public
:
String(
const
char
* str =
""
)
{
int
len =
strlen
(str);
_capacity = len + 1;
_size = len;
_str =
new
char
[_capacity];
strcpy
(_str, str);
}
//交换函数
void
_Swap(String& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
String(
const
String& s)
:_str(
new
char
[1]())
{
String tmp = s._str;
_Swap(tmp);
}
//析构函数
~String()
{
if
(_str)
{
delete
[]_str;
_str = NULL;
}
}
void
Display()
{
cout << _str << endl;
}
/********************************************************/
/***********************重载各个运算符*******************/
/********************************************************/
String& operator=(String s)
{
String _Swap(s);
return
*
this
;
}
bool
operator>(
const
String& s)
{
return
strcmp
(_str, s._str) > 0 ?
true
:
false
;
}
bool
operator<(
const
String& s)
{
return
strcmp
(_str, s._str)>0 ?
false
:
true
;
}
bool
operator==(
const
String& s)
{
return
strcmp
(_str, s._str) == 0 ?
true
:
false
;
}
bool
operator>=(
const
String& s)
{
return
strcmp
(_str, s._str) >= 0 ?
true
:
false
;
}
bool
operator<=(
const
String& s)
{
return
strcmp
(_str, s._str) <= 0 ?
true
:
false
;
}
/************************************************************/
/***********************实现String常见函数*******************/
/************************************************************/
//尾插
void
PushBack(
char
ch)
{
_CheckCapacity(_size + 2);
_str[_size++] = ch;
_str[_size] =
'\0'
;
}
//pos位置插入字符ch
void
Insert(
size_t
pos,
char
ch)
{
assert
(pos <= _size);
_CheckCapacity(_size + 2);
int
end = _size + 1;
while
(end >= pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos - 1] = ch;
++_size;
}
/*
int len = 1;
_size += len;
_str = _CheckCapacity();
for (size_t i = _size; i >= pos; i++)
{
_str[i] = _str[i - 1];
}
_str[pos - 1] = ch;
*/
//pos位置插入一个把字符串
void
Insert(
size_t
pos,
const
char
* str)
{
assert
(pos <= _size);
size_t
strSize =
strlen
(str);
_CheckCapacity(_size + 1 + strSize);
int
end = _size;
while
(end >= (
int
)pos)
{
_str[end + strSize] = _str[end];
--end;
}
while
(*str)
{
_str[pos++] = *str++;
}
_size += strSize;
}
char
& operator[](
size_t
index)
{
return
_str[index];
}
//Find一个字符
int
Find(
char
ch)
{
for
(
size_t
i = 0; i < _size; i++)
{
if
(_str[i] == ch)
{
return
i;
}
}
return
-1;
}
//Find一个字符串
int
Find(
const
char
* str)
{
const
char
* pscr = _str;
const
char
* psub = str;
int
scrlen = _size;
int
sublen =
strlen
(str);
int
scrIndex = 0;
while
(scrIndex <= scrlen - sublen)
{
int
i = scrIndex;
int
j = 0;
while
(scrlen > i&&j < sublen&&pscr[i] == psub[j])
{
i++;
j++;
}
if
(j == sublen)
{
return
scrIndex;
}
scrIndex++;
}
return
-1;
}
//删除pos位置后的n个字符
void
Erase(
size_t
pos,
size_t
n)
{
assert
(pos < _size);
for
(
size_t
i = pos; i < _size; i++)
{
_str[i] = _str[i + n];
}
_size -= n;
}
//删除pos位置的字符
void
Erase(
size_t
pos)
{
assert
(pos < _
|
本文转自 七十七快 51CTO博客,原文链接:http://blog.51cto.com/10324228/1752047