c++实现简单的string类

学得快,忘得快,老师讲完了运算符重载这一节然后就带我们开发一个字符串类,我的代码也是在理解的基础上跟着老师敲的,觉得太有意思了!!!赶紧写博客总结一下,加深理解,希望我这记性别让我失望!其实字符串也是可以理解为字符数组,所以在类中有两个属性,字符串长度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类,并且实现输入和输出等操作。

猜你喜欢

转载自blog.csdn.net/qq_41681241/article/details/84329841
今日推荐