C++ 简单实现string类

 使用C++ 实现string类

重载运算符 = += ==  !=  >>  << 

#include <iostream>
#include <cstring>
#include <cassert>

using namespace std;

class myString{
    private:
        char *_str;
    public:
        myString(const char *s){
            if(s!=NULL){
                _str = new char[strlen(s)+1];
                strcpy(_str,s);
            }
        }
        myString(const char c){
            _str = new char[2];
            _str[0] = c;
            _str[1] = '\0';
        }
        myString(){
            _str = nullptr;
        }
        myString(const myString &s){
            char *p = _str;
            _str = new char[strlen(s._str)+1];
            if(p!=nullptr){
                delete p;
            }
            strcpy(_str,s._str);
        }
        ~myString(){
            delete _str;
        }
        bool operator==(myString const&str1)const{
            return strcmp(this->_str,str1._str)==0;
        }
        bool operator!=(myString const&str1)const{
            return !(strcmp(this->_str,str1._str)==0);
        }
        myString &operator=(myString const&str1){
            if(strlen(this->_str)>=strlen(str1._str)){
                strcpy(this->_str,str1._str);
            }else{
                char *p = new char[strlen(str1._str)+1];
                char *q = this->_str;
                strcpy(p,str1._str);
                this->_str = p;
                if(q!=nullptr){
                    delete q;
                }
            }
            return *this;
        };
        myString &operator=(char const*str1){
            if(_str && strlen(this->_str)>=strlen(str1)){
                strcpy(this->_str,str1);
            }else{
                char *p = new char[strlen(str1)+1];
                char *q = this->_str;
                strcpy(p,str1);
                this->_str = p;
                if(q!=nullptr){
                   delete q;
                }
            }
            return *this;
        };
        friend ostream &operator << (ostream &os,myString const &str1){
            os<<str1._str;
            return os;
        }
        friend istream &operator >> (istream &is,myString &str1){
            char *p = new char[1024];
            is>>p;
            str1=p;
            return is;
        }
        
        myString operator+(myString const&str1)const{
            myString mystr;
            mystr._str = new char[strlen(this->_str)+strlen(str1._str)+1];
            strcpy(mystr._str,this->_str);
            strcat(mystr._str,str1._str);
            return mystr;
        };
        myString &operator+=(myString const&str1){
            char *p = new char[strlen(str1._str)+strlen(this->_str)+1];
            char *q = this->_str;
            strcpy(p,this->_str);
            strcat(p,str1._str);
            this->_str = p;
            if(q!=nullptr){
                delete q;
            }
            return *this;
        };
        myString &operator+=(const char *str1){
            char *p = new char[strlen(str1)+strlen(this->_str)+1];
            char *q = this->_str;
            strcpy(p,this->_str);
            strcat(p,str1);
            this->_str = p;
            if(q!=nullptr){
                delete q;
            }
            return *this;
        };
        myString &operator+=(const char str1){
            assert(str1);
            char *p = new char[strlen(this->_str)+2];
            char *q = this->_str;
            strcpy(p,this->_str);
            p[strlen(this->_str)+1] = str1;
            p[strlen(this->_str)+1] = '\0';
            this->_str = p;
            if(q!=nullptr){
                delete q;
            }
            return *this;
        };
};

int main(){

    myString s1("hello");
    myString s2 = "world";
    myString s3;
    s3 = "world";
    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
    cout<<s1+s2<<endl;
    cout<<(s1==s2)<<endl;
    cout<<(s1!=s2)<<endl;
    cout<<(s1+=s2)<<endl;
    cout<<s1<<endl;
    cin>>s1;
    cout<<s1<<endl;
}
<Zepro>[~/Class/C++/day4]->./a.out 
hello
world
world
helloworld
0
1
helloworld
helloworld
nihao
nihao

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值