Day40 4.21 C++

string类

#include <iostream>
#include <cstring>

using namespace std;


class mystring{
private:
    char *str;//记录c风格的字符串
    int size;//记录字符串的实际长度
public:
    //无参构造
    mystring():size(10)
    {
        str=new char[size];
        strcpy(str,"");
    }

    //有参构造
    mystring(const char*s)
    {
        size=strlen(s);
        str=new char[size+1];
        strcpy(str,s);

    }

    //拷贝构造
    mystring(mystring & other):str(new char(*(other.str))),size(other.size)
    {
        strcpy(str,other.str);
    }

    //拷贝赋值
    mystring &operator =(const mystring & other)
    {
        //排除给自己赋值
        if(this!=&other)
        {
            delete str;
            this->size=other.size;
            this->str=new char[size+1];
            strcpy(this->str,other.str);
        }
        return *this;
    }

    //析构函数
    ~mystring()
    {
        delete str;
    }

    //判空函数
    bool empty()
    {
        if(strlen(str)==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //size函数
    int length()
    {
        return size;
    }

    //c_str函数
    char* c_str()
    {
        return str;
    }

    //at函数
    char &at(int pos)
    {
        if(pos>=size||pos<0)
        {
            cout<<"error pos"<<endl;
        }

        return str[pos];
    }

    //成员函数实现[]重载
    char &operator [](const int &pos)const
    {
        if(pos<=size && pos>=0)
        {
            return str[pos];
        }
        else
        {
            cout<<"error pos"<<endl;
            return str[pos];
        }
    }

    //成员函数实现+重载
    const mystring operator +(const mystring &other)const
    {
        mystring t;
        t.size=this->size+other.size;
        t.str=new char[t.size+1];
        strcpy(t.str,this->str);
        strcat(t.str,other.str);
        return t;
    }



    //成员函数实现+=重载
    mystring &operator +=(const mystring & other)
    {
        //判断s1是否等于他本身
        if(this == &other)
        {
            char* t=new char[this->size];     //申请空间存储s1
            strcpy(t,this->str);              //拷贝s1
            delete []this->str;               //释放原来存储s1的空间
            this->size=other.size+this->size; //size=s1的size+s2的size
            this->str=new char[this->size+1]; //申请空间存储s1+s2
            memset(str,0,this->size);
            strcpy(this->str,t);              //拷贝s1
            strcat(this->str,t);              //链接s2
            delete []t;                       //释放用于存储s1的空间
        }
        else
        {
            char* t=new char[this->size];     //申请空间存储s1
            strcpy(t,this->str);              //拷贝s1
            delete []this->str;               //释放原来存储s1的空间
            this->size=other.size+this->size; //size=s1的size+s2的size
            this->str=new char[this->size+1]; //申请空间存储s1+s2
            memset(str,0,this->size);
            strcpy(this->str,t);              //拷贝s1
            strcat(this->str,other.str);      //链接s2
            delete []t;                       //释放用于存储s1的空间
        }
        return *this;
    }

    //成员函数实现==重载
    bool operator ==(const mystring &other)const
    {
        if(0==strcmp(this->str,other.str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //成员函数实现!=重载
    bool operator !=(const mystring &other)const
    {
        if(0!=strcmp(this->str,other.str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //成员函数实现<重载
    bool operator <(const mystring &other)const
    {
        if(strcmp(this->str,other.str)<0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //成员函数实现>重载
    bool operator >(const mystring &other)const
    {
        if(strcmp(this->str,other.str)>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    friend istream &operator>>(istream &L,mystring &other);

    friend ostream &operator<<(ostream &cout,mystring &other);

};

//全局函数实现>>重载
istream & operator>>(istream &L,mystring &other)
{
    char s[other.length()];
    cin>>s;
    strcpy(other.c_str(),s);
    return L;
}

//全局函数实现<<重载
ostream & operator<<(ostream &cout,mystring &other)
{
    cout<<other.str<<endl;
    return cout;
}

int main()
{
    //有参构造
    mystring str1("Hello World!");
    cout<<"str1:"<<str1.c_str()<<endl;
    cout<<endl;

    //拷贝构造
    mystring str2(str1);
    cout<<"str2:"<<str2<<endl;
    cout<<endl;

    //    //判空
    //    cout<<"empty:"<<boolalpha<<str2.empty()<<endl;
    //    cout<<endl;

    //    //size函数
    //    cout<<"size:"<<str2.length()<<endl;
    //    cout<<endl;

    //    //c_str函数
    //    cout<<"c_str:"<<str2.c_str()<<endl;
    //    cout<<endl;

    //    //at函数
    //    str2.at(4)='O';
    //    cout<<"at(4):"<<str2.at(4)<<endl;
    //    cout<<"str2:"<<str2.c_str()<<endl;
    //    cout<<endl;

    // +
    mystring str3;
    str3=str1+str2;
    cout<<"str1+str2:"<<str3<<endl;
    cout<<endl;

    //+=
    cout<<"str1+=str1:"<<(str1+=str1)<<endl;
    cout<<endl;

    cout<<"str2+=str1:"<<(str2+=str1)<<endl;
    cout<<endl;
    // <
    cout<<"str1<str2:"<<boolalpha<<(str1<str2)<<endl;
    cout<<endl;

    // >
    cout<<"str1>str2:"<<boolalpha<<(str1>str2)<<endl;
    cout<<endl;

    // =
    cout<<"str1=str2:"<<boolalpha<<(str1==str2)<<endl;
    cout<<endl;

    // !=
    cout<<"str1!=str2:"<<boolalpha<<(str1!=str2)<<endl;
    cout<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: U盘芯片检测V4.21是一种软件工具,用于检测U盘芯片型号。当我们使用U盘时,可能会遇到一些问题,如写入速度缓慢、读取数据失败等。这些问题可能源于U盘芯片型号不匹配,或者U盘本身存在一些质量问题。因此,使用U盘芯片检测V4.21可以帮助我们确定U盘芯片型号,从而更好地解决U盘问题。 U盘芯片检测V4.21可以通过简单的操作来完成检测过程。只需要将U盘插入电脑,运行该软件,点击检测按钮,即可获取U盘芯片型号以及U盘的相关信息。此外,该软件还具有修复U盘功能,可以修复一些U盘读取和写入数据的问题,极大提高了U盘的使用效率。 总之,使用U盘芯片检测V4.21能够帮助我们有效地识别U盘芯片型号,准确找出U盘问题的原因,提高U盘的读写效率,保护我们的数据安全。 ### 回答2: U盘芯片检测V4.21是U盘芯片检测软件的版本号,它是用于检测U盘芯片型号和厂家的小型软件。这个软件可以让用户在U盘出现问题时,快速识别U盘是否故障,从而方便用户更好地解决问题。 在使用U盘芯片检测V4.21之前,需要先将U盘插入计算机USB接口中,并等待系统自动识别。接着打开软件,点击"开始检测"按钮,软件会自动对U盘进行识别。在识别的过程中,软件会显示出U盘的型号、容量、生产厂家和芯片详细信息等。 如果U盘芯片检测V4.21显示出U盘故障,可能是由于芯片损坏、读写头出现问题、接触不良等原因引起的。此时需要根据具体的原因进行相应的处理,如更换芯片、清洁接触点等。 总之,U盘芯片检测V4.21是一个十分实用的工具,能够帮助用户快速检测U盘的芯片型号和工作状态。在使用过程中,需注意保持软件和系统的兼容性,以免造成不必要的损失。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值