C++—String类接口的实现

本文介绍了一个自定义的C++ String类实现,包括构造函数、拷贝构造、赋值操作符重载等,并实现了字符串的插入、追加、扩展等功能。通过深拷贝确保了内存安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对c++string库部分接口的实现,通过接口间的复用,使个个接口紧密联系,更加简洁。
重要接口Expand()通过检测当前对象容量是否满足要求,对对象进行扩容等操作。
此次实现string全部使用的是深拷贝。

#include<iostream>
#include<string.h>
#include<assert.h>
#include<string>
using namespace std;

class String
{
public:
    String(const char* str = "")
        :_size(strlen(str))
    {
        _capacity = _size;
        _str = new char[_capacity + 1];
        strcpy(_str, str);
    }

    String(const String& s)
        :_size(s._size)
        , _capacity(s._capacity)
        , _str(new char[s._capacity + 1])
    {
        strcpy(_str, s._str);
    }

    //string(const String& s)
    //:_size(0)
    //,_capacity(0)
    //,_str(NULL)
    //{
    //      String tmp(s._str);
    //      Swap(tmp);
    //}

    String& operator=(String s)
    {
        swap(_capacity, s._capacity);
        swap(_size, s._size);
        swap(_str, s._str);
        //Swap(s);
        return *this;
    }

    ~String()
    {
        if (NULL != _str)
        {
            delete[] _str;
            _str = NULL;
        }
    }

    void Insert(size_t pos, char s)
    {
        assert(pos <= _size);
        if (_size == _capacity)
        {
            Expand(_capacity * 2);
        }
        size_t end = _size;

        while (end >= pos)
        {
            _str[end + 1] = _str[end];
            end--;
        }
        _str[pos] = s;
        _size++;
    }

    void Insert(size_t pos, const char* str)
    {
        assert(pos <= _size);
        size_t len = strlen(str);
        Expand(_size + len);
        size_t end = _size;
        while (end >= pos)
        {
            _str[end + len] = _str[end];
            end--;
        }
        size_t index = 0;
        while (index<len)
        {
            _str[pos + index] = str[index];
            index++;
            pos++;
        }
        _size += len;
    }

    void Append(const char* str)
    {
        Insert(_size, str);
    }

    char* C_str()
    {
        return _str;
    }

    void Swap(String& s)
    {
        swap(_size, s._size);
        swap(_capacity, s._capacity);
        swap(_str, s._str);
    }

    size_t Size()const
    {
        return _size;
    }

    size_t Length()const
    {
        return _size;
    }

    void Expand(size_t n)
    {
        if (n>_capacity)
        {
            char* tmp = new char[n + 1];
            strcpy(tmp, _str);
            delete[] _str;
            _str = tmp;
            _capacity = n;
        }
    }

    char& operator[](size_t pos)
    {
        assert(pos<_size);
        return _str[pos];
    }

    String& operator+=(const String& s)
    {
        Append(s._str);
        return *this;
    }

    String operator+(const String& s)
    {
        String tmp(_str);
        tmp.Append(s._str);
        return tmp;
    }

    void Erase(size_t pos, size_t n)
    {
        assert(pos<_size);
        if (pos + n >= _size)
        {
            _str[pos] = '\0';
            _size = pos;
        }
        else
        {
            size_t index = pos + n;
            while (index <= _size)
            {
                _str[pos] = _str[index];
                pos++;
                index++;
            }
            _size -= n;
        }
    }

    void PushBack(char s)
    {
        Insert(_size, s);
    }

    void PopBack()
    {
        assert(_size>0);
        Erase(_size - 1, 1);
    }

    bool operator>(const String& s)const
    {
        char* str1 = _str;
        char* str2 = s._str;
        while (*str1&&*str2)
        {
            if (*str1>*str2)
            {
                return true;
            }
            else if (*str1<*str2)
            {
                return false;
            }
            else
            {
                str1++;
                str2++;
            }
        }
        if (*str1 == '\0')
            return false;
        else
        {
            return true;
        }
    }

    bool operator>=(const String& s)const
    {
        char* str1 = _str;
        char* str2 = s._str;
        while (*str1&&*str2)
        {
            if (*str1 >= *str2)
                return true;
            else
            {
                str1++;
                str2++;
            }
        }
        return true;
    }

    bool operator<(const String& s)const
    {
        char* str1 = _str;
        char* str2 = s._str;
        while (*str1&&*str2)
        {
            if (*str1<*str2)
            {
                return true;
            }
            else if (*str1>*str2)
            {
                return false;
            }
            else
            {
                str1++;
                str2++;
            }
        }
        if (*str2 == '\0')
            return false;
        else
            return true;
    }

    bool operator<=(const String& s)const
    {
        char* str1 = _str;
        char* str2 = s._str;
        while (*str1&&*str2)
        {
            if (*str1<*str2)
                return true;
            else if (*str1>*str2)
                return false;
            else
            {
                str1++;
                str2++;
            }
        }
        return true;
    }

    void Print()const
    {
        cout << _str << endl;
    }
private:
    char* _str;
    size_t _size;
    size_t _capacity;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值