9.23每日作业

仿照string类,自己手动实现 My_string

list.h

#ifndef LIST_H
#define LIST_H
#include <iostream>

using namespace std;

class My_string
{
private:
    char *ptr;
    int size;
    int len;

public:
    //无参构造
    My_string();
    //有参构造
    My_string(const char * src);
    My_string(int num, char value);
    //拷贝构造
    My_string(const My_string& other);
    //拷贝赋值
    My_string & operator= (const My_string &other);
    //析构构造
    ~My_string();
    //判空
    bool MYempty() const;
    //尾插
    void push_back(char value);
    //尾删
    void pop_back();
    //at函数实现
    char &at(int index);
    //清空函数
    void clear();
    //返回C风格字符串
    char *data();
    //返回实际长度
    int get_length();
    //返回当前最大容量
    int get_size();
    //君子函数:二倍扩容
    void resize();
    void show();
};
#endif // LIST_H

list.c

#include "list.h"
using namespace std;
My_string::My_string():size(15)
{
    this->ptr = new char[15];
    this->ptr[0] = '\0';
    this->len = 0;
}
//有参构造
My_string::My_string(const char * src)
{
    this->len = 0;
    for(int i=0;src[i] != 0;i++)
    {
        this->len++;
    }
    this->size = this->len+1;
    this->ptr = new char[this->size];
    for(int i = 0;src[i] != 0;i++)
    {
        this->ptr[i] = src[i];
    }
}
My_string::My_string(int num, char value)
{
    this->len = num;
    this->size = this->len+1;
    this->ptr = new char[this->size];
    for (int i = 0; i < this->len; i++)
    {
        this->ptr[i] = value; // 用value填充数组
    }
    this->ptr[this->len] = '\0'; // 添加结束符
}
//拷贝构造
My_string::My_string(const My_string& other):ptr(new char[other.len + 1]),size(other.size), len(other.len)
{
    for(int i = 0;other.ptr[i] != 0;i++) // 复制字符串内容
    {
        this->ptr[i] = other.ptr[i];
    }
}
//拷贝赋值
My_string & My_string::operator= (const My_string &other)
{
    if(this != &other)               //防止自己给自己赋值
    {
        this->size = other.size;
        this->len = other.len;
        //this->ptr = other.ptr;                 //浅拷贝
        for(int i = 0;other.ptr[i] != 0;i++)     //深拷贝
        {
            this->ptr[i] = other.ptr[i];
        }
    }
    return *this;                  //返回值自身的引用
}
//析构构造
My_string::~My_string()
{
    delete[] ptr;     //需要显性定义析构函数,在析构函数的函数体内释放堆区空间
    cout<<"Person::析构函数,this = "<<this<<endl;
}
//判空
bool My_string::MYempty() const
{
    return this->ptr == nullptr || this->len == 0; // 如果指针为空或长度为0,则认为是空
}
//尾插
void My_string::push_back(char value)
{
    this->size++;
    this->ptr[len] = value;
    this->len++;
    this->ptr[size] = 0;
}
//尾删
void My_string::pop_back()
{
    this->ptr[len-1] = 0;
    this->size--;
    this->len--;
}
//at函数实现
char & My_string::at(int index)
{
    static char num;
    num = this->ptr[index-1];
    return num;  
}
//清空函数
void My_string::clear()
{
    this->size = 15;
    delete[] ptr;
    this->ptr = new char[1];
    this->ptr[0] = '\0';
    this->len = 0;
}
//返回C风格字符串
char *My_string::data()
{
    return ptr;
}
//返回实际长度
int My_string::get_length()
{
    return this->len;
}
//返回当前最大容量
int My_string::get_size()
{
    return this->size;
}
//君子函数:二倍扩容
void My_string::resize()
{
    size *= 2; // 容量翻倍
    char* newptr = new char[size]; // 分配新的内存
    for(int i = 0;this->ptr[i] != 0;i++)     //深拷贝
    {
        newptr[i] = this->ptr[i];
    }
    delete[] ptr; // 释放旧内存
    this->ptr = newptr; // 更新指针
}
void My_string::show()
{
    cout<< "ptr = " << this->ptr << endl;
    cout<< "size = " << this->size << endl;
    cout<< "len = " << this->len << endl;
}

main.c

#include "list.h"

using namespace std;

int main()
{
    My_string s;
    s.show();
    if(s.MYempty()==1)
    {
        cout << "s" << "为空" << endl;
    }
    My_string s1("hello world");
    s1.show();

    My_string s2(5,'A');
    s2.show();

    My_string s3(s1);
    s3.show();
    s.operator=(s2);
    s.show();

    My_string s4 = s1;
    s4.push_back('B');
    s4.show();

    s4.pop_back();
    s4.show();

    cout << s4.at(3) << endl;

    s4.clear();
    if(s4.MYempty()==1)
    {
        cout << "s4" << "为空" << endl;
    }

    s3.data();
    s3.show();

    cout << s2.get_length() << endl;
    cout << s2.get_size() << endl;

    s1.resize();
    s1.show();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值