1. 程式人生 > >資料結構 筆記:字串類的建立(上)

資料結構 筆記:字串類的建立(上)

歷史遺留問題

-C語言不支援真正意義上的字串

-C語言字元陣列和一組函式實現字串操作

-C語言不支援自定義型別,因此無法獲得字串型別

 從C到C++的進化過程引入了自定義型別

在C++中可以通過類完成字串型別的定義

字串類的實現

class  String : public Object
{
protected:
    char* m_str;
    int m_length;

    void init(const char* s);

public:
    String();
    String(const char* s);
    String(const String& s);
    int length() const;
    const char* str() const;

    /* 比較操作符重在函式 */
    /* 加法操作符重在函式 */
    /* 賦值操作符重在函式 */

    ~String();
};

實現時的注意事項

-無縫實現String物件與char*字串的互操作

-操作符過載函需要考慮是否支援const版本

-通過C語言中的字串函式實現String的成員函式

void String::init(const char* s)
{
    m_str = strdup(s);

    if(m_str)
    {
        m_length = strlen(m_str);
    }
    else
    {
        //丟擲異常
    }
}

String::String()
{
    init("");
}

String::String(const char* s)
{
    init(s? s : "");
}

String::String(const String &s)
{
    init(s.m_str);
}

String::String(char c)
{
    char s[] = {c, '\0'};

    init(s);
}

int String::length() const
{
    return m_length;

}

const char* String::str() const
{
    return m_str;
}

bool String::operator == (const String& s) const
{
    return (strcmp(m_str,s.m_str) == 0);
}

bool String::operator == (const char* s) const
{
    return (strcmp(m_str,s ? s : "") == 0);
}

bool String::operator != (const String& s) const
{
    return !(*this == s);
}

bool String::operator != (const char* s) const
{
    return !(*this == s);
}

bool String::operator > (const String& s) const
{
    return  (strcmp(m_str,s.m_str) > 0);
}

bool String::operator > (const char* s) const
{
    return  (strcmp(m_str,s ? s : "") > 0);
}

bool String::operator < (const String& s) const
{
    return  (strcmp(m_str,s.m_str) < 0);
}

bool String::operator < (const char* s) const
{
    return  (strcmp(m_str,s ? s : "") < 0);
}


bool String::operator >= (const String& s) const
{
    return  (strcmp(m_str,s.m_str) >= 0);
}

bool String::operator >= (const char* s) const
{
    return  (strcmp(m_str,s ? s : "") >= 0);
}

bool String::operator <= (const String& s) const
{
    return  (strcmp(m_str,s.m_str) <= 0);
}

bool String::operator <= (const char* s) const
{
    return  (strcmp(m_str,s ? s : "") <= 0);
}

String String::operator + (const String& s) const
{
    return (*this + s.m_str);
}

String String::operator + (const char* s) const
{
    String ret;
    int len = m_length + strlen(s ? s : "");
    char* str = reinterpret_cast<char*>(malloc(len + 1));
    if(str)
    {
        strcpy(str,m_str);
        strcat(str,s ? s : "");
        free(ret.m_str);
        ret.m_str = str;
        ret.m_length = len;

    }
    else
    {
        qDebug() << 3;
        //丟擲異常
    }
    return ret;
}
String& String::operator += (const String& s)
{
    return (*this = *this + s.m_str);
}

String& String::operator += (const char* s)
{
    return (*this = *this + s);
}

String& String::operator = (const String& s)
{
    return (*this = s.m_str);
}

String& String::operator = (const char* s)
{
    if(m_str != s)
    {
        char* str = strdup(s ? s: "");
        if(str)
        {
            free(m_str);

            m_str = str;
            m_length = strlen(m_str);
        }
        else
        {
            //丟擲異常
        }
    }
    return *this;
}

String& String::operator = (char c)
{
    char s[] = {c,'\0'};

    return (*this = s);
}

String::~String()
{
    free(m_str);

}

總結:

-C/C++語言本事不支援字串型別

-C語言通過字元陣列和一組函式支援字串操作

-C++通過自定義字串型別支援字串操作

-字串型別通過C語言中的字串函式實現