在my_sting中将能实现重载的运算符尽可能重载

my_string的代码

#include <iostream>
#include<cstring>

using namespace std;
class my_string
{
    private:
        char *cstr;
        int len;
    public:
    my_string():cstr(NULL),len(0)
    {

      //  cout << "是无参构造函数" <<endl;
    }       //无参构造函数
    my_string(const char *cstr)
    {
        this->len = strlen(cstr);

        this->cstr= new char [this->len+1];
        strcpy(this->cstr,cstr);

       // cout << "执行有参构造函数"<< endl;
    } //有参构造函数
    my_string(const my_string &other)
    {

        this->len= other.len;

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

        strcpy(this->cstr ,other.cstr);
     //   cout <<"执行拷贝构造函数" <<endl;
    }       //拷贝构造函数
    ~my_string()
    {
        delete []this->cstr;
      //  cout << "解析的地址" <<this <<endl;
     //   cout <<"执行解析构造函数"<< endl;
    }
    char*  show()       //返回cstr
    {
        return this->cstr;
    }
    //判空
    bool enpty()
    {
        return this->cstr==NULL?0:1;
    }
    //返回字符串长度
    int size()
    {
        return this->len;
    }
    char &at(int index)
    {
        if(index<0||index>this->len)
        {
            cout << "定位错误" <<endl;
        }
        return *(this->cstr+index);
    }
    //赋值运算符'='函数重载

    my_string &operator=(const my_string &R)   //给this对象= R
    {
        if(this->cstr==NULL)
        {
            //先申请空间
            this->cstr=new char[R.len+1];
            strcpy(this->cstr,R.cstr);
            this->len=R.len;

        }
        else
        {
            //不管两个对象的cstr哪个空间大,统一先释放空间在申请
            delete this->cstr;
            this->len =R.len;
            this->cstr=new char[this->len+1];
            strcpy(this->cstr,R.cstr);

        }
        //cout <<"执行赋值=函数重载" <<endl;
        return *this;
    }


    //赋值运算符‘+’函数重载
    my_string operator+(const my_string &R)const
    {
        //不管两个对象的cstr哪个空间大,先申请一片空间用来存两个字符串的拼接
        my_string  temp;
        temp.len=this->len+R.len;
        temp.cstr =new char[temp.len];
        strcpy(temp.cstr,this->cstr);
        strcat(temp.cstr,R.cstr);
        return temp;
    }
    //赋值运算符 ‘+=’函数重载
    const my_string &operator+=(const my_string &R)
    {
        my_string temp;
        temp.len= this->len+R.len+1;
        temp.cstr =new char [temp.len];
        strcpy(temp.cstr,this->cstr);
        strcat(temp.cstr,R.cstr);
        delete this->cstr;
        this->cstr=new char[temp.len];

        strcpy(this->cstr,temp.cstr);
        delete  temp.cstr;
        return *this;
    }

    //字符串相等,关系运算符重载
    bool  operator==(const my_string &R)const
    {
         if(strcmp(this->cstr,R.cstr)==0)
         {
             return 1;
         }
         else
         {
             return 0;
         }
    }
     //,关系运算符'!='重载
    bool  operator!=(const my_string &R)const
    {
         if(strcmp(this->cstr,R.cstr)!=0)
         {
             return 1;
         }
         else
         {
             return 0;
         }
     }
    //关系运算符'<'重载
     bool  operator<(const my_string &R)const
    {
        if(strcmp(this->cstr,R.cstr)<0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    //关系运算符‘>’重载
     bool  operator>(const my_string &R)const
    {
        if(strcmp(this->cstr,R.cstr)>0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
     //关系运算符‘<=’函数重载
     bool  operator<=(const my_string &R)const
     {
         if(strcmp(this->cstr,R.cstr)<=0)
         {
             return 1;
         }
         else
         {
             return 0;
         }
     }
     //关系运算符‘>=’函数重载
     bool  operator>=(const my_string &R)const
     {
         if(strcmp(this->cstr,R.cstr)>=0)
         {
             return 1;
         }
         else
         {
             return 0;
         }
     }

     friend const ostream &operator<<(ostream &O,const my_string &R);
     friend const istream &operator>>(istream &I, my_string &R);
     friend const istream &getline(istream &i, my_string &R);
};
//输出
 const ostream &operator<<( ostream &O,const my_string &R)
 {
     O <<strlen(R.cstr) << R.cstr<<endl;
     return O;
 }
 //输入
 const istream &operator>>(istream &i, my_string &R)
 {
     i>> R.cstr;
     R.len=strlen(R.cstr);
     return i;
 }
int main()
{

    my_string a="zhangsan";
    my_string b("lisi");
    my_string c;
    my_string d=a;
    my_string r;
    r="liside";
    cout <<"r=" <<r;
    c=a;
    cout <<c.show() <<endl;
    cout <<"****************" <<endl;
    my_string e;
    //算术运算符重载
    e=a+b;
    cout << e.show() <<endl;
    //关系运算符'=='重载
    if(a==a)
    {
        cout <<"相同" <<endl;
    }
    else
    {
        cout <<"不相同"<< endl;
    }
    cout<<a;
    cin >>a;
    cout <<a;
    b= a+b;
    cout <<b;
    string s;
    s="shang";

    cout <<s<<s.size() <<endl;

    return 0;
}

现象

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`grid_sting`是一个基于Python的库,用于实现网格STING算法(SpaTial clustING of appliCation with noise,具有噪声的应用空间聚类算法)。它是基于网格的聚类算法,将数据集根据网格划分成若干个小区域,然后对每个小区域进行聚类。 `grid_sting`库的主要功能包括: 1.将数据集根据网格划分成若干个小区域。 2.对每个小区域内的数据进行聚类,得到聚类簇。 3.将相邻的小区域合并成一个大区域,并更新大区域内的聚类簇。 4.支持不同的距离度量和聚类算法,可以根据实际需求进行配置。 下面是一个使用`grid_sting`库的简单例子: ```python import numpy as np import pandas as pd from grid_sting import GridSTING # 加载数据集 data = pd.read_csv('data.csv') # 数据预处理 X = data[['x', 'y']].values # 将数据集根据网格划分成若干个小区域 grid = GridSTING(X, epsilon=0.1, min_pts=5) # 对每个小区域内的数据进行聚类 grid.cluster() # 将相邻的小区域合并成一个大区域,并更新大区域内的聚类簇 grid.merge() # 输出聚类结果 print(grid.labels_) ``` 在这个例子中,我们使用了`grid_sting`库对一个二维数据集进行了聚类。具体来说,我们将数据集根据网格划分成若干个小区域,然后对每个小区域内的数据进行聚类。最后,我们将相邻的小区域合并成一个大区域,并更新大区域内的聚类簇。需要注意的是,这里我们使用了默认的距离度量和聚类算法。如果需要使用其他的距离度量和聚类算法,可以通过配置参数来实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值