c++中用STL封装了字符串类,但是这是不通用的。

 

 

 

 实现字符串类,就是用面向对象的方式封装C语言对字符串的操作。

添加DTString.h文件:

 1 #ifndef DTSTRING_H
 2 #define DTSTRING_H
 3 
 4 #include "Object.h"
 5 
 6 namespace DTLib
 7 {
 8 
 9 class String : Object
10 {
11 protected:
12     char* m_str;
13     int m_length;
14 
15     void init(const char* s);
16 public:
17     String();
18     String(char c);
19     String(const char* s);
20     String(const String& s);
21 
22     int length() const;
23     const char* str() const;
24 
25     bool operator == (const String& s) const;
26     bool operator == (const char* s) const;
27 
28     bool operator != (const String& s) const;
29     bool operator != (const char* s) const;
30 
31     bool operator > (const String& s) const;
32     bool operator > (const char* s) const;
33 
34     bool operator < (const String& s) const;
35     bool operator < (const char* s) const;
36 
37     bool operator >= (const String& s) const;
38     bool operator >= (const char* s) const;
39 
40     bool operator <= (const String& s) const;
41     bool operator <= (const char* s) const;
42 
43     String operator + (const String& s) const;
44     String operator + (const char* s) const;
45     String operator += (const String& s);
46     String operator += (const char* s);
47 
48     String& operator = (const String& s);
49     String& operator = (const char* s);
50     String& operator = (char c);
51 
52     ~String();
53 };
54 
55 }
56 
57 
58 #endif // DTSTRING_H

 

添加DTString.cpp文件:

  1 #include <cstring>
  2 #include <cstdlib>
  3 #include "DTString.h"
  4 #include "Exception.h"
  5 
  6 using namespace std;
  7 
  8 namespace DTLib
  9 {
 10 
 11 void String::init(const char* s)
 12 {
 13     m_str = strdup(s);
 14 
 15     if( m_str )
 16     {
 17         m_length = strlen(m_str);
 18     }
 19     else
 20     {
 21         THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create string object ...");
 22     }
 23 }
 24 
 25 String::String()
 26 {
 27     init("");
 28 }
 29 
 30 String::String(const char* s)
 31 {
 32     init(s ? s : "");   //空指针就转换成空字符串
 33 }
 34 
 35 String::String(const String& s)
 36 {
 37     init(s.m_str);
 38 }
 39 
 40 String::String(char c)
 41 {
 42     char s[] = {c, '\0'};
 43 
 44     init(s);
 45 }
 46 
 47 int String::length() const
 48 {
 49     return m_length;
 50 }
 51 
 52 const char* String::str() const
 53 {
 54     return m_str;
 55 }
 56 
 57 bool String::operator == (const String& s) const
 58 {
 59     return ( strcmp(m_str, s.m_str) == 0 );
 60 }
 61 
 62 bool String::operator == (const char* s) const
 63 {
 64     return ( strcmp(m_str, s ? s : "") == 0 );
 65 }
 66 
 67 bool String::operator != (const String& s) const
 68 {
 69     return !(*this == s);
 70 }
 71 
 72 bool String::operator != (const char* s) const
 73 {
 74     return !(*this == s);
 75 }
 76 
 77 bool String::operator > (const String& s) const
 78 {
 79     return (strcmp(m_str, s.m_str) > 0);
 80 }
 81 
 82 bool String::operator > (const char* s) const
 83 {
 84     return (strcmp(m_str, s ? s : "") > 0);
 85 }
 86 
 87 bool String::operator < (const String& s) const
 88 {
 89     return (strcmp(m_str, s.m_str) < 0);
 90 }
 91 
 92 bool String::operator < (const char* s) const
 93 {
 94     return (strcmp(m_str, s ? s : "") < 0);
 95 }
 96 
 97 bool String::operator >= (const String& s) const
 98 {
 99     return (strcmp(m_str, s.m_str) >= 0);
100 }
101 
102 bool String::operator >= (const char* s) const
103 {
104     return (strcmp(m_str, s ? s : "") >= 0);
105 }
106 
107 bool String::operator <= (const String& s) const
108 {
109     return (strcmp(m_str, s.m_str) <= 0);
110 }
111 
112 bool String::operator <= (const char* s) const
113 {
114     return (strcmp(m_str, s ? s : "") <= 0);
115 }
116 
117 String String::operator + (const String& s) const
118 {
119     return (*this + s.m_str);
120 }
121 
122 String String::operator + (const char* s) const
123 {
124     String ret;
125 
126     int len = m_length + strlen(s ? s : "");
127 
128     char* str = reinterpret_cast<char*>(malloc(len + 1));
129 
130     if( str )
131     {
132         strcpy(str, m_str);
133         strcat(str, s ? s : "");
134 
135         free(ret.m_str);
136 
137         ret.m_str = str;
138         ret.m_length = len;
139     }
140     else
141     {
142         THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create str object...");
143     }
144 
145     return ret;
146 }
147 String String::operator += (const String& s)
148 {
149     return (*this = *this + s.m_str);
150 }
151 
152 String String::operator += (const char* s)
153 {
154     return (*this = *this + s);
155 }
156 
157 String& String::operator = (const String& s)
158 {
159     return (*this = s.m_str);
160 }
161 
162 String& String::operator = (const char* s)
163 {
164     if( m_str != s )
165     {
166         char* str = strdup(s ? s: "");
167 
168         if( str )
169         {
170             free(m_str);
171 
172             m_str = str;
173             m_length = strlen(m_str);
174         }
175         else
176         {
177             THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create str object...");
178         }
179     }
180 
181     return *this;
182 }
183 
184 String& String::operator = (char c)
185 {
186     char s[] = {c, '\0'};
187 
188     return (*this = s);
189 }
190 
191 String::~String()
192 {
193     free(m_str);
194 }
195 
196 }

 

测试程序如下:

 1 #include <iostream>
 2 #include "DTString.h"
 3 
 4 using namespace std;
 5 using namespace DTLib;
 6 
 7 void test_1()
 8 {
 9     cout << "test_1() begin ... " << endl;
10 
11     String s;
12 
13     s = 'D';
14 
15     cout << s.str() << endl;
16     cout << s.length() << endl;
17     cout << (s == "D") << endl;
18     cout << (s > "CCC") << endl;
19 
20     s += " Zhang ";
21 
22     cout << s.str() << endl;
23     cout << s.length() << endl;
24     cout << (s == "D Zhang ") << endl;
25 
26     cout << "test_1() end ... " << endl;
27 }
28 
29 void test_2()
30 {
31     cout << "test_2() begin ... " << endl;
32 
33     String a[] = {"E", "D", "C", "B", "A"};
34     String min = a[0];
35 
36     for(int i = 0; i < 5; i++)
37     {
38         if( min > a[i] )
39         {
40             min = a[i];
41         }
42     }
43 
44     cout << "min = " << min.str() << endl;
45 
46     cout << "test_2() end ... " << endl;
47 }
48 
49 int main()
50 {
51     test_1();
52 
53     test_2();
54 
55     return 0;
56 }

运行结果如下:

 

小结:

 

posted on 2018-09-17 21:56  周伯通789  阅读(208)  评论(0编辑  收藏  举报