string类

#include <iostream>

#include <cstring>

#include <string>

using namespace std;

 

class My_string

{

private:

    char *cstr;

    int len;

 

public:

    My_string(); //无参构造

 

    My_string(const char *str); //有参构造

 

    My_string(const My_string &other); //拷贝构造

 

    My_string &operator=(const My_string &other);//拷贝赋值

    My_string &operator=(const char* other);

 

    char &operator[](int); //[]

 

    const My_string operator+(const My_string &other)const; //+

    const My_string operator+(const char* other)const;

 

    bool operator==(const My_string &other)const; //==

 

    bool operator!=(const My_string &other)const; //!=

 

    bool operator<(const My_string &other)const; //<

 

    bool operator>(const My_string &other)const; //>

 

    bool operator<=(const My_string &other)const; //<=

 

     bool operator>=(const My_string &other)const; //>=

 

     friend ostream &operator<<(ostream &L,My_string &R); //将插入运算符重载函数设为友元

 

     friend istream &operator>>(istream &L,My_string &R);

 

    ~My_string(); //析构函数

 

};

 

My_string::My_string():cstr(NULL),len(0){} //无参构造

 

My_string::My_string(const char *str) //有参构造

{

    len=strlen(str);

    cstr=new char[len+1];

    strcpy(cstr,str);

}

My_string::My_string(const My_string &other) //拷贝构造

{

    len=other.len;

    cstr=new char[len+1];

    strcpy(cstr,other.cstr);

}

 

My_string& My_string::operator=(const My_string &other)//拷贝赋值

{

    if(this->cstr!=NULL)

    {

        delete this->cstr;

    }

    len=other.len;

    this->cstr=new char[len+1];

    strcpy(this->cstr,other.cstr);

    return *this;

}

 

My_string &My_string::operator=(const char *other)

{

    if(this->cstr!=NULL)

    {

        delete []this->cstr;

    }

    len=strlen(other);

    this->cstr=new char[len+1];

    strcpy(this->cstr,other);

    return *this;

}

 

char &My_string::operator[](int index) //[]

{

    static char c=-1;

    if(index<0||index>this->len)

    {

        cout<<"段错误"<<endl;

        return c;

    }

    return cstr[index];

}

 

const My_string My_string::operator+(const My_string &other) const //+

{

    My_string temp;

    temp.len=this->len+other.len;

    temp.cstr=new char(len+1);

    strcpy(temp.cstr,this->cstr);

    strcat(temp.cstr,other.cstr);

    return temp;

}

 

const My_string My_string::operator+(const char *other) const

{

    My_string temp;

    temp.len=this->len+strlen(other);

    temp.cstr=new char(len+1);

    strcpy(temp.cstr,this->cstr);

    strcat(temp.cstr,other);

    return temp;

}

 

bool My_string::operator==(const My_string &other) const //==

{

    return !strcmp(this->cstr,other.cstr);

}

 

bool My_string::operator!=(const My_string &other) const //!=

{

    return strcmp(this->cstr,other.cstr);

}

 

bool My_string::operator<(const My_string &other) const //<

{

    if(strcmp(this->cstr,other.cstr)<0)

        return 1;

    return 0;

}

 

bool My_string::operator>(const My_string &other) const //>

{

    if(strcmp(this->cstr,other.cstr)>0)

        return 1;

    return 0;

}

 

bool My_string::operator<=(const My_string &other) const //<=

{

    if(strcmp(this->cstr,other.cstr)<=0)

        return 1;

    return 0;

}

 

bool My_string::operator>=(const My_string &other) const //>=

{

    if(strcmp(this->cstr,other.cstr)>=0)

        return 1;

    return 0;

}

 

ostream &operator<<(ostream &L,My_string &R)//插入运算符重载

{

    L<<R.cstr;

    return L;

}

 

istream &operator>>(istream &L,My_string &R)

{

    if(R.cstr!=NULL)

    {

        delete []R.cstr;

    }

    R.cstr=new char[128];

    L>>R.cstr;

    R.len=strlen(R.cstr);

    return L;

}

 

My_string::~My_string() //析构函数

{

    delete []cstr;

}

 

int main()

{

    My_string s1="jing";

    My_string s2="hao";

    My_string s4;

    cin>>s4;

    if(s1<s2)

    {

        cout<<"s1<s2"<<endl;

    }

    else

    {

        cout<<"s1>s2"<<endl;

    }

    cout<<s4;

    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值