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

9.23作业

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

代码如下

MyString.h
#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
#include "MyString.h"

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

//有参构造
My_string::My_string(const char* src):size(15) {
    this->ptr = new char[size];
    strcpy(ptr, src); // 复制字符串
    this->len = strlen(src);
    cout << "一个形参的有参构造" << endl;
}
My_string::My_string(int num, char value):size(15),len(num){
    if(num>15){
        cout<<"超出默认长度"<<endl;
        return;
    }
    this->ptr = new char[15];
    for(int i=0;i<num;i++){
        this->ptr[i] = value;
    }
    cout<<"部分形参的有参构造"<<endl;
}
//拷贝构造
My_string::My_string(const My_string &other):size(other.size),len(other.len){
    this->ptr = new char[size];
    strcpy(this->ptr, other.ptr); // 复制字符串
    cout<<"拷贝构造"<<endl;
}
//拷贝赋值
My_string& My_string::operator= (const My_string &other){
    if(this != &other){
        this->len =other.len;
        this->size = other.size;
        for(int i=0;i<other.len;i++){
            this->ptr[i] =other.ptr[i];
        }
    }
    cout<<"拷贝赋值"<<endl;
    return *this;
}
//析构函数
My_string::~My_string(){

    cout<<this->ptr<<"析构函数"<<endl;
    delete[] ptr;
}
//判空
bool My_string::Isvoid(){
    return this->len ==0 ? true:false;
}
//显示
void My_string::show(){
    for(int i=0;i<this->len;i++){
        cout<<*(this->ptr+i);
    }
    cout<<endl;
}
//尾插
void My_string::push_back(char value){
    if(this->len < this->size-1){
        *(this->ptr+len++) = value;
    }else if(this->Add()){
        *(this->ptr+len++) = value;
    }
}
//尾删
void My_string::pop_back(){
    this->len--;
}
//at函数实现
char& My_string::at(int index){
    if(index<=this->len-1){
        return this->ptr[index];
    }else {
        cout<<"下标越界"<<endl;
        exit(EXIT_SUCCESS);
    }
}
//清空函数
void My_string::clear(){
    free(this->ptr);
    this->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;
}

bool My_string::Add(){
    if(this->len == this->size-1){
        char *p = new char[size*2];
        strcpy(p,this->ptr);
        free(this->ptr);
        this->ptr = p;
        return true;
    }else return false;
}

main.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的值:"<<s3.at(4)<<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;
//    char c =' ';
//    while(c!='#'){
//        cin>>c;
//        s3.push_back(c);
//    }
    //s3.show();
}

运行结果

在这里插入图片描述

思维导图

在这里插入图片描述


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

相关文章:

  • 无人机之激光避障篇
  • 3.4 爬虫实战-爬去智联招聘职位信息
  • 什么是反射,反射用途,spring哪些地方用到了反射,我们项目中哪些地方用到了反射
  • 【python】requests 库 源码解读、参数解读
  • Maven笔记(一):基础使用【记录】
  • Spring Boot 中的拦截器 Interceptors
  • 【已解决】用JAVA代码实现递归算法-从自然数中取3个数进行组合之递归算法-用递归算法找出 n(n>=3) 个自然数中取 3 个数的组合。
  • 在云渲染中3D工程文件安全性怎么样?
  • 【HarmonyOS】深入理解@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化
  • Unity-Screen屏幕相关
  • 【设计模式】万字详解:深入掌握五大基础行为模式
  • 鸿蒙 OS 开发零基础快速入门教程
  • ER论文阅读-Incomplete Multimodality-Diffused Emotion Recognition
  • 【LLM学习之路】9月22日 第九天 自然语言处理
  • 计算一个矩阵的逆矩阵的方法
  • 2024ICPC网络赛第一场C. Permutation Counting 4(线性代数)
  • nginx的反向代理和负载均衡
  • 16.3 k8s容器cpu内存告警指标与资源request和limit
  • 【数据结构-栈】力扣682. 棒球比赛
  • 0-1开发自己的obsidian plugin DAY 1
  • 鸿蒙操作系统(HarmonyOS)生态与机遇
  • YOLOv10改进,YOLOv10替换主干网络为PP-HGNetV1(百度飞桨视觉团队自研,全网首发,助力涨点)
  • watch和computed的使用及区别
  • Correcting Chinese Spelling Errors with Phonetic Pre-training(ACL2021)
  • Python Web 面试题
  • Spring Boot自定义配置项
  • [leetcode刷题]面试经典150题之6轮转数字(简单)
  • k8s上安装prometheus
  • 字母与符号检测系统源码分享
  • ubuntu、linux安装redis(使用tar包的方式)