C++:String的写时拷贝

本文详细介绍了使用C++实现字符串类的过程,包括构造函数、赋值运算符、写时拷贝机制等关键部分,深入探讨了字符串操作如插入、追加、删除等功能的实现细节。

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

String的写时拷贝
//test.h
#pragma once

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

#define TESTHEADER printf("\n================%s=============\n", __FUNCTION__)
class String
{
public:
    String(const char* str);
    String(const String& s);
    String& operator = (const String s);
    ~String();
    char* c_str();
    void CopyOnWrite();
    char& operator [] (size_t pos);
    const char& operator [] (size_t pos)const;
    String& Insert(size_t pos, char ch);
    String& Insert(size_t pos, const char* str);
    String& Append(const char* str);
    String& Erase(size_t pos, size_t n);
    void Expand(size_t n);
private:
    char* _str;
    int* _pcount;
    size_t _size;
    size_t _capacity;
};

//test.cc
#include "test.h"
String::String(const char* str)
    :_size(strlen(str))
,_capacity(_size)
{
    _str = new char [strlen(str) + 1];
    _pcount = new int(1);
    strcpy(_str, str);
}


//s(s1)
String::String(const String& s)
{
    _str = s._str;
    _pcount = s._pcount;
    ++(*_pcount);
    _size = s._size;
    _capacity = s._capacity;
}

//s1 = s2
String& String:: operator = (const String s) 
{
    if(_str != s._str)
    {
        _str = s._str;
        --(*_pcount);
        if(*_pcount == 0)
        {
            delete [] _str;
            delete _pcount;
        }
        _size = s._size;
        _capacity = s._capacity;
    }
    return *this;
}

String::~String()
{
    delete [] _str;
    delete _pcount;
    _size = 0;
    _capacity = 0;
}
char* String::c_str()
{
    return _str;
}

void String::CopyOnWrite()
{
    if(*_pcount > 1)
    {
        char* tmp = new char[_size + 1];
        strcpy(tmp, _str);
        --(*_pcount);
         _str = tmp;
         _pcount = new int(1);
    }
}

//s[1]
char& String::operator [] (size_t pos)
{
    CopyOnWrite();
    return _str[pos];
}

const char& String:: operator [] (size_t pos)const
{
    return _str[pos];
}

void String::Expand(size_t n)
{
    if( *_pcount  == 1)
    {
        char* tmp = new char [n];
        strcpy(tmp, _str);
        delete [] _str;
        --(*_pcount);
        delete _pcount;
        _pcount = new int(1);
        _str = tmp;
    }
    else
    {
        char* tmp = new char [n];
        strcpy(tmp, _str);
        _str = tmp;
        --(*_pcount);
        _pcount = new int(1);
    }
}

String& String::Insert(size_t pos, char ch)
{
    assert(pos < _size);
    if(_size == _capacity)
    {
        Expand(2 * _capacity);
    }
    int end = _size;
    for(; end >= (int)pos; end--)
    {
        _str[end] = _str[end - 1];
    }
    _str[pos] = ch;
    return *this;
}

String& String::Insert(size_t pos, const char* str)
{
    assert(pos <= _size);
    int len = strlen(str);
    if(_size == _capacity)
    {
        Expand(_size + len);
    }
    int end = _size;
    for(; end >= (int)pos; end--)
    {
        _str[end + len] = _str[end];
    }
    strncpy(_str + pos, str, len);
    _size += len;
    /* _str[_size] = '\0'; */
    return *this;
}


String& String::Append(const char* str)
{
    int len_ = strlen(_str);
    int len = strlen(str);
    if(_size == _capacity)
    {
        Expand(_size + len);
    }
    strcpy(_str + len_, str);
    _size += len;
    return *this;
}


String& String::Erase(size_t pos, size_t n)
{
    assert(pos <= _size);
    CopyOnWrite();
    if(pos + _size > _capacity)
    {
        _str[pos] = '\0';
    }
    strcpy(_str + pos, _str + pos + n);
    _size -= n;
    return *this;
}

如果有什么错误,还望大家提出来,共同进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值