当前位置: 首页 > article >正文

9.23 My_string.cpp

在这里插入图片描述

my_string.h

#ifndef MY_STRING_H
#define MY_STRING_H


#include <iostream>
#include <cstring>


using namespace std;


class My_string
{
private:
    char *ptr;         //指向字符数组的指针
    int size;           //字符串的最大容量
    int len;            //字符串当前容量


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

    //有参构造
    My_string(const char* src){
        cout<<"****************一个参数有参构造***********"<<endl;
        this->len = strlen(src);
        this->size = len + 1;
        this->ptr = new char[size];
        strcpy(this->ptr,src);
    }

    My_string(int num,char value){
        cout<<"****************两个参数有参构造***********"<<endl;
        this->len = num;
        this->size = len + 1;
        this->ptr = new char[len+1];
        int i=0;
        while(i<num){
            this->ptr[i]=value;
            i++;
        }
        this->ptr[num]='\0';
    }

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

    //拷贝赋值
    My_string &operator=(const My_string &other){
        cout<<"****************拷贝赋值***********"<<endl;
        if (this == &other) {
                return *this;  // 直接返回当前对象
        }

        delete[] ptr;
        this->len = other.len;
        this->size = other.size;
        this->ptr = new char[size];
        strcpy(ptr, other.ptr);
        return *this;
    }

    //析构函数
    ~My_string(){
        cout<<"****************析构函数***********"<<endl;
        delete []this->ptr;
    }

    //显示内容
    void show();
    //判空
    void isempty();
    //判满
    void isfull();
    //尾插
    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();

};


#endif // MY_STRING_H

my_string.cpp

#include "my_string.h"
#include <cstring>
void My_string::show(){
    cout<<ptr<<endl;
}
//判空
void My_string::isempty(){
    if(this->len==0){
        cout<<"字符串为空"<<endl;
    }
    return;
}
//判满
void My_string::isfull(){
    if(this->len>=this->size){
      cout<<"字符串满"<<endl;
      resize();
      cout<<"重新分配空间"<<endl;
    }else{
        cout<<"字符串未满"<<endl;
    }
    return;
}

//尾插
void My_string::push_back(char value){
    this->isfull();
    this->ptr[len]=value;
    len++;
    ptr[len]='\0';
}

//尾删
void My_string::pop_back(){
    this->len=this->len-1;
    ptr[len]='\0';
}
//at函数实现
char & My_string::at(int index){
    return ptr[index];
}
//清空函数
void My_string::clear(){
    ptr[0]='\0';
    this->len=0;
}

//返回C风格字符串
char* My_string::data(){
    return this->ptr;  // 返回指向字符串的指针
}
//返回实际长度
int My_string::get_length(){
    return this->len;
}
//返回当前最大容量
int My_string::get_size(){
    return this->size;
}


//君子函数:二倍扩容
void My_string::resize() {
        size *= 2;
        char* new_ptr = new char[size];
        strcpy(new_ptr, ptr);
        delete[] ptr;
        ptr = new_ptr;
    }

main.cpp

#include "my_string.h"

int main(){

    My_string p1;

    My_string p2("hello");

    p2.show();
    My_string p3(5,'A');

    p3.show();
    My_string p4 = p2;
    p4.show();

    p3=p2;
    p3.show();

    p2.show();
    cout<<"p2的第3个字符"<<p2.at(3)<<endl;

    cout<<"p2C风格的字符串"<<p2.data()<<endl;

    p2.isfull();

    cout<<"p2的最大长度:"<<p2.get_size()<<endl;
    cout<<"p2现在的长度:"<<p2.get_length()<<endl;

    p2.clear();
    p2.show();

    p2.push_back('W');
    p2.show();

    p2.pop_back();
    p2.show();

}



http://www.kler.cn/a/322232.html

相关文章:

  • 浅谈计算机视觉的学习路径1
  • C++ : 多态
  • uni-app App版本更新
  • 【永磁同步电机(PMSM)】 6. 矢量空间算法(SVPWM)
  • fuzzer实战-magma-模糊测试
  • 【计算机科学导论】
  • 安卓手机视频被误删怎么恢复,这3个方法满足你
  • 智能电话机器人的优势有哪些突出的?电销系统搭建部署
  • 西圣、漫步者头戴式耳机哪个音质好?热门主流头戴式耳机专业评测
  • MQTT.fx 1.7.1使用说明篇(OneNET-MQTT-API调试)
  • 走进上海郭培高定会馆:以冠珠华脉、华珍筑就至臻至性的艺术空间
  • 软件测试之单元测试/系统测试/集成测试详解
  • 深度学习经典模型之BERT(下)
  • 展锐平台的手机camera 系统isptool 架构
  • 剪映草稿批量自动化导出教程实操演示
  • 广州C++信奥老师解一本通题 1919:【02NOIP普及组】选数
  • 有关在.Net Core中以TEXT类型将Json格式字段存到数据库的学习
  • 基于ollama的本地RAG实践
  • 什么是网络安全自动化以及优势与挑战
  • 深入探索《AI大模型开发之路》:我的读书心得