string類的實現

來源:互聯網
上載者:User

標籤:string類的實現

class String

{

public:

     String(const char*str = NULL);// 普通建構函式

     String(const String &other);    // 拷貝建構函式

     ~ String(void);    // 解構函式

     String & operate =(const String &other);// 賦值函數

    String operator+(const String&other)const;//operator+ 

    bool operator==(const String&);            //operator== 

char&operator[](unsigned int);             //operator[]

friendostream& operator<< (ostream&,String&);//<<

friendostream& operator>> (ostream&,String&);

size_tsize(){return strlen(m_data);}; 

private:

     char *m_data;// 用於儲存字串

};

 

//普通建構函式

String::String(const char *str)

{

        if(str==NULL)

        {

                m_data = new char[1]; // 得分點:對Null 字元串自動申請存放結束標誌‘\0‘的//加分點:對m_data加NULL 判斷

                *m_data = ‘\0‘;

        }   

        else

        {

         int length = strlen(str);

         m_data = new char[length+1]; // 若能加 NULL 判斷則更好

         strcpy(m_data, str);

        }

}

 

//String的解構函式

String::~String(void)

{

        delete [] m_data; // 或delete m_data;

}

 

//拷貝建構函式

String::String(const String &other)    // 得分點:輸入參數為const型

{     //對other.m_data==NULL判斷更好,因為strlen(NULL)會崩潰

        int length = strlen(other.m_data);

        m_data = new char[length+1];     //加分點:對m_data加NULL 判斷

        strcpy(m_data, other.m_data);   

}

 

//賦值函數

String& String::operate =(const String &other) // 得分點:輸入參數為const

 

型

{    

        if(this ==&other)                    //得分點:檢查自賦值

                return *this;  

        delete [] m_data;                //得分點:釋放原有的記憶體資源

        int length = strlen( other.m_data );     

        m_data = new char[length+1];  //加分點:對m_data加NULL 判斷

        strcpy( m_data, other.m_data );  

        return *this;             //得分點:返回本對象的引用 

 

}

//賦值函數一種更好的寫法

String& String::operate =(const String &other) // 得分點:輸入參數為const

 

型

{    

        if(this !=&other)                    //得分點:檢查自賦值

        {

            String strTemp(other);//建立一個臨時對象,if語句執行完後對象自動銷毀

           

            Char *pTemp=strTemp.m_data;

            strTemp.m_data=m_data;

            m_data=pTemp

        }

 

        return *this;             //得分點:返回本對象的引用 

 

}

 

//運算子 +

inlineString String::operator+(const String &other)const 

{ 

    String newString; 

    if(!other.m_data) 

        newString = *this; 

    else if(!m_data) 

        newString = other; 

    else 

    { 

        newString.m_data = newchar[strlen(m_data)+strlen(other.m_data)+1]; 

        strcpy(newString.m_data,m_data); 

       strcat(newString.m_data,other.m_data); 

    } 

    return newString; 

} 

 //運算子==

boolString::operator==(const String &str) 

{ 

    return strcmp(m_data,str.m_data) == 0; 

} 

//運算子[]

inlinechar& String::operator[](unsigned int e) 

{ 

    if(e>=0&&e<=strlen(m_data)) 

        return m_data[e]; 

} 

//運算子<<

ostream&operator<<(ostream& os,String& str) 

{ 

    os << str.m_data; 

    return os; 

} 

 //運算子>>

istream&operator>>( istream &input, String &s ) 

{ 

   chartemp[ 255 ]; //用於儲存輸入資料流 

   input>>setw(255)>>temp; 

   s = temp; //使用賦值運算子 

   return input; //使用return可以支援連續使用>>運算子 

} 

 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

string類的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.