zoukankan      html  css  js  c++  java
  • string 类的实现

    1.    #include<iostream>  
    2.    #include<iomanip>  
    3.    using namespace std;  
    4.      
    5.    class String{  
    6.         friend ostream& operator<< (ostream&,String&);//重载<<运算符  
    7.         friend istream& operator>> (istream&,String&);//重载>>运算符  
    8.    public:  
    9.        String(const char* str=NULL);                //赋值构造兼默认构造函数(char)  
    10.        String(const String &other);                 //赋值构造函数(String)  
    11.        String& operator=(const String& other);       //operator=  
    12.        String operator+(const String &other)const;  //operator+  
    13.        bool operator==(const String&);              //operator==  
    14.        char& operator[](unsigned int);              //operator[]  
    15.        size_t size(){return strlen(m_data);};  
    16.        ~String(void) {delete[] m_data;}  
    17.    private:  
    18.        char *m_data; // 用于保存字符串  
    19.    };  
    20.      
    21.    inline String::String(const char* str)     
    22.    {  
    23.        if(!str)m_data=0;      //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用  
    24.        else {  
    25.            m_data=new char[strlen(str)+1];  
    26.            strcpy(m_data,str);  
    27.        }  
    28.    }  
    29.      
    30.    inline String::String(const String &other)  
    31.    {  
    32.        if(!other.m_data)m_data=0;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)  
    33.        else   
    34.        {  
    35.            m_data=new char[strlen(other.m_data)+1];  
    36.            strcpy(m_data,other.m_data);  
    37.        }  
    38.    }  
    39.      
    40.    inline String& String::operator=(const String& other)  
    41.    {  
    42.        if (this!=&other)  
    43.        {  
    44.            delete[] m_data;  
    45.            if(!other.m_data) m_data=0;  
    46.            else  
    47.            {  
    48.                m_data = new char[strlen(other.m_data)+1];  
    49.                strcpy(m_data,other.m_data);  
    50.            }  
    51.        }  
    52.        return *this;  
    53.    }  
    54.    inline String String::operator+(const String &other)const  
    55.    {  
    56.        String newString;  
    57.        if(!other.m_data)  
    58.            newString = *this;  
    59.        else if(!m_data)  
    60.            newString = other;  
    61.        else  
    62.        {  
    63.            newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];  
    64.            strcpy(newString.m_data,m_data);  
    65.            strcat(newString.m_data,other.m_data);  
    66.        }  
    67.        return newString;  
    68.    }  
    69.      
    70.    inline bool String::operator==(const String &s)      
    71.    {  
    72.        if ( strlen(s.m_data) != strlen(m_data) )  
    73.            return false;  
    74.        return strcmp(m_data,s.m_data)?false:true;  
    75.    }  
    76.      
    77.    inline char& String::operator[](unsigned int e)  
    78.    {  
    79.        if (e>=0&&e<=strlen(m_data))  
    80.            return m_data[e];  
    81.    }  
    82.      
    83.    ostream& operator<<(ostream& os,String& str)  
    84.    {  
    85.        os << str.m_data;  
    86.        return os;  
    87.    }  
    88.      
    89.    istream &operator>>( istream &input, String &s )  
    90.    {  
    91.       char temp[ 255 ]; //用于存储输入流  
    92.       input>>setw(255)>>temp;  
    93.       s = temp; //使用赋值运算符  
    94.       return input; //使用return可以支持连续使用>>运算符  
    95.    }  
  • 相关阅读:
    Task的用法
    C# 反射调用方法
    C#常用公共方法
    Spire.Doc for .NET(1)
    C#异常Retry通用类
    Java面试中经常遇到的类执行顺序
    Tensorflow学习教程集合
    SSD——样本正负比控制+多尺度检测 (目标检测)(one-stage)(深度学习)(ECCV 2016)
    SSD算法详解 及其 keras实现
    SSD原理解读-从入门到精通
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/5312196.html
Copyright © 2011-2022 走看看