码迷,mamicode.com
首页 > 其他好文 > 详细

自定义实现String类

时间:2015-05-14 18:51:59      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:实现string

自定义实现String类的内容
直接在代码中阐述:附有测试样例
代码:

#include <iostream>
//#include <cstring>
using namespace std;
class String
{
    public:
        //ctor
        String();
        String(const char* s);
        String(const char* s, size_t len);
        String(const String& rhs);
        String(size_t Len);

        String& operator=(const String& rhs);

        //dtor
        ~String();

        //get
        size_t size() const;
        const char* data() const;
        const char* c_str() const;

        // set & get
        char & operator[](size_t index);
        char operator[] (size_t index) const;

        //imp
        String operator +(const String& rhs);
        void operator +=(const String& rhs);


        //输出
        friend ostream& operator <<(ostream& out,const String& rhs);

    private:
        char*  data_;
        size_t len_;
};
    String::String()
        : data_(NULL), len_(0)
    {
        data_ = new char[1];
        data_[0] = ‘\0‘;
    }

    String::String(size_t Len)
    {
        data_ = new char[Len+1];
        for(size_t i=0;i<=Len;i++)
        {
            data_[i] = ‘\0‘;
        }
    }

    String::String( const char* s )
    {
        size_t i = 0;
        size_t len = 0;
        while(s[i] != ‘\0‘)
        {
            i++;
            len++;
        }
        data_ = new char[len+1];

        for(size_t j = 0;j < len;j++)
        {
            data_[j] = s[j];
        }
        data_[len] = ‘\0‘;
        len_ = len;
    }

    String::String( const char* s, size_t len )
    {
        data_ = new char[len+1];
        for(size_t k=0;k<len;k++)
        {
            data_[k] = s[k];
        }
        data_[len] = ‘\0‘;
        len_ = len;
    }

    String::String( const String& rhs )
    {
        size_t len = rhs.size();
        data_ = new char[len+1];
        for(size_t i=0;i<len;i++)
        {
            data_[i] = rhs.data_[i];
        }
        data_[len] = ‘\0‘;
        len_ = len;
    }

    String String::operator +(const String& rhs)
    {
        size_t totalLen = len_ + rhs.len_;
        String tmp(totalLen);
        size_t i,j;
        for(i=0;i<len_;i++)
        {
            tmp[i] = data_[i];
        }
        for(j=0;j<rhs.len_;j++,i++)
        {
            tmp[i] = rhs.data_[j];
        }
        return tmp;
    }

    void String::operator +=(const String& rhs)
    {
        size_t totalLen = len_ + rhs.len_;
        String tmp(totalLen);
        size_t i,j;
        for(i=0;i<len_;i++)
        {
            tmp[i] = data_[i];
        }
        for(j=0;j<rhs.len_;j++,i++)
        {
            tmp[i] = rhs.data_[j];
        }
        *this = tmp;
    }

    String& String::operator=( const String& rhs )
    {
        //比较对象的地址
        if(this == &rhs)
        {
            return *this;
        }

        delete[] data_;
        size_t len = rhs.size();
        data_ = new char[len+1];

        for(size_t i = 0;i < len; i++)
        {
            data_[i] = rhs.data_[i];
        }
        data_[len] = ‘\0‘;

        return *this;
    }

    String::~String()
    {
        if(data_)
        {
            delete[] data_;
            data_ = NULL;
        }
        len_ = 0;
    }

    size_t String::size() const
    {
        int i = 0;
        int len_ = 0;
        while(data_[i] != ‘\0‘)
        {
            i++;
            len_++;
        }
        return len_;
    }

    const char* String::data() const
    {
        return data_;
    }

    const char* String::c_str() const
    {

        return data_;
    }

    char & String::operator[](size_t index )
    {
        if(index > len_)
        {
            return data_[len_-1];
        }
        else
        {
            return data_[index];
        }
    }
    char String::operator[] (size_t index) const
    {
        if(index > len_)
        {
            return data_[len_-1];
        }
        else
        {
            return data_[index];
        }
    }
    //注意 友员不是成员函数 不用加上String::
    ostream& operator <<(ostream& out,const String& rhs)
    {
        out<< rhs.data_ << endl;
        return out;
    }
int main()
{
    String a("hello",3);
    cout << a ;
    String b("world");
    String c(b);
    cout << c ;
    cout << c[2] <<endl;
    cout << a+b;
    //cout << a << endl;
    //cout << a+b << endl;
    /*
    String a("hello");
    cout << a << endl; //这个是 重载的 <<
    char x[20];
    String c = "wolrd";
    strcpy(x,c.c_str());
    cout << x << endl;
    cout << "Hello world!" << endl;
    return 0;
    */
}

自定义实现String类

标签:实现string

原文地址:http://blog.csdn.net/xd_122/article/details/45722335

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!