The class Str {
Public:
Str ();//the default constructor
Str (const char * s);//by the constructor of the structure of string constants such as Str s (" aaa ");
Str (const Str& S);//copy constructor
~ the Str ();//destructors
Const int len ();//returns the length of the string that contains several valid characters, not including the end '\ 0'.
Bool empty () const;//return for null
Void append (char ch);//add character to the end of the string, such as s="aaa", s.a ppend (' b '), s="aaab";
Void the clear ();//to empty the string
Str substr (int position, int count) const;//return from pos location, count a substring of the characters in a row, namely data [pos and pos + count), left closed right open interval,
Str operator + (const Str& S) const;//operator overloading +, makes the "aa" + "b" + "CD"="aabcd";
Boolean operator==(const Str& S) const;//reloading the==operator, whether two strings are equal, if s1. The data="https://bbs.csdn.net/topics/aba", s2. The data="https://bbs.csdn.net/topics/aba", then the s1==s2;
Str operator=(const Str & amp; S);//overloaded assignment operator '=', put the string s the content of the assignment for yourself.
Const char operator [] (int index);//overloaded operator, '[]' return data [index];
Void the print () const {
For (int I=0; icout cout }
Private:
Char * data;
Int length;
};
The main function is as follows:
Int main () {
Int N;
cin> N;
Str s;
While (N -) {
Char c_str [100].
The scanf (" % s ", c_str);
Int pos, count;
cin> Pos & gt;> The count.
Str TMP (c_str);//by character array structure of an object, call the Str: : Str (const char * s);
TMP. Print ();//print
Str tmp2=TMP. Substr (pos, count);//take a joke list
Tmp2. Print ();//print
Str tmp3;
For (int I=0; iTmp3. Append (TMP + pos [I]);//add char after string
Tmp3. Print ();//print
cout <(tmp2==tmp3) & lt;
Str tmp4 (TMP);//copy constructor
TMP=s + TMP.//string concatenation
S.c Lear ();//empty s
For (int I=0; i[I] s.a ppend (TMP);//add TMP to s
Supachai panitchpakdi rint ();//print
cout <(s==tmp4) & lt;
TMP. The clear ();/TMP/clearing
cout
cout
}
}
I realize to member functions are as follows:
Str: : Str () {
Data=https://bbs.csdn.net/topics/new char [10000];
Length=0;
}
Str: : Str (const char * s) {
Length=strlen (s);
If (length & gt; 10000) {
Data=https://bbs.csdn.net/topics/new char [length * 2];
}
The else
Data=https://bbs.csdn.net/topics/new char [10000];
Memcpy (data, s, length * sizeof (char));
The data/length='\ 0';
}
Str: : Str (const Str& S) {
Length=s.l ength;
If (length> 10000) {
Data=https://bbs.csdn.net/topics/new char [length * 2];
}
The else
Data=https://bbs.csdn.net/topics/new char [10000];
Memcpy (data, s.d. ata, length * sizeof (char));
The data/length='\ 0';
}
Str: : ~ Str () {
If (data)
The delete [] data;
}
Const int Str: : len () {
Return length;
}
Bool Str: : empty () const {
If (length==0) return true;
The else return false;
}
Void Str: : append (char ch) {
If (length + 1 & gt; 10000) {
Char * tem=new char [length * 2];
Memcpy (tem data, length * sizeof (char));
}
The data/length=ch;
Length++;
The data/length='\ 0';
}
Void Str: : clear () {
If (data)
The delete [] data;
Data=https://bbs.csdn.net/topics/new char [10000];
Length=0;
}
Str Str: : substr (int position, int count) const {
If (position + count & gt; Length) count=length - position;
Char * tem;
If (count & gt; 10000) {
Tem=new char [count * 2];
}
The else {
Tem=new char [10000].
}
Memcpy (tem data + position, the count * sizeof (char));
Tem (count)='\ 0';
Str temsrt (tem);
Return temsrt;
}
Str Str: : operator + (const Str& S) const {
Int total=s.l ength + length;
Char * tem;
If (total & gt; 10000)
Tem=new char [total * 2];
The else tem=new char [10000].
Memcpy (tem data, length * sizeof (char));
Memcpy (tem + length, s.d. ata, s.l ength * sizeof (char));
Tem (total)='\ 0';
Str temstr (tem);
Return temstr;
}
Str Str: : operator=(const Str& S) {
If (s.l ength> 10000) {
If (data)
The delete [] data;
Data=https://bbs.csdn.net/topics/new char [s.l ength * 2];
}
Length=s.l ength;
Memcpy (data, s.d. ata, length * sizeof (char));
The data/length='\ 0';
Return * this;
}
Bool Str: : operator==(const Str& S) const {
If (length!=s.l ength) return false.
The else {
If (data!=NULL& & Spyware doctor ata!=NULL)
for (int i=0; iIf (data [I]!=s.d. ata [I]) return false;
}
return true;
}
}
Char Str: : operator (int [] position) const {
The return data [position];
}
nullnullnullnullnullnull