c++实现简单的string类

本文介绍了一个自定义的C++字符串类mystring的实现,通过运算符重载,使该类能像内置string类一样进行比较、赋值等操作。详细展示了类的构造、析构、赋值、比较等核心函数的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学得快,忘得快,老师讲完了运算符重载这一节然后就带我们开发一个字符串类,我的代码也是在理解的基础上跟着老师敲的,觉得太有意思了!!!赶紧写博客总结一下,加深理解,希望我这记性别让我失望!其实字符串也是可以理解为字符数组,所以在类中有两个属性,字符串长度len和字符指针,当定义mystring 类时可以为字符指针申请空间。并将向存入的字符拷贝到刚申请空间的字符数组里!然后定义一系列运算符重载函数,实现像操作string 类实例一样操作mystring 类实例。
这是一个字符串类的头文件

#ifndef _MYSTRING_H_
#define __MYSTRING_H_
#include<string.h>
#include<iostream>
using namespace std;
class mystring{
//友元函数,输出流和输入流函数
    friend ostream&operator<<(ostream &out,mystring &s);
    friend istream&operator>>(istream &in ,mystring &s);
private:
    char* p ;
    int len ;
public:
    mystring();
    mystring(int len);
    mystring(const char * p);
    bool operator==(const mystring &s);
    mystring(const mystring &s);
    ~mystring();
//各种操作运算符函数
public:
    mystring& operator=(const mystring &s);
    mystring& operator=(const char *q);
    bool operator==(const char * s);
    char operator[](const int i);
    bool operator!=(const mystring &s);
    bool operator!=(const char * s);
    int operator>(const char *s);
    int operator>(const mystring&s);
    int operator<(const char* s);
    int operator<(const mystring &s);
};
//输入流函数,将所输入的字符串存到字符数组中
istream&operator>>(istream &in ,mystring &s){
    cin>>s.p;
    return in ;
}

int mystring::operator>(const mystring &s){
    return strcmp(p,s.p) ;
}

int mystring::operator<(const char*s){
    return strcmp(p,s) ;
}

int mystring::operator<(const mystring &s){
    return strcmp(p,s.p);
}

int mystring::operator>(const char*s){
    return strcmp(p,s);
}

bool mystring::operator!=(const mystring &s){

    if(!strcmp(p,s.p)){
        return false ;
    }
    return true ;
}

bool mystring::operator!=(const char * s){
    if(p==NULL){
        if(len ==0)
             return false ;
        else{
            return true ;
        }
    }
    if(!strcmp(p,s)){
        return false ;
    }
    else{
        return true;
    }
}
bool mystring::operator==(const mystring&s){

    if(strcmp(s.p,p)==0){
        return true;
    }
    else{
        return false;
    }
}

bool mystring::operator==(const char *s){
    if(s==NULL){
        if(len == 0){
            return true ;
        }
        else{
            return false ;
        }
    }
        if(strcmp(s,p)==0){
            return true ;
        }
        else{
            return false ;
        }
}

ostream&operator<<(ostream &out,mystring &s){
    out<<s.p;
    return out;
}

char mystring::operator[](const int i){
    return p[i];
}

mystring &mystring::operator=(const char*q){
    if(p!= NULL){
        delete[]p ;
        len = 0 ;
    }
    if(q==NULL){
        len = 0 ;
        p = new char[len+1];
        strcpy(p,"");
    }
    else{
        int lenth = strlen(q);
        p = new char[lenth];
        len = lenth ;
        len = strlen(q);
        strcpy(p,q);
    }
    return *this ;
}

mystring &mystring::operator=(const mystring&s){
    if(p != NULL){
        delete[]p;
        len = 0 ;
    }
    len = s.len ;
    p = new char[len+1];
    strcpy(p,s.p);
    return *this ;
}

mystring ::mystring(int len){
    if(len == 0){
        (*this).len = 0 ;
        p = strcpy(p,"");
    }
    else{
        (*this).len = len ;
        p = new char[len];
        bzero(p,strlen(p));
    }
} 

mystring::mystring(){
    len = 0 ;
     p = new char[len+1];
     strcpy(p,"");
}

mystring::mystring(const char *s){
    if(p==NULL){
        len = 0 ;
        p = new char[len+1];
        strcpy(p , "");
    }
    else{
        len = strlen(s);
        p = new char[len+1];
        strcpy(p,s);
    }
}

mystring::mystring(const mystring& s){
    int lenth = s.len;
    p = new char[lenth];
    strcpy(p,s.p);
}
//析构函数,只需将申请的字符数组释放掉即可
mystring::~mystring(){

    if( p != NULL){
        delete[]p ;
        len = 0 ;
    }
}
#endif

读者可以通过在main 函数中用mystring来像定义字符串那样定义一个mystring类,并且实现输入和输出等操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值