9.23作业

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

代码如下

MyString.h
cpp 复制代码
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>

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 Isvoid();
    //显示
    void show();
    //尾插
    void push_back(char value);
    //尾删
    void pop_back();
    //at函数实现
    char &at(int index);
    //清空函数
    void clear();
    //返回C风格字符串
    char *data();
    //返回实际长度
    int get_length();
    //返回当前最大容量
    int get_size();


    //君子函数:二倍扩容
    bool Add();

};
#endif // MYSTRING_H
MyString.cpp
cpp 复制代码
#include "MyString.h"

My_string::My_string() : size(15), len(0) {
    ptr = new char[size];
    ptr[0] = '\0'; // 表示串为空串
    cout << "无参构造" << endl;
}

My_string::My_string(const char* src) : size(15) {
    ptr = new char[size];
    strcpy(ptr, src); // 复制字符串
    len = strlen(src);
    cout << "一个形参的有参构造" << endl;
}

My_string::My_string(int num, char value) : size(15), len(num) {
    if (num > 15) {
        cout << "超出默认长度" << endl;
        return;
    }
    ptr = new char[size];
    for (int i = 0; i < num; i++) {
        ptr[i] = value;
    }
    ptr[num] = '\0'; // 确保字符串以'\0'结尾
    cout << "部分形参的有参构造" << endl;
}

My_string::My_string(const My_string &other) : size(other.size), len(other.len) {
    ptr = new char[size];
    strcpy(ptr, other.ptr); // 复制字符串
    cout << "拷贝构造" << endl;
}

My_string& My_string::operator= (const My_string &other) {
    if (this != &other) {
        delete[] ptr; // 释放旧内存
        size = other.size;
        len = other.len;
        ptr = new char[size];
        strcpy(ptr, other.ptr); // 复制字符串
    }
    cout << "拷贝赋值" << endl;
    return *this;
}

My_string::~My_string() {
    cout << ptr << "析构函数" << endl;
    delete[] ptr;
}

bool My_string::Isvoid() {
    return len == 0;
}

void My_string::show() {
    cout << ptr << endl;
}

void My_string::push_back(char value) {
    if (len < size - 1) {
        ptr[len++] = value;
        ptr[len] = '\0'; // 确保字符串以'\0'结尾
    } else if (Add()) {
        ptr[len++] = value;
        ptr[len] = '\0'; // 确保字符串以'\0'结尾
    }
}

void My_string::pop_back() {
    if (len > 0) {
        len--;
        ptr[len] = '\0'; // 确保字符串以'\0'结尾
    }
}

char& My_string::at(int index) {
    if (index < len) {
        return ptr[index];
    } else {
        cout << "下标越界" << endl;
        exit(EXIT_FAILURE);
    }
}

void My_string::clear() {
    len = 0;
    ptr[0] = '\0'; // 确保字符串以'\0'结尾
}

char* My_string::data() {
    return ptr;
}

int My_string::get_length() {
    return len;
}

int My_string::get_size() {
    return size;
}

bool My_string::Add() {
    if (len == size - 1) {
        char *p = new char[size * 2];
        strcpy(p, ptr);
        delete[] ptr; // 释放旧内存
        ptr = p;
        size *= 2; // 更新容量
        return true;
    }
    return false;
}
main.cpp
cpp 复制代码
#include "MyString.h"

int main() {
    My_string s;
    cout << "s:";
    s.show();

    My_string s1("hello");
    cout << "s1:";
    s1.show();

    My_string s2(5, 'A');
    cout << "s2:";
    s2.show();

    My_string s3 = s2;
    cout << "s3:";
    s3.show();

    s3 = s1;
    cout << "s3:";
    s3.show();

    if (s3.Isvoid()) {
        cout << "s3空" << endl;
    } else {
        cout << "s3非空" << endl;
    }

    cout << "尾插:";
    s3.push_back('a');
    s3.show();

    cout << "尾删:";
    s3.pop_back();
    s3.show();

    cout << "查看下标4的值:";
    if (s3.get_length() > 4) {
        cout << s3.at(4) << endl;
    } else {
        cout << "下标越界" << endl;
    }

    cout << "清空s3函数" << endl;
    s3.clear();
    cout << "s3:";
    s3.show();

    cout << "s1的C风格字符串:" << s1.data() << endl;
    cout << "s1的实际长度:" << s1.get_length() << endl;
    cout << "s1当前最大容量:" << s1.get_size() << endl;

    cout << "s3的二倍扩容" << endl;
    cout << "请输入一串字符串:";
    char c = ' ';
    while (true) {
        cin >> c;
        if (c == '#') break; // 直接在这里判断并退出循环
        s3.push_back(c);
    }
    s3.show();

    return 0;
}

运行结果

思维导图

相关推荐
saltymilk18 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥18 小时前
C++ lambda 匿名函数
c++
沐怡旸1 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥1 天前
C++ 内存管理
c++
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++