Implementation of C + + string.

Source: Internet
Author: User

Implementation of C + + string

Finish Textquery, then do string. It feels so much simpler.

1) The most basic string function is implemented. [],=,+.

2) Basic is familiar with the value of the pointer copy of the scene.

3) Look at some examples on the internet, many have not str_capition this field. The default constructor has a size of only 1 bytes. Even if it is an example, the feeling can not write like this, its own example is the default 32 bytes, a CPU word length. I do not know whether I understand the problem, or some examples on the internet is really inappropriate.

#include <iostream>#include"malloc.h"#include"TypeInfo"using namespacestd;unsignedintGetcharsize (Const Char*_p);Char* Get_p_copyvalue (Const Char*_p);voidP2p_copyvalue (Const Char*ConstSpChar*ConstDP);classmystring{ Public: MyString (); MyString (Const Char*_p); MyString (Constmystring&_mys); //effect recommends that =copy return a reference. The main idea is to omit the creation of temporary objects and the western structure. Because this is not the same as the normal function.    There is already a hidden parameter this. //There is no need to be sure to return an object. 1) This has updated the data. No need to return objects to deputy. 2) The return pointer can pass in the pointer to the next expression of the hyphen =, (oh, no, here, even if the next or incoming pointer is returned)mystring&operator=(Constmystring&_mys); //here is the need to modify char. Therefore, a reference or pointer must be returned. In the case where the object must exist, you must discard the pointer and use the reference.     Char&operator[](ConstUnsignedintindex); //It feels like you can't return a reference here. =copy, the This is passed in, and the object returned is this. The + number, though, is also passed in this. However, the returned temporary object.    The + number is impossible to modify this. //then the object must be returned to the temporary object so that the correct deputy is given to the temporary variable. MyStringoperator+(Constmystring&_mys); ~MyString (); unsignedintGetcap (); Friend Ostream&operator<< (ostream& OS,Constmystring&ms);Private: Unsignedintstr_capition; Char*char_p;}; unsignedintMystring::getcap () {returnstr_capition;} Mystring::mystring () { This->char_p= (Char*)malloc( +);  This->char_p[0]=' /';  This->str_capition= +;} Mystring::mystring (Const Char*_p) {    if(_p==0x0)    {         This->char_p= (Char*)malloc( +);  This->char_p[0]=' /';  This->str_capition= +; }    Else{unsignedintCharsize=getcharsize (_p); Str_capition=charsize+1; Char_p=Get_p_copyvalue (_p); }}mystring::mystring (Constmystring&_mys) {    //why private, and _mys.str_capition is legal? str_capition=_mys.str_capition; Char_p=Get_p_copyvalue (_mys.char_p);} MyString& MyString::operator=(Constmystring&_mys) {unsignedintCharsize=getcharsize (_mys.char_p); if( This->str_capition>=charsize+1) {P2p_copyvalue (_mys.char_p, This-char_p); }    Else    {        Char* oldp= This-char_p;  This->str_capition=_mys.str_capition;  This->char_p=Get_p_copyvalue (_mys.char_p); DeleteOLDP; }    return* This;}Char& MyString::operator[](ConstUnsignedintindex) {unsignedintCharsize=getcharsize ( This-char_p); if(index<=charsize-1&&index>=0)    {        return  This-Char_p[index]; }    Else    {        //at first I did not know how to deal with, just rushed back to the mystring end of the sign ' \ \ ' address. Very good.         return  This-Char_p[charsize]; }}mystring MyString::operator+(Constmystring&_mys) {unsignedintLhslen=getcharsize ( This-char_p); unsignedintrhslen=getcharsize (_mys.char_p); unsignedintstrcap=lhslen+rhslen+1; Char* P= (Char*)malloc(STRCAP);  for(unsignedintI=0; i!=lhslen;++i) {P[i]= This-Char_p[i]; }     for(unsignedintI=0; i!=rhslen;++i) {p[i+lhslen]=_mys.char_p[i]; } p[strcap-1]=' /'; MyString tmp=MyString (P); returntmp;} MyString::~MyString () {Deletechar_p;}intMain () {MyString str1="hi!"; MyString str2=str1; //Although there is no direct pointer to the copy function. But in fact it will execute mystring::mystring (const char* _p) and mystring& mystring::operator= (const mystring& _mys)//a hermit performs 2 functions. str1="hi!pp."; cout<<str1<<Endl; cout<<str2<<Endl; MyString STR3="ABCDEFG"; cout<<str3<<Endl; STR3=str1; cout<<str3<<Endl;    MyString Emptystr; cout<<"Empty:"<<emptystr<<". Cap:"<<emptystr.getcap () <<Endl; Emptystr=STR3; cout<<"Empty:"<<emptystr<<". Cap:"<<emptystr.getcap () <<Endl; str3[3]='x'; cout<<str3<<"'"<<str3[ About]<<"'"<<Endl; STR3=str1+str2; cout<<str3<<Endl; MyString STR4=str1+str2; cout<<str4<<Endl; Char* emptyp="aaa!"; MyString STR5=MyString (Emptyp); cout<<str5<<Endl; Emptyp=0x0; MyString STR6=MyString (Emptyp); cout<<"0x0 Pointer:"<<str6<<"Cap:"<<str6.getcap () <<Endl; return 0;} unsignedintGetcharsize (Const Char*_p) {unsignedintI=0;  while(_p[i]!=' /')    {        ++i; }    returni;}Char* Get_p_copyvalue (Const Char*_p) {unsignedintCharsize=getcharsize (_p); Char* Char_p= (Char*)malloc(charsize+1);  for(unsignedintI=0; i!=charsize;++i) {Char_p[i]=_p[i]; } Char_p[charsize]=' /'; returnchar_p;}voidP2p_copyvalue (Const Char*ConstSpChar*ConstDP) {unsignedintCharsize=getcharsize (SP);  for(unsignedintI=0; i!=charsize;++i) {Dp[i]=Sp[i]; } Dp[charsize]=' /';} Ostream&operator<< (ostream& OS,Constmystring&ms) {    returnos<<ms.char_p;}

Implementation of C + + string.

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.