构造string类.cpp

/*构造string类*/
#include <iostream>
#include <string.h>
using namespace std;

class String {
//友元函数,该函数虽然不是类成员但是可以访问类中的所有成员
    friend ostream& operator<<(ostream& out, const String& s);
    friend istream& operator>>(istream& in, String& s);
public:
//无参构造函数
    String() : len(0), ptr(new char[1])
    {
        ptr[0] = '\0';
    }
//有参构造函数
    String(const char* s) : len(strlen(s)), ptr(new char[len+1])
    {
        strcpy(ptr, s);
    }
//拷贝构造函数(深拷贝)
    String(const String& s) : len(s.len), ptr(new char[len+1])
    {
        strcpy(ptr, s.ptr);
    }
//析构函数,用于销毁类在堆区申请的内存,类成员保存在栈区,可自动销毁
    ~String()
    {
        delete[] ptr;
    }
//重定义字符串长度
    void resize(int n)
    {
        char* p = new char[n+1]{'\0'};
        for (int i=0; i<n&&i<len; i++) {
            p[i] = ptr[i];
        }
        len = n;
        delete[] ptr;
        ptr = p;
    }
//操作符重载函数 +  用于拼接两个字符串
    String operator+(const char* s)
    {
        String tmp;
        tmp.resize(len + strlen(s));
        strcpy(tmp.ptr, ptr);
        strcat(tmp.ptr, s);
        return tmp;
    }
//操作符重载函数 +  用于拼接两个字符串
    String operator+(const String& s)
    {
        String tmp;
        tmp.resize(len + s.len);
        strcpy(tmp.ptr, ptr);
        strcat(tmp.ptr, s.ptr);
        return tmp;
    }

//操作符重载函数 - 用于将第二个字符串从第一个字符串中移除(如果有)
    String operator-(const char* s)
    {
//strstr 判断字符串2是否出现在字符串1中
        char* f = strstr(ptr, s);
        if (f == NULL) {
            return String(ptr);
        } else {
            String tmp;
            tmp.resize(len - strlen(s));
//strncpy 从字符串指定位置拷贝
            strncpy(tmp.ptr, ptr, f-ptr);
            strcat(tmp.ptr, f+strlen(s));
            return tmp;
        }
    }
//操作符重载函数 - 用于将第二个字符串从第一个字符串中移除(如果有)
    String operator-(const String& s)
    {
        char* f = strstr(ptr, s.ptr);
         if (f == NULL) {
            return String(ptr);
        } else {
        String tmp;
        tmp.resize(len - s.len);
        strncpy(tmp.ptr, ptr,f-ptr);
        strcat(tmp.ptr, f+s.len);
        return tmp;
        }
    }
//运算符重载函数 = 赋值运算
    String& operator=(const String& s)
    {
        operator=(s.ptr);
        return *this;
    }
//运算符重载函数 = 拷贝赋值运算
    String& operator=(const char* s)
    {
        delete[] ptr;
        len = strlen(s);
        ptr = new char[len+1]{'\0'};
        strcpy(ptr, s);
        return *this;
    }
//操作符重载函数 < 判断字符串的字典序
    bool operator<(const String& s)
    {
        return operator<(s.ptr);
    }
//操作符重载函数 < 判断字符串的字典序
    bool operator<(const char* s)
    {
        int ret = strcmp(ptr, s);
        return ret < 0;
    }
//返回字符串首字符地址
    const char* c_str()
    {
        return ptr;
    }
private:
    int len;
    char* ptr;
};
//重载操作符<<,用于输出类的实例
ostream& operator<<(ostream& out, const String& s)
{
    out << s.ptr;
    return out;
}
//重载操作符>>, 用于输入类的实例
istream& operator>>(istream& in, String& s)
{
    char* buf = new char[2048]{'\0'};
    
    cin >> buf;
    s.operator=(buf);
    delete[] buf;

    return in;
}
int main()
{
     String s("hello");
     cout << "s = " << s << endl;
     String s1 = s;
     cout << "s1 = " << s1 << endl;
     cout << "s1+ moring = " << s1 + " moring" << endl;
     String s2;
     cout << "Input s2 : ";
     cin >> s2;
     cout << "s2 = : " << s2 << endl;
     String s3 = s1+ s2 ;
     cout << "s1+s2 = " << s3 << endl;
     String s4 = "hello world";
     cout << "s4 =  " << s4 << endl;
     String s5 = s4 - s1;
     cout << "s4-s1 = " << s5 << endl;
     cout << "s4-h = " << s4 - "h" << endl;
     bool b1 = s2 < s1;
     cout << "b1 = " << b1 << endl;
     bool b2 = s1 < "z";
     cout << "b2 = " << b2 << endl;
     bool b3 = s1 < "a";
     cout << "b3 = " << b3 << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值