• string的模拟实现


    一、整体框架

    1、代码

    namespace snow
    {
        class string
        {
        public:
            static const size_t npos = -1;
        private:
            char* _str;
            size_t _capacity;
            size_t _size;
        };
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、实现原理

    • 使用命名空间的方式,避免模拟实现的string类与C++库里面的string冲突。
    • 定义一个size_t(无符号整型)的npos,赋值为-1,即整型的最大值。
    • 使用三个成员变量实现string类的操作,_str的作用是指向在堆上开辟的空间,string类的数据都存放在这块空间上;_capacity是记录开辟的空间能存放多少个元素,但实际开辟的空间会比_capacity多一个元素空间,用于存储\0;_size是记录当前开辟的空间内实际存放了多少个元素。

    3、注意

    • 使用static const修饰npos时,不加const修饰,该变量为静态变量,需要在类内声明类外定义;但如果加了const修饰,就需要在类内声明时定义,但这种做法只适用于size_t类型。

    二、构造函数

    1、代码

    string(const char* str = "")
    {
        _size = strlen(str);
        _capacity = _size;
        _str = new char[_capacity + 1];
        strcpy(_str,str);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、实现原理

    • 用一个空的的字符串作为形参,使构造函数成为无参构造函数。
    • 计算形参str的大小并赋值给_size,而_capacity赋值为_size。
    • 最后用strcpy将str的内容拷贝给_str。

    3、注意

    • 本函数和后续的函数都是放在string类中且用public限定符限定的,后续不再说明,如不是会另外说明。
    • 对于_capacity,无需用三目运算符_size == 0 ? 4 : _size为它赋值,因为下面开辟空间时会多开辟一个元素空间,不会有开辟0个空间的情况发生。
    • 用strcpy拷贝时,会将被拷贝字符串的\0也拷贝过去,所以,无需自己手动增加\0,即不用写_str[_size] = ‘\0’;语句。

    三、析构函数

    1、代码

    ~string()
    {
        delete[]_str;
        _str = nullptr;
        _size = _capacity = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、实现原理

    • 因为在构造函数开辟空间的时候是用new[]开辟的,所以在析构的时候,要用对应的delete[]去释放这块空间。
    • 最后为了安全,将_str置为空和将_size与_capacity都置为0。

    四、swap和拷贝构造函数

    1、代码

    void swap(string& s)
    {
        ::swap(_str, s._str);
        ::swap(_size, s._size);
        ::swap(_capacity, s._capacity);
    }
    
    string(const string& s)
        :_str(nullptr)
        ,_size(0)
        ,_capacity(0)
    {
        string tmp(s._str);
        swap(tmp);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、实现原理

    • 实现一个swap交换函数,方便后续两个string对象的交换。
    • 使用std::swap或::swap的意思是使用C++库里面的swap函数,因为交换的是内置类型的变量,所以调用库里面的swap函数进行交换会比较方便。
    • 因为无论如何,编译器都会执行初始化列表对成员变量进行初始化,所以用初始化列表对本对象的成员变量进行初始化。
    • 用被拷贝的对象s创建一个tmp对象,再调用swap函数将tmp对象和本对象进行交换。
    • 因为构造函数能创建出一个string对象,而出了拷贝构造函数的作用域,tmp对象会自动调用析构函数,所以无需自己编写拷贝的代码和手动调用析构函数。

    五、赋值运算符重载函数

    1、代码

    string& operator=(const string& s)
    {
        if (this != &s)
        {
            string tmp(s);
            swap(tmp);
        }
        return *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、实现原理

    • 对s和本对象进行判断,如果相同就无需进行操作,因为无意义;反之,实现方法与上方拷贝构造函数一致。
    • 最后返回*this,即返回本对象,实现支持对象连续赋值的功能。

    六、resize函数

    1、代码

    void resize(size_t n, char c = '\0')
    {
        if (n <= _size)
        {
            _size = n;
        }
        else
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                _capacity = n;
                strcpy(tmp, _str);
                delete[] _str;
                _str = tmp;
            }
            for (_size; _size < n; ++_size)
            {
                _str[_size] = c;
            }
        }
        _str[_size] = '\0';
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2、实现原理

    • 当欲调整的大小n比_size小或相等时,直接将_size改为n即可。
    • 当欲调整的大小n比_size大时,需要考虑是否需要扩容,如果n比_capacity大,就需要进行扩容。无论扩不扩容,都需要为多出的元素空间进行初始化,即赋值为c。
    • 最后,将_size位置的元素赋值为\0。

    3、注意

    • 在进行扩容时,需要用临时变量tmp接收扩容后的地址,因为如果new扩容失败,会直接抛出异常,而接收地址的变量也会改变,如果此时接收地址的变量为本对象的_str,会导致_str被修改而无法找到之前的地址,并且本对象的_str指向的空间会造成内存泄漏。
    • 为了防止意外,释放和修改_str的操作都应该放在最后进行。

    七、reserve函数

    1、代码

    void reserve(size_t n)
    {
        if (_capacity < n)
        {
            char* tmp = new char[n + 1];
            strcpy(tmp, _str);
            delete[] _str;
            _str = tmp;
            _capacity = n;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、实现原理

    • 当n比_capacity大时,就进行扩容,反之,不进行任何操作,因为使用new和delete操作空间的开销比较大。所以,只进行扩容而不进行缩容。
    • 扩容的操作与上方的resize函数一致。

    八、c_str函数

    1、代码

    const char* c_str()const
    {
        return _str;
    }
    
    • 1
    • 2
    • 3
    • 4

    2、实现原理

    • 因为c_str是将string对象转换为C类型的字符串,而不会对它进行修改,所以返回类型和this指针都用const进行修饰。
    • 因为需要返回C类型的字符串,而_str是一个字符指针,符合返回要求,所以直接返回_str就行,无需进行其他操作。

    九、[]运算符

    1、代码

    char& operator[](size_t index)
    {
        assert(index < _size);
        return _str[index];
    }
    const char& operator[](size_t index)const
    {
        assert(index < _size);
        return _str[index];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、实现原理

    • 因为[]需要返回对应位置元素的引用,所以对下标有所要求,如果下标大于或等于_size时,说明是越界访问。因此,加上assert进行断言,如果不符合要求,直接报错而不进行下面的操作。
    • 因为使用[]运算符的string对象有可能是用const修饰的,此时,const修饰的对象是不能被修改的,而不用const修饰的[]运算符返回的是可以被修改的引用,所以const修饰的对象无法使用[]运算符。因此,需要重载一个用const修饰和返回类型也是被const修饰的[]运算符。

    十、insert函数

    1、代码

    string& insert(size_t pos, char c)
    {
        assert(pos <= _size);
        if (_size == _capacity)
            reserve(_capacity == 0 ? 4 : _capacity * 2);
        for (size_t end = _size + 1; end > pos; --end)
            _str[end] = _str[end - 1];
        _str[pos] = c;
        ++_size;
        return *this;
    }
    string& insert(size_t pos, const char* str)
    {
        assert(pos <= _size);
        size_t len = strlen(str);
        if (_size + len > _capacity)
            reserve(_size + len);
    
        for (size_t end = _size + len; end >= pos + len; --end)
            _str[end] = _str[end - len];
    
        strncpy(_str + pos, str, len);
        _size += len;
        return *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2、实现原理

    • insert函数的作用是在指定位置,即pos位置上插入字符或者字符串,并返回string对象的引用。所以,上方代码中第一个insert函数是插入字符,而第二个insert函数是插入字符串。
    • 首先判断插入位置pos是否小于或等于_size,如果不是,就是越界了,直接报错而不进行其他操作。
    • 如果_size == _capacity时,说明需要扩容了,扩容后再将元素往后移,最后将pos位置处的元素赋值为插入元素的值c。
    • 插入字符串的操作与插入字符的操作类似。

    3、注意

    • 在插入字符的insert函数中,需要对_capacity进行判断,因为进行插入操作的string对象有可能是空的。
    • 在使用reserve函数时,传入的参数不能是_capacity * 2,因为如果string对象是空的,那它的_capacity是0,所以,传入的参数将永远是0。
    • 在进行移动元素的循环语句中,需要用_str[end] = _str[end - 1];进行操作,当end与pos相等时退出循环。因为如果用_str[end + 1] = _str[end];进行操作,判断条件为end >= pos。当pos为0,而end等于0时会进入循环,再对end减一,因为end是size_t 类型的,那减一后end将会变成整型的最大值,这样循环就会变成死循环,而[]的访问也会越界,即程序将会出错。
    • 在插入字符串的insert函数的移动元素循环语句中,注意点与上方的一致。

    十一、erase函数

    1、代码

    string& erase(size_t pos, size_t len = npos)
    {
        assert(pos < _size);
        if (len == npos || pos + len >= _size)
        {
            _str[pos] = '\0';
            _size = pos;
        }
        else
        {
            strcpy(_str + pos, _str + pos + len);
            _size -= len;
        }
        return *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、实现原理

    • 因为是在指定位置删除元素,所以删除的位置必须在对象的字符串里面,即pos需小于_size,因为_size处的元素值为’\0’,所以pos不能等于_size。
    • 当len == npos || pos + len >= _size时,说明要将指定位置后面的所有元素都删除,所以把指定位置元素的值改为\0即可,但要记得对_size进行修改。
    • 反之,直接用strcpy进行拷贝即可,无需用循环一一拷贝,因为那样会比较麻烦。

    十二、push_back函数

    1、代码

    void push_back(char c)
    {
        //if (_size == _capacity)
        //{
        //    /*char* tmp = new char[_capacity * 2 + 1];
        //    _capacity = _capacity * 2;
        //    strcpy(tmp, _str);
        //    delete[] _str;
        //    _str = tmp;*/
    
        //    //此处可以复用reserve
        //    reserve(_capacity == 0 ? 4 : _capacity * 2);
        //}
        //_str[_size] = c;
        //_str[++_size] = '\0';
    
        //此处可以复用insert
        insert(_size, c);
    }
    string& operator+=(char c)
    {
        push_back(c);
        return *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2、实现原理

    • 由于实现了insert函数,所以,push_back可以复用insert函数,插入位置为最后一个元素的下一个位置,即_size位置处。
    • push_back函数中注释掉的那部分是不复用insert函数的代码。
    • 由于实现了push_back函数,则插入字符的+=运算符可以复用push_back函数。

    十三、append函数

    1、代码

    void append(const char* str)
    {
        //size_t len = strlen(str);
        //if (_size + len > _capacity)
        //{
        //    /*char* tmp = new char[_size + len + 1];
        //    strcpy(tmp, _str);
        //    _str = tmp;
        //    _capacity = _size + len;*/
    
        //    //此处可以复用reserve
        //    reserve(_size + len);
        //}
        //strcpy(_str + _size, str);
        //_size = _size + len;
    
        //此处可以复用insert
        insert(_size, str);
    }
    string& operator+=(const char* str)
    {
        append(str);
        return *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2、实现原理

    • 由于实现了insert函数,所以,append可以复用insert函数,插入位置为最后一个元素的下一个位置,即_size位置处。
    • append函数中注释掉的那部分是不复用insert函数的代码。
    • 由于实现了append函数,则插入字符串的+=运算符可以复用append函数。

    十四、关系运算符

    1、代码

    bool operator>(const string& s)
    {
        return strcmp(_str, s._str) > 0;
    }
    bool operator==(const string& s)
    {
        return strcmp(_str, s._str) == 0;
    }
    bool operator>=(const string& s)
    {
        return *this > s || *this == s;
    }
    bool operator<=(const string& s)
    {
        return !(*this > s);
    }
    bool operator!=(const string& s)
    {
        return !(*this == s);
    }
    bool operator<(const string& s)
    {
        return !(*this >= s);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2、实现原理

    • 关系运算符之间是可以复用的,即只需重载一个大于或小于运算符和一个等于运算符即可,其他的直接复用这两个运算符即可。
    • 字符串之间的比较可以使用strcmp函数,当它的返回结果是0,说明两个字符串相等,返回结果是1,说明第一个字符串比第二个字符串大,返回结果是-1,说明第一个字符串比第二个字符串小。
    • 其它关系运算符可以复用定义的那两个关系运算符,因为实现的是string对象的比较,所以可以直接用string对象进行比较,而不用调用_str去比较。

    十五、find函数

    1、代码

    size_t find(char c, size_t pos = 0) const
    {
        assert(pos < _size);
    
        for (size_t i = pos; i < _size; ++i)
        {
            if (_str[i] == c)
                return i;
        }
        return npos;
    }
    
    size_t find(const char* sub, size_t pos = 0) const
    {
        assert(sub);
        assert(pos < _size);
    
        const char* ptr = strstr(_str + pos, sub);
        if (ptr == nullptr)
            return npos;
        else
            return ptr - _str;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2、实现原理

    • 第一个find函数为在string对象中查找字符c,第二个find函数为查找字符串sub,pos为开始查找的位置,如果找到,返回c/sub在string中第一次出现的位置。
    • 使用assert进行断言,当pos非法时,直接报错而不进行下面的操作;当sub为空时,同样直接报错不再进行下面的操作,因为无意义。
    • 当满足条件时,进行查找,第一个find函数一个一个地遍历,如果指定位置(i)的元素与c相等,说明找到了,直接返回i,即下标。否则返回npos,说明没找到;第二个find函数使用strstr进行查找,如果找到,会返回找到的那个字符串头部的地址,否则,返回空指针。对接收变量ptr进行判断,如果为空,返回npos,否则,返回下标,即ptr与起始指针_str相减的结果。

    3、注意

    • 第一个find函数的查找循环语句中,因为是从pos位置开始查找,所以i的初始值是pos而不是0。
    • 第二个find函数中,对strstr传参时,_str需要加上pos,相当于从下标为pos的位置开始查找。

    十六、流输入与流提取运算符重载

    1、代码

    friend ostream& operator<<(ostream& _cout, const snow::string& s);
    friend istream& operator>>(istream& _cin, snow::string& s);
    
    • 1
    • 2
    ostream& operator<<(ostream& _cout, const snow::string& s)
    {
        for (size_t i = 0; i < s._size; ++i)
        {
            _cout << s[i];
        }
        return _cout;
    }
    istream& operator>>(istream& _cin, snow::string& s)
    {
        s.clear();
        char ch = _cin.get();
    
        const size_t N = 32;
        char buff[N];
        size_t i = 0;
    
        while (ch != ' ' && ch != '\n')
        {
            buff[i++] = ch;
            if (i == N - 1)
            {
                buff[i] = '\0';
                s += buff;
                i = 0;
            }
    
            ch = _cin.get();
        }
    
        buff[i] = '\0';
        s += buff;
    
        return _cin;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    2、实现原理

    • 第一份代码写在string类中,是两个重载运算符的友元声明,第二份代码写在string类外,是两个重载运算符的定义。
    • 因为使用上述两个运算符时,是cout << string对象和cin >> string对象,即string对象是在第二个形参的位置处。而如果在string类中直接定义而不写成友元函数,函数的第一个位置是隐藏的this指针,即string对象,第二个形参才是流,这与我们想要的不符,所以只能写成友元函数,自己决定形参的位置。
    • 如果在上述两个重载运算符实现函数的函数体内没有使用到string对象private限定符限定的成员,可以不将上述两个重载运算符实现函数声明为友元函数。
    • 流输入函数可以使用循环将string对象的元素一一输出,最后,为了可以接着使用,需要返回流的引用,即返回_cout。
    • 流提取函数则是先调用clear函数把string对象的内容清理掉,接着用get一个一个地提取并赋值给变量ch,方便下面输入到string对象s中,然后定义一个数组buff,进行分次提取,这样不用每次只提取一个字符就直接输入到s中,提高了输入的效率。当提取到的字符为空格或换行符时,说明提取操作结束了,则退出循环。

    3、注意

    • 上述两个函数的定义需放在命名空间内定义,否则就和C++库里面的冲突了。
    • 在流提取函数中,提取字符时得用get,而不能用cin >> ch。因为cin >> ch默认是从缓冲区里提取内容的,它会觉得空格和换行是输入内容之间的间隔而忽略掉空格与换行,这将导致下面的循环变成死循环而使程序出错。
    • buff数组中需要将最后一个元素赋值为\0,因为插入字符串的函数是用strlen计算插入字符串的大小的,而strlen函数是以\0作为字符串的结束标志。
    • 当退出循环后,需要把buff的内容输入到s中,否则可能最后有一些内容的数量不足N而没有输入到s中。

    十七、模拟实现string与测试的代码

    #pragma once
    #define  _CRT_SECURE_NO_WARNINGS
    #include
    #include
    using namespace std;
    
    namespace snow
    {
        class string
        {
            friend ostream& operator<<(ostream& _cout, const snow::string& s);
            friend istream& operator>>(istream& _cin, snow::string& s);
        public:
            typedef char* iterator;
            string(const char* str = "")
            {
                _size = strlen(str);
                _capacity = _size;
    
                _str = new char[_capacity + 1];
                strcpy(_str,str);
            }
    
            string(const string& s)
                :_str(nullptr)
                ,_size(0)
                ,_capacity(0)
            {
                string tmp(s._str);
                swap(tmp);
            }
    
            string& operator=(const string& s)
            {
                if (this != &s)
                {
                    string tmp(s);
                    swap(tmp);
                }
                return *this;
            }
            ~string()
            {
                delete[]_str;
                _str = nullptr;
                _size = _capacity = 0;
            }
    
            iterator begin()
            {
                return _str;
            }
            iterator end()
            {
                return _str + _size;
            }
    
            void push_back(char c)
            {
                //if (_size == _capacity)
                //{
                //    /*char* tmp = new char[_capacity * 2 + 1];
                //    _capacity = _capacity * 2;
                //    strcpy(tmp, _str);
                //    delete[] _str;
                //    _str = tmp;*/
    
                //    //此处可以复用reserve
                //    reserve(_capacity == 0 ? 4 : _capacity * 2);
                //}
                //_str[_size] = c;
                //_str[++_size] = '\0';
    
                //此处可以复用insert
                insert(_size, c);
            }
            string& operator+=(char c)
            {
                push_back(c);
                return *this;
            }
            void append(const char* str)
            {
                //size_t len = strlen(str);
                //if (_size + len > _capacity)
                //{
                //    /*char* tmp = new char[_size + len + 1];
                //    strcpy(tmp, _str);
                //    _str = tmp;
                //    _capacity = _size + len;*/
    
                //    //此处可以复用reserve
                //    reserve(_size + len);
                //}
                //strcpy(_str + _size, str);
                //_size = _size + len;
    
                //此处可以复用insert
                insert(_size, str);
            }
            string& operator+=(const char* str)
            {
                append(str);
                return *this;
            }
            void clear()
            {
                _size = 0;
                _str[_size] = '\0';
            }
            void swap(string& s)
            {
                ::swap(_str, s._str);
                ::swap(_size, s._size);
                ::swap(_capacity, s._capacity);
            }
            const char* c_str()const
            {
                return _str;
            }
    
            size_t size()const
            {
                return _size;
            }
            size_t capacity()const
            {
                return _capacity;
            }
            bool empty()const
            {
                return _size == 0;
            }
            void resize(size_t n, char c = '\0')
            {
                if (n <= _size)
                {
                    _size = n;
                }
                else
                {
                    if (n > _capacity)
                    {
                        char* tmp = new char[n + 1];
                        _capacity = n;
                        strcpy(tmp, _str);
                        delete[] _str;
                        _str = tmp;
                    }
                    for (_size; _size < n; ++_size)
                    {
                        _str[_size] = c;
                    }
                }
                _str[_size] = '\0';
            }
            void reserve(size_t n)
            {
                if (_capacity < n)
                {
                    char* tmp = new char[n + 1];
                    strcpy(tmp, _str);
                    delete[] _str;
                    _str = tmp;
                    _capacity = n;
                }
            }
    
            char& operator[](size_t index)
            {
                assert(index < _size);
                return _str[index];
            }
            const char& operator[](size_t index)const
            {
                assert(index < _size);
                return _str[index];
            }
    
            bool operator>(const string& s)
            {
                return strcmp(_str, s._str) > 0;
            }
            bool operator==(const string& s)
            {
                return strcmp(_str, s._str) == 0;
            }
            bool operator>=(const string& s)
            {
                return *this > s || *this == s;
            }
            bool operator<=(const string& s)
            {
                return !(*this > s);
            }
            bool operator!=(const string& s)
            {
                return !(*this == s);
            }
            bool operator<(const string& s)
            {
                return !(*this >= s);
            }
    
            size_t find(char c, size_t pos = 0) const
            {
                assert(pos < _size);
                for (size_t i = pos; i < _size; ++i)
                {
                    if (_str[i] == c)
                        return i;
                }
                return npos;
            }
    
            size_t find(const char* sub, size_t pos = 0) const
            {
                assert(sub);
                assert(pos < _size);
                const char* ptr = strstr(_str + pos, sub);
                if (ptr == nullptr)
                    return npos;
                else
                    return ptr - _str;
            }
    
            string& insert(size_t pos, char c)
            {
                assert(pos <= _size);
    
                if (_size == _capacity)
                    reserve(_capacity == 0 ? 4 : _capacity * 2);
                    //reserve(_capacity * 2);
                for (size_t end = _size + 1; end > pos; --end)
                    _str[end] = _str[end - 1];
                _str[pos] = c;
                ++_size;
                return *this;
            }
            string& insert(size_t pos, const char* str)
            {
                assert(pos <= _size);
                size_t len = strlen(str);
                if (_size + len > _capacity)
                    reserve(_size + len);
    
                for (size_t end = _size + len; end >= pos + len; --end)
                    _str[end] = _str[end - len];
    
                strncpy(_str + pos, str, len);
                _size += len;
                return *this;
            }
    
            string& erase(size_t pos, size_t len = npos)
            {
                assert(pos < _size);
                if (len == npos || pos + len >= _size)
                {
                    _str[pos] = '\0';
                    _size = pos;
                }
                else
                {
                    strcpy(_str + pos, _str + pos + len);
                    _size -= len;
                }
                return *this;
            }
    
            static const size_t npos = -1;
        private:
            char* _str;
            size_t _capacity;
            size_t _size;
        };
        ostream& operator<<(ostream& _cout, const snow::string& s)
        {
            for (size_t i = 0; i < s._size; ++i)
            {
                _cout << s[i];
            }
            return _cout;
        }
        istream& operator>>(istream& _cin, snow::string& s)
        {
            s.clear();
            char ch = _cin.get();
    
            const size_t N = 32;
            char buff[N];
            size_t i = 0;
            while (ch != ' ' && ch != '\n')
            {
                buff[i++] = ch;
                if (i == N - 1)
                {
                    buff[i] = '\0';
                    s += buff;
                    i = 0;
                }
    
                ch = _cin.get();
            }
            buff[i] = '\0';
            s += buff;
    
            return _cin;
        }
    
        //测试函数需写于命名空间内才能调试进来
        void Test_string1()
        {
            string s1;
            string s2("hello");
            cout << s1 << endl;
            cout << s2 << endl;
            
    
            string s3(s2);
            cout << s3 << endl;
    
            s1 = s3;
            cout << s1 << endl;
    
            s2 = "snow";
            for (auto it = s2.begin(); it < s2.end(); ++it)
            {
                cout << *it;
            }
        }
    
        void Test_string2()
        {
            string s1("snow");
            cout << s1 << endl;
            cout << s1.size() << endl;
            cout << s1.capacity() << endl;
    
            /*s1.push_back('!');
            cout << s1 << endl;*/
    
            s1 += '!';
            cout << s1 << endl;
            cout << s1.size() << endl;
            cout << s1.capacity() << endl;
    
            /*s1.append("dragon!");
            cout << s1 << endl;*/
    
            s1 += "dragon!";
            cout << s1 << endl;
    
            s1.clear();
            cout << s1 << endl;
    
        }
        void Test_string3()
        {
            string s1("snow");
            s1 += '\0';
            s1 += "dragon";
            cout << s1.c_str() << endl;
            cout << s1 << endl;
            cout << s1.empty() << endl;
    
    
            std::string s2;
            cout << s2.empty() << endl;
    
            string s3;
            cout << s3.empty() << endl;
        }
    
        void Test_string4()
        {
            string s1("snow");
            cout << s1 << endl;
            s1.resize(10, '!');
            cout << s1 << endl;
            s1.resize(3);
            cout << s1 << endl;
    
            string s2("dragon");
            cout << s2.capacity() << endl;
            s2.reserve(18);
            cout << s2.capacity() << endl;
        }
    
        void Test_string5()
        {
            string s1("snowdragon");
            //std::string s1("snowdragon");
    
            cout << s1.find('a', 0) << endl;
            cout << s1.find('a') << endl;
            cout << s1.find('a', 20) << endl;
            cout << s1.find('l', 0) << endl;
    
            cout << s1.find("dra", 0) << endl;
            cout << s1.find("dra") << endl;
            cout << s1.find("dra", 20) << endl;
            cout << s1.find("sd", 0) << endl;
        }
        void Test_string6()
        {
            string s1("snowdragon");
    
            cout << s1 << endl;
            s1.insert(4, 'h');
            cout << s1 << endl;
            s1.insert(0, 'a');
            cout << s1 << endl;
            s1.insert(12, 'z');
            cout << s1 << endl;
    
            cout << "**************************" << endl;
    
            string s2("snow");
            //std::string s2("snow");
            cout << s2 << endl;
            s2.insert(4, "dragon");
            cout << s2 << endl;
            s2.insert(0, "hello");
            cout << s2 << endl;
            s2.insert(5, ",i am ");
            cout << s2 << endl;
            /*s2.insert(30, "hello");
            cout << s2 << endl;*/
    
            cout << "**************************" << endl;
    
            s2.erase(10, 11);
            cout << s2 << endl;
            s2.erase(0, 6);
            cout << s2 << endl;
            s2.erase(2, 1);
            cout << s2 << endl;
        }
    
        void Test_string7()
        {
            string s1, s2;
            //std::string s1, s2;
            cin >> s1 >> s2;
            cout << s1 << "   " << s2 << endl;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 本文只是对string的一些常用与常见的函数模拟实现与讲解,一些比较简单的函数没有讲解但在上方的代码中都有模拟实现的代码。

    本文到这里就结束了,如有错误或者不清楚的地方欢迎评论或者私信
    创作不易,如果觉得博主写得不错,请务必点赞、收藏加关注💕💕💕

  • 相关阅读:
    【图解计算机网络】网络协议分层解析
    11.4 校招 实习 内推 面经
    太空射击第10课: Score (繪畫和文字)
    JavaEE——Thread类
    R语言使用data.table包的fread函数读取(加载)csv数据为data.table格式、使用anyNA函数判断data.table中是否存在缺失值
    【LeetCode 力扣】3.无重复字符的最长子串 Java实现 滑动窗口
    设置小数点后2位,随机保存财富txt,生成随机富翁数
    解决WPF+Avalonia在openKylin系统下默认字体问题
    【flink报错】flink cdc无主键时的操作
    2023物联网新动向:WEB组态除了用于数据展示,也支持搭建业务逻辑,提供与蓝图连线和NodeRed规则链类似的可视化编程能力
  • 原文地址:https://blog.csdn.net/Snow_Dragon_L/article/details/133920068