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

C++ 9.24

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

main.c

#include <iostream>
#include "text.h"
 
using namespace std;
 
 
int main()
{
 
        My_string str("hello world"); // 使用 C 字符串初始化
        str.push_back('q'); // 尾插字符
        std::cout << "输出为: " << str.data() << std::endl; // 输出:
        std::cout << "实际长度: " << str.get_length() << std::endl; // 输出  实际长度
        std::cout << "总长度: " << str.get_size() << std::endl; // 输出总长度
 
        str.pop_back(); // 尾删字符
        std::cout << "删除后的数据为: " << str.data() << std::endl; // 输出: Hello
        str.clear(); // 清空字符串
        std::cout << "清空后的内容为 " << str.data() << std::endl; // 输出: (空字符串)
 
 
        return 0;
 
 
}

test.h

 #include <iostream>
#ifndef TEXT_H
#define TEXT_H
 
 
 
 
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 is_empty()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 my_resize(int new_size);
};
 
#endif // TEXT_H

test.cpp

 #include "text.h"
#include <cstring>
 
 
//无参构造
My_string::My_string():size(15)
{
    this->ptr = new char[size];
    this->ptr[0] = '\0';            //表示串为空串
    this->len = 0;
}
 
//有参构造
//1 从字符串构造的构造函数
My_string::My_string(const char *src)
{
    len =strlen(src); //计算字符串换长度
    size=len+1;         //+1 留出终止符
    ptr=new char[size];  //分配字符数组
    strcpy(ptr,src);    //添加字符串到数组
}
//2 从字符和数量构造的构造函数
My_string::My_string(int num, char value)
{
    size=num+1;  //+1留出终止符
    len=num;       //实际长度
    ptr=new char[size];
    for(int i=0;i<len;i++)
    {
        ptr[i]=value;
    }
    ptr[len]='\0';
 
}
//拷贝构造
My_string::My_string(const My_string& other)
{
    this->size=other.size;
    this->len=other.len;
    this->ptr=new char[this->size];
    strcpy(ptr,other.ptr);
}
//拷贝赋值
My_string& My_string::operator=(const My_string & other)
{
    if(this!=&other)
    {
        delete[]ptr; //释放原有内存
        this->size=other.size;
        this->len=other.len;
        this->ptr=new char[this->size];
        strcpy(this->ptr,other.ptr);
    }
    return *this;
}
//析构函数
    My_string::~My_string()
{
    delete [] this->ptr;    //释放内存
}
    //判空
    bool My_string::is_empty()const
    {
        return  len==0;
    }
    //尾插
void My_string::push_back(char value)
{
if (len + 1 >= size)
        {
           // 扩容
          my_resize(size * 2);
       }
 
        ptr[len++]=value;  //加入字符
        ptr[len]='\0';
 }
    //尾删
void My_string::pop_back()
    {
     if(len>0)  //判断是否可以删除
     {
         len--;   //长度-1
         ptr[len]='\0';
     }
    }
 //at函数实现
char & My_string::at(int index)
{
    if(index>=0&&index<size)
    {
        return  ptr[index];
    }
    else{
        cout<<"超出范围 "<<endl;
        exit(1);   //终止程序
    }
}
 
    //清空函数
void My_string:: clear()
    {
        this->len=0; //重置长度
        ptr[0]='\0';  //置空
 
    }
 
 
    //返回C风格字符串
 char * My_string::data()
 {
     return ptr;
 }
    //返回实际长度
    int My_string::get_length()
    {
        return len;
    }
    //返回当前最大容量
    int My_string::get_size()
    {
        return  size;
    }
 
    //君子函数:二倍扩容
void My_string::my_resize(int new_size)
{
        char* new_ptr = new char[new_size]; // 动态分配新数组
        strcpy(new_ptr, ptr); // 复制旧数据
        delete[] ptr; // 释放旧内存
        ptr = new_ptr; // 更新指针
        size = new_size; // 更新大小
 }
 

Xmind

 


http://www.kler.cn/news/317499.html

相关文章:

  • 如何使用ssm实现疫苗预约系统+vue
  • 使用synchronized锁住字符串
  • Shire 智能体市场:IDE 一键安装多智能体,协同打造集体智慧 Copilot
  • 迎国庆-为祖国庆生python、Java、C各显神通
  • 【Python】数据可视化之分布图
  • 联影医疗嵌入式面试题及参考答案(3万字长文)
  • wpf,工具栏上,最小化按钮的实现
  • ubuntu 系统下,安装stable diffusion解决下载速度慢的问题
  • (十五)、把自己的镜像推送到 DockerHub
  • 数模方法论-无约束问题求解
  • 科龙睡眠空调小耳朵LF上线,“亲身”答疑空调一天多少度电
  • 【二十五】【QT开发应用】无边窗窗口鼠标拖动窗口移动,重写mousePressEvent,mouseMoveEvent函数
  • 专属文生图助手——SD3+ComfyUI文生图部署步骤
  • 安卓Settings值原理源码剖析存储最大的字符数量是多少?
  • css设置动态数组渲染及中间线平均分开显示
  • IMX6UL开发板中断实验(三)
  • 深度学习02-pytorch-01-张量的创建
  • 使用python-pptx拆分PPT文档:将一个PPT文件拆分成多个小的PPT文件
  • 某yandex图标点选验证码逆向
  • 使用双向 LSTM 和 CRF 进行中文命名实体识别
  • Spring全家桶
  • 图为科技大模型一体机,智领未来社区服务
  • C++中stack类和queue类
  • vue3/Element-Plus/路由的使用
  • Flask-Migrate的使用
  • 学生宿舍管理:Spring Boot技术实现
  • 国内外动态sk5
  • react hooks--useRef
  • 结构设计模式 -装饰器设计模式 - JAVA
  • dockerfile案例