Topic
Use C + + to implement the String class in STL, construct, copy construct, reconstruct, assign, compare, string add, get length and substring function.
Code
/* -------------------------------------* Date: 2015-03-31* sjf0115* Title: Implementing String Class * Source: Baidu * Blog:------------------ ------------------*/#include <iostream>#include <cstring>using namespace STD;classstring{ Public://default constructorString (Const Char* str = NULL);//Copy constructorString (ConstString &str);//destructor~string ();//String ConnectionStringoperator+(ConstString & str);//String AssignmentString &operator=(ConstString &str);//String AssignmentString &operator=(Const Char* str);//Determine if strings are equal BOOL operator==(ConstString &str);//Get string length intLength ();//substring [start,start+n-1]String substr (intStartintn);//heavy-duty output friendOstream &operator<< (Ostream &o,ConstString &str);Private:Char* DATA;intsize;};//ConstructorsString::string (Const Char*STR) {if(str = = NULL) {data =New Char[1]; data[0] =' + '; Size =0; }//if Else{size =strlen(str); data =New Char[size+1];strcpy(DATA,STR); }//else}//Copy constructorString::string (ConstString &str) {size = Str.size; data =New Char[size+1];strcpy(Data,str.data);}//destructorString::~string () {Delete[] data;}//String ConnectionString string::operator+(ConstString &str) {string newstr;//Release the original space Delete[] newstr.data; newstr.size = size + str.size; Newstr.data =New Char[newstr.size+1];strcpy(Newstr.data,data);strcpy(Newstr.data+size,str.data);returnNewstr;}//String AssignmentString & string::operator=(ConstString &str) {if(data = = Str.data) {return* This; }//if Delete[] data; size = Str.size; data =New Char[size+1];strcpy(Data,str.data);return* This;}//String Assignmentstring& String::operator=(Const Char* Str) {if(data = = str) {return* This; }//if Delete[] data; Size =strlen(str); data =New Char[size+1];strcpy(DATA,STR);return* This;}//Determine if strings are equalBOOLString::operator==(ConstString &str) {return strcmp(data,str.data) = =0;}//Get string lengthintString::length () {returnSize;}//substring [start,start+n-1]String String::substr (intStartintN) {String newstr;//release old memory Delete[] newstr.data;//re-request memoryNewstr.data =New Char[n+1]; for(inti =0; i < n;++i) {Newstr.data[i] = Data[start+i]; }//forNewstr.data[n] =' + '; Newstr.size = n;returnNewstr;}//heavy-duty outputOstream &operator<< (Ostream &o,ConstString &str) {o<<str.data;returno;}intMain () {String str1 ("Hello"); String str2 ="World"; String STR3 = str1 + str2;cout<<"Str1->"<<str1<<"Size->"<<str1.length () <<endl;cout<<"Str2->"<<str2<<"Size->"<<str2.length () <<endl;cout<<"Str3->"<<str3<<"Size->"<<str3.length () <<endl; String STR4 ("HelloWorld");if(STR3 = = STR4) {cout<<str3<<"and"<<str4<<"It's the same."<<endl; }//if Else{cout<<str3<<"and"<<str4<<"It's not the same."<<endl; }cout<<STR3.SUBSTR (6,5) <<"Size->"<<STR3.SUBSTR (6,5). Length () <<endl;return 0;}
[Classic face question] [Baidu]c++ implements the string class in STL