首页 > 代码库 > 自己实现C++的string类

自己实现C++的string类

使用C++的朋友对string类都不会陌生,string类使用起来很是方便,比C的字符数组好用多了,但是我们能不能自己实现一个属于自己的string类呢?

实现一个具有基本功能的string类并不难,但是还是得掌握以下几点知识:构造函数、析构函数、运算符重载、指针、引用等等。

我自己实现了一个string类,一来是想用自己学的知识做一些有实际作用的东西,二则使用自己的东西也很有成就感,现在给大家作为参考,有错误或者不合理的地方欢迎大家指正。

为了区别标准库中的string类和相关标识符,我的类名叫mystring,而相关函数名也大多以这种方式处理,其中每个函数都经过本人测试,在参数合法的前提下均能正常使用,但组合在一起使用的情况则没有进行太多测试。

首先是mystring.h文件

#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED

#include <iostream>

using namespace std;

class mystring
{
public:
    mystring();
    mystring(const char * ptr_s);
    mystring(const mystring & refer_str);//拷贝构造函数
    int mylength();//计算字符串的长度
    ~mystring();
    friend istream & operator>>(istream & is, mystring & refer_str);
    friend ostream & operator<<(ostream & os, const mystring & refer_str);
    mystring & operator=(const mystring & refer_str);
    void mygetline();//对输入按行读取
    char & operator[](int n);
    friend mystring operator+(const mystring & refer_str_1, const mystring & refer_str_2);
    mystring & operator+=(const mystring & refer_str);
    friend bool operator==(const mystring & refer_str_1, const mystring & refer_str_2);
    friend bool operator!=(const mystring & refer_str_1, const mystring & refer_str_2);
    mystring mysubstr(int pos, int posn);//计算一个字符串的子串,pos为起始位置,从0开始,posn为希望求得的子串的长度
    friend void mystrswap(mystring & str1, mystring & str2);//交换两个字符串
    int myfind(char ch, int pos = 0);//从pos开始查找字符ch在当前字符串中的位置,pos从0开始,返回值也从0开始
    mystring & myerase(int pos, int posn);//从pos开始删除当前字符串中的字符,pos从0开始,posn表示希望删除的字符数量,返回删除后字符串的引用,也即当前字符串
    mystring & myinsert(int pos, const mystring & refer_str);//将refer_str代表的字符串插入到当前字符串中,pos表示插入的位置,0 <= pos <= mystrlen((*this).ptr_s)
    mystring & myinsert(int pos, const mystring & refer_str, int posn);//将refer_str代表的字符串插入到当前字符串中,pos表示插入的位置,0 <= pos <= mystrlen((*this).ptr_s),0 <= posn <= mystrlen(refer_str.ptr_s)
private:
    char * ptr_s;
};

int mystrlen(const char * ptr_s);//计算字符串的长度
void mystrcpy(char * ptr_s_1, const char * ptr_s_2);//将ptr_s_2指向的内容复制到ptr_s_1所指向的空间,但前提后者空间足够
int mystrcmp(const char * ptr_s_1, const char * ptr_s_2);//比较ptr_s_1指向的内容和ptr_s_2指向的内容,若两者相同返回0,前者大返回1,前者小返回-1
void mystrcat(char * ptr_s_1, const char * ptr_s_2);//将ptr_s_2指向的内容连接到ptr_s_1指向的内容的后面,但需保证ptr_s_1所指向的空间足够,并且ptr_s_1所指向的空间必须经过初始化,即包含有意义的字符串结束符\0

#endif // MYSTRING_H_INCLUDED

我的想法是尽量不使用标准库,但为了重载>>和<<这两个运算符并实现mygetline()函数,还是得在mystring.h中包含iostream。

然后是mystring.cpp文件

#include "mystring.h"

mystring::mystring()
{
    ptr_s = new char[1];
    *ptr_s = \0;
}

mystring::mystring(const char * ptr_s)
{
    this->ptr_s = new char[mystrlen(ptr_s)+1];
    mystrcpy(this->ptr_s, ptr_s);
}

mystring::mystring(const mystring & refer_str)
{
    ptr_s = new char[mystrlen(refer_str.ptr_s)+1];
    mystrcpy(ptr_s, refer_str.ptr_s);
}

int mystring::mylength()
{
    return mystrlen(ptr_s);
}

mystring::~mystring()
{
    delete [] ptr_s;
}

istream & operator>>(istream & is, mystring & refer_str)
{
    delete [] refer_str.ptr_s;
    char ch_tmp[1000];// 申请一块在一般情况下足够大的内存
    is >> ch_tmp;
    refer_str.ptr_s = new char[mystrlen(ch_tmp)+1];
    mystrcpy(refer_str.ptr_s, ch_tmp);
    return is;
}

ostream & operator<<(ostream & os, const mystring & refer_str)
{
    os << refer_str.ptr_s;
    return os;
}

mystring & mystring::operator=(const mystring & refer_str)
{
    if(this == &refer_str)
        return *this;
    else
    {
        delete [] ptr_s;
        ptr_s = new char[mystrlen(refer_str.ptr_s)+1];
        mystrcpy(ptr_s, refer_str.ptr_s);
        return *this;
    }
}

void mystring::mygetline()
{
    delete [] ptr_s;
    char ch_tmp[1000];// 申请一块在一般情况下足够大的内存
    cin.getline(ch_tmp, 1000);
    ptr_s = new char[mystrlen(ch_tmp)+1];
    mystrcpy(ptr_s, ch_tmp);
}

char & mystring::operator[](int n)
{
    return *(ptr_s+n);
}

mystring operator+(const mystring & refer_str_1, const mystring & refer_str_2)
{
    mystring str_tmp;
    str_tmp.ptr_s = new char[mystrlen(refer_str_1.ptr_s)+mystrlen(refer_str_2.ptr_s)+1];
    mystrcpy(str_tmp.ptr_s, refer_str_1.ptr_s);
    mystrcpy(str_tmp.ptr_s+mystrlen(refer_str_1.ptr_s), refer_str_2.ptr_s);
    return str_tmp;
}

mystring & mystring::operator+=(const mystring & refer_str)
{
    mystring str_tmp;
    str_tmp.ptr_s = new char[mystrlen(ptr_s)+mystrlen(refer_str.ptr_s)+1];
    mystrcpy(str_tmp.ptr_s, ptr_s);
    mystrcpy(str_tmp.ptr_s+mystrlen(ptr_s), refer_str.ptr_s);
    delete [] ptr_s;
    ptr_s = new char[mystrlen(str_tmp.ptr_s)+1];
    mystrcpy(ptr_s, str_tmp.ptr_s);
    return *this;
}

bool operator==(const mystring & refer_str_1, const mystring & refer_str_2)
{
    int ret = mystrcmp(refer_str_1.ptr_s, refer_str_2.ptr_s);
    if(ret == 0)
        return true;
    else
        return false;
}

bool operator!=(const mystring & refer_str_1, const mystring & refer_str_2)
{
    int ret = mystrcmp(refer_str_1.ptr_s, refer_str_2.ptr_s);
    if(ret == 0)
        return false;
    else
        return true;
}

mystring mystring::mysubstr(int pos, int posn)
{
    mystring str_tmp;
    delete [] str_tmp.ptr_s;
    str_tmp.ptr_s = new char[posn+1];
    for(int i = 0;i < posn;i++)
        *(str_tmp.ptr_s+i) = *(ptr_s+pos+i);
    *(str_tmp.ptr_s+posn) = \0;
    return str_tmp;
}

void mystrswap(mystring & str1, mystring & str2)
{
    char * ptr_s;
    ptr_s = str1.ptr_s;
    str1.ptr_s = str2.ptr_s;
    str2.ptr_s = ptr_s;
}

int mystring::myfind(char ch, int pos)
{
    int str_length = mystrlen(ptr_s);
    int i;
    for(i = 0;i < str_length-pos;i++)
        if(*(ptr_s+pos+i) == ch)
            return pos+i;
    return -1;
}

mystring & mystring::myerase(int pos, int posn)
{
    mystring str_tmp;
    delete [] str_tmp.ptr_s;
    int str_length = mystrlen(ptr_s);
    str_tmp.ptr_s = new char[str_length-posn+1];
    for(int i = 0;i < pos;i++)
        *(str_tmp.ptr_s+i) = *(ptr_s+i);
    for(int i = pos+posn;i < str_length;i++)
        *(str_tmp.ptr_s+i-posn) = *(ptr_s+i);
    *(str_tmp.ptr_s+str_length-posn) = \0;
    char * ptr_s_tmp;
    ptr_s_tmp = str_tmp.ptr_s;
    str_tmp.ptr_s = ptr_s;
    ptr_s = ptr_s_tmp;
    return *this;
}

mystring & mystring::myinsert(int pos, const mystring & refer_str)
{
    mystring str_tmp;
    delete [] str_tmp.ptr_s;
    int str_length_1 = mystrlen(ptr_s);
    int str_length_2 = mystrlen(refer_str.ptr_s);
    str_tmp.ptr_s = new char[str_length_1+str_length_2+1];
    int i;
    for(i = 0;i < pos;i++)
        *(str_tmp.ptr_s+i) = *(ptr_s+i);
    mystrcpy(str_tmp.ptr_s+i, refer_str.ptr_s);
    while(i < str_length_1)
    {
        *(str_tmp.ptr_s+str_length_2+i) = *(ptr_s+i);
        i++;
    }
    *(str_tmp.ptr_s+str_length_1+str_length_2) = \0;
    char * ptr_s_tmp;
    ptr_s_tmp = str_tmp.ptr_s;
    str_tmp.ptr_s = ptr_s;
    ptr_s = ptr_s_tmp;
    return *this;
}

mystring & mystring::myinsert(int pos, const mystring & refer_str, int posn)
{
    mystring str_tmp;
    delete [] str_tmp.ptr_s;
    int str_length = mystrlen(ptr_s);
    str_tmp.ptr_s = new char[str_length+posn+1];
    int i;
    for(i = 0;i < pos;i++)
        *(str_tmp.ptr_s+i) = *(ptr_s+i);
    for(int j = 0;j < posn;j++)
        *(str_tmp.ptr_s+pos+j) = *(refer_str.ptr_s+j);
    while(i < str_length)
    {
        *(str_tmp.ptr_s+posn+i) = *(ptr_s+i);
        i++;
    }
    *(str_tmp.ptr_s+str_length+posn) = \0;
    char * ptr_s_tmp;
    ptr_s_tmp = str_tmp.ptr_s;
    str_tmp.ptr_s = ptr_s;
    ptr_s = ptr_s_tmp;
    return *this;
}

int mystrlen(const char * ptr_s)
{
    int length = 0;
    while(*(ptr_s++) != \0)
        length++;
    return length;
}

void mystrcpy(char * ptr_s_1, const char * ptr_s_2)
{
    while(*ptr_s_2 != \0)
    {
        *ptr_s_1 = *ptr_s_2;
        ptr_s_1++;
        ptr_s_2++;
    }
    *ptr_s_1 = \0;
}

int mystrcmp(const char * ptr_s_1, const char * ptr_s_2)
{
    int length_1 = mystrlen(ptr_s_1);
    int length_2 = mystrlen(ptr_s_2);
    int length_shorter = length_1 > length_2 ? length_2 : length_1;
    int i;
    for(i = 0;i < length_shorter;i++)
        if(*(ptr_s_1+i) != *(ptr_s_2+i))
            break;
    if(i == length_shorter)
    {
        if(length_1 == length_2)
            return 0;
        else if(length_1 > length_2)
            return 1;
        else
            return -1;
    }
    else
    {
        if(*(ptr_s_1+i) > *(ptr_s_2+i))
            return 1;
        else
            return -1;
    }
}

void mystrcat(char * ptr_s_1, const char * ptr_s_2)
{
    ptr_s_1 += mystrlen(ptr_s_1);
    while(*ptr_s_2 != \0)
    {
        *ptr_s_1 = *ptr_s_2;
        ptr_s_1++;
        ptr_s_2++;
    }
    *ptr_s_1 = \0;
}

实现时很多地方用到了指针,指针确实非常强大。

这样只要将这两个文件放在目标工程文件夹里,并且在开头加上

#include "mystring.h"

就可以使用自己的string类了,如果自己需要什么个性化的功能,还可以自由添加修改。

自己实现C++的string类