[Classic face question] [Baidu]c++ implements the string class in STL

Source: Internet
Author: User

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&LT;&LT;STR3.SUBSTR (6,5) <<"Size->"&LT;&LT;STR3.SUBSTR (6,5). Length () <<endl;return 0;}

[Classic face question] [Baidu]c++ implements the string class in STL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.