【1】String类基本函数如何实现?
示例代码如下:
1 #include <iostream>
2 #include <assert.h>
3 #include <string>
4 using namespace std;
5
6 class String
7 {
8 public:
9 String(const char *str = NULL); // 复合默认构造函数
10 String(const String &other); // 拷贝构造函数
11 String & operator=(const String &other); // 赋值构造函数
12 String operator+(const String &s1); // 双目运算符重载
13 ~String(); // 析构函数
14 void PrintData() const;
15
16 private:
17 char *m_data; //指针成员变量
18 };
19
20 String::String(const char *str)
21 {
22 cout << "Construction" << endl;
23 if (NULL == str)
24 {
25 m_data = new char[1];
26 assert(m_data != NULL);
27 *m_data = '\0';
28 }
29 else
30 {
31 int length = strlen(str);
32 m_data = new char[length + 1];
33 assert(m_data != NULL);
34 strcpy(m_data, str);
35 }
36 }
37
38 String::String(const String &other)
39 {
40 cout << "Copy Construction" << endl;
41 int length = strlen(other.m_data);
42 m_data = new char[length + 1];
43 assert(m_data != NULL);
44 strcpy(m_data, other.m_data);
45 }
46
47 String & String::operator=(const String &other)
48 {
49 cout << "= Construction" << endl;
50 if (this == &other)
51 {
52 return *this;
53 }
54 delete []m_data;
55 int length = strlen(other.m_data);
56 m_data = new char[length + 1];
57 assert(m_data != NULL);
58 strcpy(m_data, other.m_data);
59 return *this;
60 }
61
62 String String::operator+(const String &s1)
63 {
64 cout << "+ operator" << endl;
65 String temp;
66 delete []temp.m_data;
67 temp.m_data = new char[strlen(m_data) + strlen(s1.m_data) + 1];
68 strcpy(temp.m_data, m_data);
69 strcat(temp.m_data, s1.m_data);
70 return temp;
71
72 }
73
74 void String::PrintData() const
75 {
76 cout << m_data << endl;
77 }
78
79 String::~String()
80 {
81 delete []m_data;
82 }
83
84 void main()
85 {
86 String s1; // 默认
87 s1.PrintData();
88
89 String s2("Liuyong"); // 构造函数
90 s2.PrintData();
91
92 String s3 = "abcde"; // 隐式转换
93 s3.PrintData();
94
95 String s4(s3); // 拷贝构造
96 s4.PrintData();
97
98 String s5;
99 s5 = s4; // 赋值构造
100 s5.PrintData();
101
102 String s6 = s2 + s3; //+operator
103 s6.PrintData();
104
105 system("pause");
106 }
107 //run out:
108 /*
109 Construction
110
111 Construction
112 Liuyong
113 Construction
114 abcde
115 Copy Construction
116 abcde
117 Construction
118 = Construction
119 abcde
120 + operator
121 Construction
122 Copy Construction
123 Liuyongabcde
124 请按任意键继续. . .
125 */
上面是string类的部分函数实现,具体实现了构造函数,拷贝构造函数,赋值构造函数,析构函数,以及实现了深拷贝的示范。
【2】String类详细实现(原旧版本)
1 #include <cassert>
2 #include <string>
3 #include <iostream>
4 using namespace std;
5
6 class String
7 {
8 public:
9 String();
10 String(int n, char c);
11 String(const char *source);
12 String(const String &s);
13 String & operator=(char *s);
14 String & operator=(const String &s);
15 ~String();
16 char &operator[](int i);
17 const char &operator[](int i)const;
18 String &operator+=(const String &s);
19 String &operator+=(const char *s);
20 friend ostream & operator<<(ostream &out, String &s);
21 friend istream & operator>>(istream &in, String &s);
22 friend bool operator<(const String &left, const String &right);
23 friend bool operator>(const String &left, const String &right);
24 friend bool operator==(const String &left, const String &right);
25 friend bool operator!=(const String &left, const String &right);
26 int length();
27 char *GetData();
28
29 private:
30 int size;
31 char *data;
32 };
33
34 String::String()
35 {
36 data = new char[1];
37 assert(data != NULL);
38 *data = '\0';
39 size = 0;
40 }
41
42 String::String(int n, char c)
43 {
44 data = new char[n + 1];
45 assert(data != NULL);
46 size = n;
47 char *temp = data;
48 while (n--)
49 {
50 *temp++ = c;
51 }
52 *temp = '\0';
53 }
54 String::String(const char *source)
55 {
56 if (NULL == source)
57 {
58 data = new char[1];
59 assert(data != NULL);
60 *data = '\0';
61 size = 0;
62 }
63 else
64 {
65 size = strlen(source);
66 data = new char[size + 1];
67 assert(data != NULL);
68 strcpy(data, source);
69 }
70 }
71 String::String(const String &s)
72 {
73 data = new char[s.size + 1];
74 assert(data != NULL);
75 strcpy(data, s.data);
76 size = s.size;
77 }
78
79 String & String::operator=(char *s)
80 {
81 if (data != NULL)
82 {
83 delete []data;
84 }
85 size = strlen(s);
86 data = new char[size + 1];
87 assert(data != NULL);
88 strcpy(data, s);
89 return *this;
90 }
91
92 String &String::operator=(const String &s)
93 {
94 if (this == &s)
95 {
96 return *this;
97 }
98 if (data != NULL)
99 {
100 delete []data;
101 }
102 size = strlen(s.data);
103 data = new char[size + 1];
104 assert(data != NULL);
105 strcpy(data, s.data);
106 return *this;
107 }
108
109 String::~String()
110 {
111 if (data != NULL)
112 {
113 delete []data;
114 data = NULL;
115 size = 0;
116 }
117 }
118
119 char &String::operator[](int i)
120 {
121 return data[i];
122 }
123
124 const char &String::operator[](int i)const
125 {
126 return data[i];
127 }
128
129 String &String::operator+=(const String &s)
130 {
131 int len = size + s.size + 1;
132 char *pTemp = data;
133 data = new char[len];
134 assert(data != NULL);
135 size = len - 1;
136 strcpy(data, pTemp);
137 strcat(data, s.data);
138 delete []pTemp;
139 return *this;
140 }
141
142 String & String::operator+=(const char *s)
143 {
144 if (NULL == s)
145 {
146 return *this;
147 }
148 int len = size + strlen(s) + 1;
149 char *pTemp = data;
150 data = new char[len];
151 assert(data != NULL);
152 size = len - 1;
153 strcpy(data, pTemp);
154 strcat(data, s);
155 delete []pTemp;
156 return *this;
157 }
158
159 int String::length()
160 {
161 return size;
162 }
163
164 ostream & operator<<(ostream &out, String &s)
165 {
166 for (int i = 0;i < s.length();i++)
167 {
168 out << s[i] << " ";
169 }
170 return out;
171 }
172
173 istream & operator>>(istream &in, String &s)
174 {
175 char p[50];
176 in.getline(p, 50);
177 s = p;
178 return in;
179 }
180
181 bool operator<(const String &left, const String &right)
182 {
183 int i = 0;
184 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
185 {
186 i++;
187 }
188 return left[i] - right[i] <0 ? true : false;
189 }
190
191 bool operator>(const String &left, const String &right)
192 {
193 int i = 0;
194 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
195 {
196 i++;
197 }
198 return left[i] - right[i] > 0 ? true : false;
199 }
200
201 bool operator ==(const String &left, const String &right)
202 {
203 int i = 0;
204 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
205 {
206 i++;
207 }
208 return (left[i] == right[i]) ? true : false ;
209 }
210
211 bool operator !=(const String &left, const String &right)
212 {
213 int i = 0;
214 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
215 {
216 i++;
217 }
218 return (left[i] == right[i]) ? false : true;
219 }
220
221 void main()
222 {
223 String str(3,'a');
224 String str1(str);
225 String str2("asdf");
226 String str3;
227
228 cout << "str: " << str << endl;
229 cout << "str1: " << str1 << endl;
230 cout << "str2: " << str2 << endl;
231 cout << "str3: " << str3 << endl;
232
233 str3 = str2;
234 cout << "str2: " << str2 << endl;
235 cout << "str3: " << str3 << endl;
236
237 str3 = "12awee";
238 cout << "str3: " << str3 << endl;
239
240 cout << "str3[2]= " << str3[2] << endl;
241
242 str3 += "1234";
243 cout << "str3: " << str3 << endl;
244
245 str3 += str1;
246 cout << "str3: " << str3 << endl;
247
248 String t1 = "1234";
249 String t2 = "1234";
250 String t3 = "12345";
251 String t4 = "12335";
252
253 cout << "t1 == t2 ? " << (t1 == t2) << endl;
254 cout << "t1 < t3 ? " << (t1 < t3) << endl;
255 cout << "t1 > t4 ? " << (t1 > t4) << endl;
256 cout << "t1 != t4 ? " << (t1 != t4) << endl;
257
258 system("pause");
259 }
260 // run out:
261 /*
262 str: a a a
263 str1: a a a
264 str2: a s d f
265 str3:
266 str2: a s d f
267 str3: a s d f
268 str3: 1 2 a w e e
269 str3[2]= a
270 str3: 1 2 a w e e 1 2 3 4
271 str3: 1 2 a w e e 1 2 3 4 a a a
272 t1 == t2 ? 1
273 t1 < t3 ? 1
274 t1 > t4 ? 1
275 t1 != t4 ? 1
276 请按任意键继续. . .
277 */
【3】String类详细实现(最新版本)(C++11后)
VS2017环境下:
1 #include <cassert>
2 #include <string>
3 #include <iostream>
4 using namespace std;
5
6 class String
7 {
8 public:
9 String();
10 String(int n, char c);
11 String(const char *source);
12 String(const String &s);
13 String(String && s);
14 String & operator=(char *s);
15 String & operator=(const String &s);
16 String & operator=(String &&s);
17 ~String();
18
19 char &operator[](int i);
20 const char &operator[](int i) const;
21 String &operator+=(const String &s);
22 String &operator+=(const char *s);
23
24 friend ostream & operator<<(ostream &out, String &s);
25 friend istream & operator>>(istream &in, String &s);
26 friend bool operator<(const String &left, const String &right);
27 friend bool operator>(const String &left, const String &right);
28 friend bool operator==(const String &left, const String &right);
29 friend bool operator!=(const String &left, const String &right);
30
31 int getSize() const;
32 void setSize(int nSize);
33 char* getData() const;
34 void setData(char *pData);
35
36 private:
37 void init();
38
39 private:
40 int m_size;
41 char *m_data;
42 };
43
44 void String::init()
45 {
46 m_data = new char[1];
47 assert(m_data != NULL);
48 *m_data = '\0';
49 m_size = 0;
50 }
51
52 String::String()
53 {
54 init();
55 }
56
57 String::String(int n, char c)
58 {
59 m_data = new char[n + 1];
60 assert(m_data != NULL);
61 m_size = n;
62 char *temp = m_data;
63 while (n--)
64 {
65 *temp++ = c;
66 }
67 *temp = '\0';
68 }
69
70 String::String(const char *source)
71 {
72 if (NULL == source)
73 {
74 init();
75 }
76 else
77 {
78 m_size = strlen(source);
79 m_data = new char[m_size + 1];
80 assert(m_data != NULL);
81 strcpy_s(m_data, (m_size + 1), source);
82 }
83 }
84 String::String(const String &s)
85 {
86 m_data = new char[s.m_size + 1];
87 assert(m_data != NULL);
88 strcpy_s(m_data, (s.m_size + 1), s.m_data);
89 m_size = s.m_size;
90 }
91
92 String::String(String &&s)
93 {
94 cout << "call move ctor" << endl;
95 m_data = s.getData();
96 m_size = s.getSize();
97 s.setData(NULL);
98 s.setSize(0);
99 }
100
101 String & String::operator=(char *s)
102 {
103 if (m_data != NULL)
104 {
105 delete []m_data;
106 }
107 m_size = strlen(s);
108 m_data = new char[m_size + 1];
109 assert(m_data != NULL);
110 strcpy_s(m_data, (m_size + 1), s);
111 return (*this);
112 }
113
114 String &String::operator=(const String &s)
115 {
116 if (this == &s)
117 {
118 return *this;
119 }
120 if (m_data != NULL)
121 {
122 delete []m_data;
123 }
124 m_size = strlen(s.m_data);
125 m_data = new char[m_size + 1];
126 assert(m_data != NULL);
127 strcpy_s(m_data, (m_size + 1), s.m_data);
128 return (*this);
129 }
130
131 String & String::operator=(String &&s)
132 {
133 cout << "call move operator=" << endl;
134 if (this == &s)
135 {
136 return (*this);
137 }
138 if (m_data != NULL)
139 {
140 delete []m_data;
141 }
142 m_data = s.getData();
143 m_size = s.getSize();
144 s.setData(NULL);
145 s.setSize(0);
146 return (*this);
147 }
148
149 String::~String()
150 {
151 if (m_data != NULL)
152 {
153 delete []m_data;
154 m_data = NULL;
155 m_size = 0;
156 }
157 }
158
159 char &String::operator[](int i)
160 {
161 return m_data[i];
162 }
163
164 const char &String::operator[](int i) const
165 {
166 return m_data[i];
167 }
168
169 String &String::operator+=(const String &s)
170 {
171 int len = m_size + s.m_size + 1;
172 char *pTemp = m_data;
173 m_data = new char[len];
174 assert(m_data != NULL);
175 strcpy_s(m_data, (m_size + 1), pTemp);
176 strcat_s(m_data, len, s.m_data);
177 m_size = len - 1;
178 delete []pTemp;
179 return (*this);
180 }
181
182 String & String::operator+=(const char *s)
183 {
184 if (NULL == s)
185 {
186 return (*this);
187 }
188 int len = m_size + strlen(s) + 1;
189 char *pTemp = m_data;
190 m_data = new char[len];
191 assert(m_data != NULL);
192 strcpy_s(m_data, (m_size + 1), pTemp);
193 strcat_s(m_data, len, s);
194 m_size = len - 1;
195 delete []pTemp;
196 return (*this);
197 }
198
199 int String::getSize() const
200 {
201 return m_size;
202 }
203
204 void String::setSize(int nSize)
205 {
206 this->m_size = nSize;
207 }
208
209 char* String::getData() const
210 {
211 return m_data;
212 }
213
214 void String::setData(char *pData)
215 {
216 if (NULL == pData)
217 {
218 init();
219 }
220 else
221 {
222 m_data = pData;
223 }
224 }
225
226 ostream & operator<<(ostream &out, String &s)
227 {
228 for (int i = 0; i < s.getSize(); ++i)
229 {
230 out << s[i] << " ";
231 }
232 return out;
233 }
234
235 istream & operator>>(istream &in, String &s)
236 {
237 char p[50];
238 in.getline(p, 50);
239 s = p;
240 return in;
241 }
242
243 bool operator<(const String &left, const String &right)
244 {
245 int i = 0;
246 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
247 {
248 i++;
249 }
250 return (left[i] - right[i] < 0);
251 }
252
253 bool operator>(const String &left, const String &right)
254 {
255 int i = 0;
256 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
257 {
258 i++;
259 }
260 return (left[i] - right[i] > 0);
261 }
262
263 bool operator==(const String &left, const String &right)
264 {
265 int i = 0;
266 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
267 {
268 i++;
269 }
270 return (left[i] == right[i]);
271 }
272
273 bool operator!=(const String &left, const String &right)
274 {
275 int i = 0;
276 while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
277 {
278 i++;
279 }
280 return (left[i] != right[i]);
281 }
282
283 int main()
284 {
285 String str(3, 'a');
286 String str1(str);
287 String str2("asdf");
288 String str3;
289 cout << "str: " << str << endl;
290 cout << "str1: " << str1 << endl;
291 cout << "str2: " << str2 << endl;
292 cout << "str3: " << str3 << endl;
293
294 // 赋值
295 str3 = str2;
296 cout << "str2: " << str2 << endl;
297 cout << "str3: " << str3 << endl;
298
299 // 移动赋值
300 str3 = "12awee"; // call move operator=
301 cout << "str3: " << str3 << endl;
302 cout << "str3[2]= " << str3[2] << endl;
303
304 str3 += "1234";
305 cout << "str3: " << str3 << endl;
306
307 str3 += str1;
308 cout << "str3: " << str3 << endl;
309
310 // 移动构造函数
311 String str4(move(str2)); // call move ctor
312 cout << "str4: " << str4 << endl;
313
314 // 移动赋值
315 String str5;
316 str5 = move(str3); // call move operator=
317 cout << "str5: " << str5 << endl;
318
319 // 比较
320 String t1 = "1234";
321 String t2 = "1234";
322 String t3 = "12345";
323 String t4 = "12335";
324 cout << "t1 == t2 ? " << (t1 == t2) << endl;
325 cout << "t1 < t3 ? " << (t1 < t3) << endl;
326 cout << "t1 > t4 ? " << (t1 > t4) << endl;
327 cout << "t1 != t4 ? " << (t1 != t4) << endl;
328
329 // 输入
330 String strInput;
331 cout << "Please input string " << endl;
332 cin >> strInput;
333 cout << "strInput: " << strInput << endl;
334
335 system("pause");
336 }
337
338 // run out:
339 /*
340 str: a a a
341 str1: a a a
342 str2: a s d f
343 str3:
344 str2: a s d f
345 str3: a s d f
346 call move operator=
347 str3: 1 2 a w e e
348 str3[2]= a
349 str3: 1 2 a w e e 1 2 3 4
350 str3: 1 2 a w e e 1 2 3 4 a a a
351 call move ctor
352 str4: a s d f
353 call move operator=
354 str5: 1 2 a w e e 1 2 3 4 a a a
355 t1 == t2 ? 1
356 t1 < t3 ? 1
357 t1 > t4 ? 1
358 t1 != t4 ? 1
359 Please input string
360 kaizen
361 strInput: k a i z e n
362 请按任意键继续. . .
363 */
Good Good Study,Day Day Up.
顺序 选择 循环 总结
原文链接: https://www.cnblogs.com/Braveliu/archive/2012/12/31/2840822.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/74238
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!