1. 程式人生 > >C++CString 字串拷貝

C++CString 字串拷貝

class CString {

public:

    int GetLength() const{

        return strlen(m_pData);

    }

    CString(const char* p);

    CString(const CString &str);  //拷貝字串

    CString(char ch,int nRepeat); //串內有N個ch字元

    CString(const char* cstr,int nLenght); //根據字串常量取一部分字元

    CString();

    ~CString();

    friend CString operator+(const CString &str1,const CString &str2);

    CString &operator=(const CString &str); //過載賦值符號只能在類成員區域;

    CString &operator=(const char *str);  //連== 賦值 ,立案等必須要有返回值,它就是用返回值去繼續賦值;

    operator const char* (){return  m_pData;}

    friend std::ostream &operator<<(std::ostream &cout,CString &str);

    friend bool operator!=(const CString& str1,const CString& str2);

    friend bool operator!=(const char* str1,const CString& str2);

    friend bool operator!=(const CString& str1,const char* str2);

    friend bool operator==(const CString& str1,const CString& str2);

    friend bool operator<(const CString& str1,const CString& str2);

    friend bool operator>(const CString& str1,const CString& str2);

    friend bool operator<=(const CString& str1,const CString& str2);

    friend bool operator>=(const CString& str1,const CString& str2);

    char operator[](int nIndex) const;

    CString &operator+=(const CString& str);

private:

    int a;

    char* m_pData;

};

CString ---- CPP

#include "CString.hpp"

#include <iostream>

CString::CString(char ch,int nRepeat){

    m_pData = new char[nRepeat+1];

    memset(m_pData, ch, nRepeat);

    m_pData[nRepeat] = '\0';

}

CString::CString(const char* cstr,int nLenght){

    int nlen = strlen(cstr); //求出字串長度

    if (nlen > nLenght)

        nlen = nLenght;

    m_pData = new char[nlen+1];

    memcpy(m_pData, cstr,nlen);

    m_pData[nlen] = '\0';

}

std::ostream &operator<<(std::ostream &out,CString &str){

    //輸出的自定義輸出符號

    out << str.m_pData;

    return out;

}

CString operator+(const CString &str1,const CString &str2)

{

    int n = str1.GetLength()+str2.GetLength();

    CString str;

    str.m_pData = new char[n+1];  // 這裡的意思是申請所需長度的位元組數然後在加一個0結束符位元組 所以表示為n+1 個位元組數;

    strcpy(str.m_pData, str1.m_pData);

    strcat(str.m_pData, str2.m_pData);

    return str;

}

CString &CString::operator=(const CString &str){

    int nLen = str.GetLength();

    //首先判斷當前的字串長度是不是大於str字串的長度 如果說大於那就先delete在strcopy

    //再重新建立一個更長的空間

    if(GetLength() < nLen){

        delete []m_pData;

        m_pData = new char(nLen+1);  //根據對方的長度重新申請一個空間

        strcmp(m_pData, str.m_pData);

    }

    strcmp(m_pData, str.m_pData);

    return *this;

}

CString::CString(const char* p){

    //定一個變數記錄長度

    int nLen = strlen(p);

    m_pData = new char[nLen+1];

    strcpy(m_pData, p);

}

CString::CString(const CString &str){

    int nLen = str.GetLength();

    //m_pData = str.m_pData; //淺拷貝這是屬於;

    m_pData = new char[nLen+1]; //深拷貝

    strcpy(m_pData, str.m_pData);

}

CString &CString::operator=(const char *str){

    //首先獲取字串長度

    int nLen = strlen(str);

    //判斷字串長度是不是

    if (nLen > GetLength()) {

        //如果str字串長度大於GetLenght現有的字串長度的時候就刪除現有的字串重新分配空間

        delete []m_pData;

        m_pData = new char[nLen+1];

    }

    strcpy(m_pData, str);

    return *this;

}

bool operator!=(const CString& str1,const CString& str2){

    return strcmp(str1.m_pData, str2.m_pData) != 0;

}

bool operator!=(const char* str1,const CString& str2){

    return strcmp(str1, str2.m_pData) !=0;

}

bool operator!=(const CString& str1,const char* str2){

    return strcmp(str1.m_pData, str2) !=0;

}

bool operator==(const CString& str1,const CString& str2){

    return strcmp(str1.m_pData, str2.m_pData) == 0;

}

bool operator<(const CString& str1,const CString& str2){

    return strcmp(str1.m_pData, str2.m_pData) < 0;

}

bool operator>(const CString& str1,const CString& str2){

    return strcmp(str1.m_pData, str2.m_pData) > 0;

}

bool operator<=(const CString& str1,const CString& str2){

    return strcmp(str1.m_pData, str2.m_pData) <= 0;

}

bool operator>=(const CString& str1,const CString& str2){

    return strcmp(str1.m_pData, str2.m_pData) >= 0;

}

char CString::operator[](int nIndex) const{

    return m_pData[nIndex];

}

CString &CString::operator+=(const CString& str){

    int nLen = GetLength()+str.GetLength();

    char *p = new char[nLen+1];

    strcpy(p,m_pData);

    strcat(p, str.m_pData);

    delete []m_pData;  //刪除舊的空間

    m_pData = p;

    return *this;

}

CString::CString(){

    m_pData = new char(0);  //在堆空間上申請一個位元組

}

CString::~CString(){

    //刪除堆空間

    delete m_pData;

}

CPP  ---- main

int main(int argc, const char * argv[]) {

    CString s1 = "abc",s2 = "-xyz";

    s1 += s2;

    std::cout << s1 << std::endl;

    return 0;

}

/*

int main(int argc, const char * argv[]) {

    CString s4("All rights reserved",8); //截斷方式字串

    CString s1 = "abc",s2 = "-xyz";

    //CString s = s1+s2;

    CString s = s1;  //深拷貝

    CString s3('*',8);

   // s1 = s2;  //直接賦值是屬於淺拷貝

    CString s5;

    s5 = s4 = "123456";

    if (s1 == s4)

        std::cout << "相等" << std::endl;

    else

        std::cout << "不相等" << std::endl;

    if ("123456" != s4)

        std::cout << "不等" << std::endl;

    else

        std::cout << "相等" << std::endl;

    int i = 0;

    while (i < s.GetLength()) {

        printf("%c",s[i]);

        ++i;

    }

    std::cout << s1 << std::endl;

    std::cout << s2 << std::endl;

    std::cout << s << std::endl;

    std::cout << s4 << std::endl;

    printf("%d \n",s1.GetLength());

    printf("%d \n",s2.GetLength());

    std::cout << s.GetLength() << std::endl;

    std::cout << s3 << std::endl;

    std::cout << s4 << "\n" << s5 << std::endl;

    //型別轉換字串

    const char *p = s1; //C++字串與C語言字串;

    return 0;

}

 */