C++ 一个自己实现的字符串类

StringEx是一个自己实现的字符串类,实现了最基本的初始化和赋值功能,重载了流运算符和加法运算符。编译环境是Windows下的 MinGW 5.1.6

#include<iostream>
#include<string.h>

using namespace std;

//字符串类
class StringEx
{
public:

    //构造函数
    StringEx(char* s = NULL)
    {
        this -> m_Data = s;
    }

    StringEx(const StringEx &Other)
    {
        m_Data = new char[strlen(Other.m_Data) + 1];
        if(this -> m_Data)
        {
            strcpy(this -> m_Data, Other.m_Data);
        }
    }

    //以输出
    char* ToString()
    {
        return m_Data;
    }

    //设置字符串的值
    void SetString(char* s)
    {
        delete[] m_Data;
        m_Data = new char[strlen(s) + 1];
        if(this -> m_Data)
        {
            strcpy(this -> m_Data, s);
        }
    }

    //在MinGW中这个函数与上面的函数:
    //    StringEx(const StringEx &Other)
    //只要声明一个即可实现赋值
    StringEx operator = (StringEx &Other)
    {
        if(this == &Other)
        {
            return *this;
        }
        delete[] this -> m_Data;
        this -> m_Data = new char[strlen(Other.m_Data + 1)];
        if(this -> m_Data)
        {
            strcpy(this -> m_Data, Other.m_Data);
        }
        return *this;
    }

    //重载加法运算符,将两个字符串连接
    StringEx operator + (StringEx &Another)
    {
        if(Another.ToString() == NULL)
        {
            return *this;
        }
        char* s = new char[strlen(this -> m_Data) + strlen(Another.m_Data) + 1];
        int i, j;
        for(i = 0, j = 0; i < strlen(this -> m_Data); i++, j++)
        {
            s[j] = m_Data[j];
        }
        for(i = 0; i < strlen(Another.m_Data); i++, j++)
        {
            s[j] = Another.m_Data[i];
        }
        s[j] = '\0';

        return StringEx(s);
    }

    //重载加法运算符,将字符串类与字符串连接
    StringEx operator + (char* s2)
    {
        if(s2 == NULL)
        {
            return *this;
        }

        char* s = new char[strlen(this -> m_Data) + strlen(s2) + 1];
        int i, j;
        for(i = 0, j = 0; i < strlen(this -> m_Data); i++, j++)
        {
            s[j] = m_Data[j];
        }
        for(i = 0; i < strlen(s2); i++, j++)
        {
            s[j] = s2[i];
        }
        s[j] = '\0';

        return StringEx(s);
    }

    //析构函数
    ~StringEx(void)
    {
        delete m_Data;
    }

private:

    //字符串内容
    char *m_Data;

};

//重载加法运算符,将字符串与字符串类连接
StringEx operator + (char* s1, StringEx &Another)
{
    if(s1 == NULL)
    {
        return Another;
    }
    
    char* s2 = Another.ToString();

    char* s = new char[strlen(s1) + strlen(s2) + 1];
    int i, j;
    for(i = 0, j = 0; i < strlen(s1); i++, j++)
    {
        s[j] = s1[j];
    }
    for(i = 0; i < strlen(Another.ToString()); i++, j++)
    {
        s[j] = s2[i];
    }
    s[j] = '\0';

    return StringEx(s);
}

//重载流插入运算符
ostream& operator << (ostream& output, StringEx& se)
{
    output << se.ToString();
    return output;
}

//重载流提取运算符
istream& operator >> (istream& input, StringEx& se)
{
    //char* s = "Galatea4321?";
    char *s = new char[1024];
    scanf("%s", s);
    se.SetString(s);
    return input;
}

int main()
{
    cout << "字符串类StringEx" << endl << endl;
    
    //3种赋值
    //以printf形式输出 "Tsybius2014!"
    StringEx x = StringEx("Tsybius2014!_1");
    printf("%s\n", x.ToString());   //1
    StringEx y = "Tsybius2014!_2";
    printf("%s\n", y.ToString());   //2
    StringEx z = x;
    printf("%s\n\n", z.ToString()); //3
    
    //以cout形式输出 "Tsybius2014!"
    cout << x << endl;
    cout << y << endl;
    cout << z << endl << endl;

    //通过cin读取输入的字符串
    cout << "请输入一个字符串" << endl;
    StringEx a;
    cin >> a;
    cout << a << endl << endl;

    //连接两个字符串
    StringEx ta = StringEx("Tsybius1234");
    cout << "ta:" << ta << endl;
    StringEx tb = StringEx("4321Galatea");
    cout << "tb:" << tb << endl;
    StringEx tc = ta + tb; //两个StringEx类连接
    cout << "tc:" << tc << endl;
    StringEx td = ta + "4321Galatea"; //StringEx类与字符串连接
    cout << "td:" << td << endl;
    StringEx te = "Tsybius1234" + tb; //字符串与StringEx类连接
    cout << "te:" << te << endl;

    return 0;
}

程序运行结果

210113_ATai_1425762.png

END

转载于:https://my.oschina.net/Tsybius2014/blog/315700

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值