字符串类示例

.head

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<stdlib.h>
 5 #include<ctype.h>
 6 using namespace std;
 7 
 8 class TString
 9 {
10 public:
11     //输入/输出操作符
12     //构造函数,创建一个空字符对象
13     TString();
14     //创建一个字符串对象,该对象包含指向字符的s指针
15     //s必须以NULL结尾,从s中复制字符
16     TString(const char* s);
17     //创建一个包含单个字符aChar的字符串
18     TString(char aChar);
19     TString(const TString& arg);//拷贝构造函数
20     ~TString();//析构函数
21     //赋值操作符
22     TString& operator=(const TString& arg);
23     TString& operator=(const char* s);
24     TString& operator=(char aChar);
25     //返回对象当前储存的字符个数
26     int Size() const;
27     //返回posn中len长度的子字符串
28     TString operator()(unsigned posn, unsigned len) const;
29     //返回下表为n的字符
30     char operator()(unsigned n) const;
31     //返回对下标为n的字符的引用
32     const char& operator[](unsigned n) const;
33     //返回指向内部数据的指针,当心
34     const char* c_str()const { return _str; };
35     //以下方法将修改原始对象。
36     //把其他对象中的字符附加在*this后
37     TString& operator+=(const TString& other);
38     //在字符串中改动字符的情况
39     TString& ToLower();//将大写字符转换成小写
40     TString& ToUpper();//将小写字符转换成大写
41 private:
42     //length是存储在对象中的字符个数,但是str所指向的内存至少要length+1长度
43     unsigned _length;
44     char* _str;//指向字符的指针
45 
46 };
47 
48 //支持TString类的非成员函数
49 //返回一个新TSring类的对象,该对象为one和two的级联
50 TString operator+(const TString& one, const TString& two);
51 //输入/输出操作符
52 ostream& operator<<(ostream& o, const TString& s);
53 istream& operator >> (istream& stream, TString& s);
54 //关系操作符,基于ASCII字符集比较。
55 //如果两字符串对象包含相同的字符,则两对象相等。
56 bool operator==(const TString& first, const TString& second);
57 bool operator!=(const TString& first, const TString& second);
58 bool operator<(const TString& first, const TString& second);
59 bool operator>(const TString& first, const TString& second);
60 bool operator>=(const TString& first, const TString& second);
61 bool operator<=(const TString& first, const TString& second);

.cpp

  1 #include "TString.h"
  2 
  3 
  4 
  5 TString::TString()
  6 {
  7     _str = 0;
  8     _length = 0;
  9 }
 10 
 11 
 12 TString::TString(const char* s)
 13 {
 14     if (s&&*s)
 15     {
 16         //指针不为0且指向有效字符
 17         _length = strlen(s);
 18         _str = new char[_length + 1];
 19         strcpy(_str, s);
 20     }
 21     else
 22     {
 23         _str = 0;
 24         _length = 0;
 25     }
 26 }
 27 
 28 TString::TString(char achar)
 29 {
 30     if (achar)
 31     {
 32         _length = 1;
 33         _str = new char[2];
 34         _str[0] = achar;
 35         _str[1] = '';
 36     }
 37     else
 38     {
 39         _str = 0;
 40         _length = 0;
 41     }
 42 }
 43 
 44 TString::TString(const TString& arg)
 45 {
 46     if (arg._str != 0) {
 47         this->_str = new char[arg._length + 1];
 48         strcpy(this->_str, arg._str);
 49         _length = this->_length;
 50     }
 51     else
 52     {
 53         _str = 0;
 54         _length = 0;
 55     }
 56 }
 57 
 58 
 59 TString::~TString()
 60 {
 61     if (_str != 0) delete[] _str;
 62 }
 63 
 64 
 65 TString& TString::operator=(const TString& arg)
 66 {
 67     if (this == &arg)
 68         return *this;
 69     if (this->_length >= arg._length)
 70     {
 71         if (arg._str != 0)
 72             strcpy(this->_str, arg._str);
 73         else
 74             this->_str = 0;
 75         this->_length = arg._length;
 76         return *this;
 77     }
 78     else
 79     {
 80         //*this没有足够空间,_arg更大;
 81         delete[] this->_str;
 82         this->_length = arg._length;
 83         if (this->_length)
 84         {
 85             this->_str = new char[this->_length + 1];
 86             strcpy(this->_str, arg._str);
 87         }
 88         else
 89             this->_length = 0;
 90     }
 91     return *this;
 92 }
 93 
 94 
 95 
 96 TString& TString::operator=(const char* s)
 97 {
 98     if (s == 0 || *s == 0)
 99     {
100         delete[] _str;
101         _length = 0;
102         _str = 0;
103         return *this;
104     }
105     int slength = strlen(s);
106     if (_length >= slength)
107         strcpy(_str, s);
108     else
109     {
110         delete[] _str;
111         _str = new char[slength + 1];
112         strcpy(_str, s);
113     }
114     _length = slength;
115     return *this;
116 }
117 
118 
119 TString& TString::operator=(char aChar)
120 {
121     char a[2];
122     a[0] = aChar;
123     a[1] = '';
124     return (*this = a);//调用上面的操作符
125 }
126 
127 
128 int TString::Size() const { return _length; }
129 
130 TString TString::operator()(unsigned posn, unsigned len) const
131 {
132     if (posn > this->_length)
133         return " ";
134     else
135         if (posn + len > this->_length)
136             len = this->_length - posn;
137     TString result;
138     if (len)
139     {
140         result._str = new char[len + 1];
141         strncpy(result._str, this->_str + posn, len);
142         result._length = len;
143         result._str[len] = '';
144     }
145     return result;
146 }
147 
148 
149 char TString::operator()(unsigned n) const
150 {
151     if (n > this->_length)
152         return 0;
153     else return this->_str[n];
154 }
155 
156 
157 
158 const char& TString::operator[](unsigned n) const
159 {
160     if (n < this->_length)
161         return this->_str[n];
162     cout << "Invalid subscript:" << n << endl;
163     exit(-1);
164     return this->_str[0];
165 }
166 
167 
168 TString& TString::operator+=(const TString& other)
169 {
170     if (other.Size())
171     {
172         this->_length += other.Size();
173         char *newstr = new char[this->_length + 1];
174         if (this->Size())
175             strcpy(newstr, this->_str);
176         else
177             *newstr = '';
178         strcat(newstr, other._str);
179         delete[]this->_str;
180         this->_str = newstr;
181     }
182     return *this;
183 }
184 
185 
186 TString& TString::ToLower()
187 {
188     if (this->_str&&*this->_str)
189     {
190         char *p = this->_str;
191         while (*p)
192         {
193             *p = tolower(*p);
194             p++;
195         }
196     }
197     return *this;
198 }
199 
200 
201 TString& TString::ToUpper()
202 {
203     if (this->_str&&*this->_str)
204     {
205         char *p = this->_str;
206         while (*p)
207         {
208             *p = toupper(*p);
209             p++;
210         }
211     }
212     return *this;
213 }
214 
215 
216 ostream& operator<< (ostream& o, const TString& s)
217 {
218     if (s.c_str())
219         o << s.c_str();
220     return o;
221 }
222 
223 
224 istream& operator >> (istream& stream, TString& s)
225 {
226     char c;
227     s = " ";
228     while (stream.get(c) && isspace(c))
229         ;//什么也不做
230     if (stream)
231     {
232         //stream正常的话
233         //读取字符直到遇到空白
234         do
235         {
236             s += c;
237         } while (stream.get(c) && !isspace(c));
238         if (stream)  //未读取额外字符
239             stream.putback(c);
240     }
241     return stream;
242 }
原文地址:https://www.cnblogs.com/zhengzhe/p/6532799.html