zoukankan      html  css  js  c++  java
  • String类运算符重载,自己实现

    #include <iostream>
    #include<stdlib.h>
    #include<stdio.h>
    #include<sstream>
    #include<string.h>
    using namespace std;
    class String
    {
            private:
                    char *pstr_;
            public:
                    String();
                    String(const char *);
                    String(const String&);
                    ~String();
                    String & operator = (const String &);
                    String & operator = (const char *);

                    String & operator += (const String &);
                    String & operator += (const char *);

                    char & operator [] (std::size_t index);
                    const char & operator [](std::size_t index)const;

                    std::size_t size() const;
                    const char* c_str() const;

                    friend bool operator == (const String &,const String &);
                    friend bool operator != (const String &,const String &);

                    friend bool operator < (const String &,const String &);
                    friend bool operator > (const String &,const String &);
                    friend bool operator <= (const String &,const String &);
                    friend bool operator >= (const String &,const String &);

                    friend std::ostream & operator << (std::ostream & os,const String &s);
                    friend std::istream & operator >> (std::istream & is,String & s);
    };
    String operator + (const String &,const String &);
    String operator + (const String &,const char *);
    String operator + (const char *,const String &);

    String::String()
    {
            cout<<"String()"<<endl;
            pstr_=new char[1];
            strcpy(pstr_,"");
    }
    String::~String()
    {
            cout<<"~String()"<<endl;
            delete []pstr_;
    }
    String::String(const char *str)
    {
            cout<<"String(const char *)"<<endl;
            pstr_=new char[strlen(str)+1];
            strcpy(pstr_,str);
    }
    String :: String(const String & s)
    {
            cout<<"String(const String &)"<<endl;
            pstr_=new char[strlen(s.pstr_)+1];
            strcpy(pstr_,s.pstr_);
    }
    String & String::operator = (const String & s)
    {
            cout<<"String & operator =(const String &)"<<endl;
            this->~String();
            pstr_=new char[strlen(s.pstr_)+1];
            strcpy(pstr_,s.pstr_);
            return *this;
    }
    String & String::operator =(const char *s)
    {
            cout<<"String & operator=(const char *)"<<endl;
            this->~String();
            pstr_=new char[strlen(s)+1];
            strcpy(pstr_,s);
            return *this;
    }
    String & String::operator +=(const String &s)
    {
            cout<<"String & operator += (const String &)"<<endl;
            int len1=strlen(this->pstr_);
            int len2=strlen(s.pstr_);
            char *a=(char*)calloc(len1,sizeof(char));
            strcpy(a,this->pstr_);
            this->~String();
            this->pstr_=new char[len1+len2+1];
            sprintf(this->pstr_,"%s%s",a,s.pstr_);
            return *this;
    }
    String & String::operator += (const char *s)
    {
            cout<<"String & operator += (const char *)"<<endl;
            int len1=strlen(this->pstr_);
            int len2=strlen(s);
            char *a=(char*)calloc(len1,sizeof(char));
            strcpy(a,this->pstr_);
            this->~String();
            this->pstr_=new char[len1+len2+1];
            sprintf(this->pstr_,"%s%s",a,s);
            return *this;
    }
    std::ostream &  operator<<(std::ostream & os,const String &s)
    {
            cout<<"std::ostream & operator << (std::ostream & ,const String &)"<<endl;
            os<<s.pstr_;
            return os;
    }
    char& String::operator[] (std::size_t index)
    {
            cout<<"char & operator[] (std:: size_t index)"<<endl;
            int len=strlen(this->pstr_);
            char *a=(char*)calloc(len,sizeof(char));
            strcpy(a,pstr_);
            return a[index];
    }




    const char& String::operator[](std::size_t index)const
    {
            cout<<"const char & operator[] (std:: size_t index)const"<<endl;
            int len=strlen(this->pstr_+1);      //这里错了,this->pstr_+1相当于指正偏移到下一个字符
            char *a=(char*)calloc(len,sizeof(char));
            strcpy(a,pstr_);
            return a[index];
    }
    std::size_t String::size()const
    {
            cout<<"std::size_t size()const"<<endl;
            int len=strlen(this->pstr_);
        return len;
    }
    const char* String::c_str() const
    {
            cout<<"const char* c_str() const"<<endl;
            int len=strlen(this->pstr_+1);
            char *a=(char*)calloc(len,sizeof(char));  
    //这里不管申请多大的空间,后面拷贝函数如果不够他都会自动在申请空间所以结果没出现错误
            strcpy(a,pstr_);
            return a;
    }
    bool operator == (const String & a1,const String &a2)
    {
            cout<<"bool operator == (const String &,const String &)"<<endl;
            if((strcmp(a1.c_str(),a2.c_str()))==0)
                            return 1;
            return 0;
    }
    bool operator != (const String & a1,const String & a2)
    {
            cout<<"bool operator != (const String &,const String &)"<<endl;
            if((strcmp(a1.c_str(),a2.c_str()))!=0)
                            return 1;
            return 0;
    }
    bool operator < (const String &a1,const String &a2)
    {
            cout<<"bool operator < (const String &,const String &)"<<endl;
            if((strcmp(a1.c_str(),a2.c_str()))<0)
                            return 1;
            return 0;
    }
    bool operator > (const String &a1,const String &a2)
    {
            cout<<"bool operator > (const String &,const String &)"<<endl;
            if((strcmp(a1.c_str(),a2.c_str()))>0)
                            return 1;
            return 0;
    }
    bool operator <= (const String &a1,const String &a2)
    {
            cout<<"bool operator <= (const String &,const String &)"<<endl;
            if((strcmp(a1.c_str(),a2.c_str()))<0)
                            return 1;
            else if((strcmp(a1.c_str(),a2.c_str()))==0)
                            return 1;
            return 0;
    }
    bool operator >= (const String &a1,const String &a2)
    {
            cout<<"bool operator >= (const String &,const String &)"<<endl;
            if((strcmp(a1.c_str(),a2.c_str()))>0)
                            return 1;
            else if((strcmp(a1.c_str(),a2.c_str()))==0)
                            return 1;
            return 0;
    }
    std::istream & operator >> (std::istream & is,String &s)
    {
            cout<<"std::istream & operator >> (std::istream &m,String &)"<<endl;
            is>>s.pstr_;
            return is;
    }
    String operator + (const String &a1,const String &a2)
    {
            cout<<"String operator + (const String &,const String &)"<<endl;
            String tmp=a1;
            tmp+=a2;
            return tmp;
    }
    String operator + (const String &a1,const char * a2)
    {
            cout<<"String operator + (const String &,const char *)"<<endl;
            String tmp=a1;
            tmp+=a2;
            return tmp;
    }
    String operator + (const char *a1,const String & a2)
    {
            cout<<"String operator + (const char *,const String &)"<<endl;
            String tmp;
            tmp+=a1;
            tmp+=a2;
            return tmp;
    }

    //测试代码
    int main()
    {
            String s;
            String s1("hello");
            String s2(s1);
            String s3="world";
            s1=s2;
            String s4;
            s4="wangdao";
            s+=s3;
            s4+="nihao";
            cout<<s4<<endl;
            cout<<s4[2]<<endl;
            cout<<s4.size()<<endl;
            cout<<s4.c_str()<<endl;
            bool bool1=(s1>s4);
            cout<<bool1<<endl;
            bool bool2=(s1>=s3);
            cout<<bool2<<endl;
            cin>>s;
            cout<<s<<endl;
            String s5=s1+s2;
            cout<<s5<<endl;
            String s6=s1+"shenzhen";
            cout<<s6<<endl;
            String s7="shenzhen1"+s1;
            cout<<s7<<endl;
            return 0;
    }
  • 相关阅读:
    Vi/Vim查找替换使用方法【转】
    ios开发--NSDate与NSDateFormatter的相关用法【转】
    IOS开发基础之—MD5加密算法【转】
    struts2多图片上传实例【转】
    IOS开发之----常用的基本GDB命令【转】
    WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks
    Xcode8 NSLog打印json不完整的解决方案
    Swift 3.1 的一些新特性
    iOS中书写代码规范35条小建议
    女程序员做了个梦,众网友的神回复
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8915613.html
Copyright © 2011-2022 走看看