C++第一次试水写cstring类

本文介绍了一个使用 C++ 实现的自定义字符串类,包括构造函数、拷贝构造函数、字符串插入、赋值、长度计算等功能,并通过重载运算符实现了字符串的连接操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class cstring
{
      public:
                 cstring(const char*p=NULL);
                 cstring(const cstring &b);//拷贝构造函数必须要用const类型,否则极有可能出现拷贝中的转换类型的错误
                 void insert(int location,cstring b);//插入一个字符串
                 void assign(const char *b);//重置字符串
                 friend cstring operator +(cstring &a,cstring &b);
                 int length()const;
                 void put();
                 friend  std::ostream & operator<<(std::ostream & os,cstring & str);
                 cstring &operator =(const char*q);
                 cstring &operator =(const cstring &b);

      private:
                 char*str;//由于不是数组指针所以在用的时候必须先申请空间才行
};
std::ostream & operator<<(std::ostream & os,cstring & str)
{
    os<<str.str;
    return os;
}

cstring::cstring(const cstring &b)//要用b这个类必须把它引用过来
{
    int len=b.length();
    str=new char[len+1];
    int i;
    for(i=0;i<=len;i++)
    {
        *(str+i)=*(b.str+i);
    }
};//
cstring::cstring(const char*p )//不能读入,因为无法做到读入,只能够赋值
{
    if(p==NULL)
    {
        str=new char[1];
        *str='\0';
    }
    else
    {
        str=new char[strlen(p)+1];
        strcpy(str,p);
    }

}//close
/*void string::insert(int location,string b)//写错了,应该是拼接,字符串重载运算符的+法中用
{
    int  i,j;
    for(i=0;;i++)
    {
            if(*(str+i)=='\0')
           {
                *(str+i)=*(p+i);
                if(*(b+i)=='\0'){break;}
            }
    }
    return;

};*/
void cstring::insert(int location,cstring b)
{
    cstring a1(*this);//要用this指正否则不能指向当前空间
    cout<<a1.str<<'\n';
    cout<<b.str<<'\n';
    system("pause");//mo problem!
    int strlen=length(),blen=b.length(),nlen=b.length()+length();
    str=new char[nlen];
    int i;
    for(i=0;i<=location;i++)
    {
        *(str+i)=*(a1.str+i);
        cout<<str<<'\n';
        system("pause");
    }
    int j,r;
    cout<<"进入二阶段\n";
    for(j=location,r=0;j<=location+blen;j++,r++)
    {
         *(str+j)=*(b.str+r);
         cout<<str<<'\n';
        system("pause");
    }
    for(j=location+blen;j<=nlen;j++,i++)
    {
        *(str+j)=*(a1.str+i);
        cout<<str<<'\n';
        system("pause");
    }
    delete a1.str;
    delete b.str;
    cout<<str;

};//插入完成,正确
void cstring::assign(const char *b)
{
    delete str;
    str=new char[strlen(b)+1];
    strcpy(str,b);
    cout<<'\n'<<str;
    return;
};
int cstring::length()const
{
    int  i,num=0;
    for(i=0;;i++)
    {
        if(*(str+i)=='\0'){cout<<'\n';break;}
        num++;
    }
   // cout<<num<<'\n';
    return num;
};
void  cstring::put()
{
    cout<<str;
}
//运算符重载:
cstring operator +(cstring &a,cstring &b)
{
      cstring c;
      int len=strlen(a.str)+strlen(b.str);
      c.str=new char[len+1];
      int i,j;
      for(i=0;;i++)
      {
          *(c.str+i)=*(a.str+i);
          if(*(a.str+i)=='\0')
          {
              break;
          }
      }
      for(j=i;;j++)
      {
          *(c.str+j)=*(b.str+j);
          if(*(b.str+j)=='\0')
          {
              break;
          }
      }
      cout<<c.str;
      return(c);
};
cstring &cstring::operator =(const char*q)
{
      int len=strlen(q);
    //  cout<<"len="<<len;
      str=new char[len];
      int i;
      for(i=0;i<=len-1;i++)
      {
          *(str+i)=*(q+i);
      }
//    cout<<str;
    return (*this);
};

cstring &cstring::operator =(const cstring &b)
{
       int len=strlen(b.str);
    //  cout<<"len="<<len;
      str=new char[len];
      int i;
      for(i=0;i<=len-1;i++)
      {
          *(str+i)=*(b.str+i);
      }
//    cout<<str;
    return (*this);


}

int main()
{
    cstring a="abcdefg";
  //  a="abcdefg";
 //   cstring b=a;
    a.put();
    return 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、付费专栏及课程。

余额充值