c++9月23日

1.My_string

头文件

复制代码
#ifndef MY_STRINGHEAD_H
#define MY_STRINGHEAD_H


#include <iostream>
#include <cstring>
using namespace std;

class My_string
{
private:
    char *ptr;//指向字符数组的指针
    int size;//字符数组的最大容量
    int len ;//当前字符串的长度
public:
    My_string()
    {
        this->size=15;
        this->len=0;
        this->ptr[len]='\0';
    }
    My_string(const char *src);//有参构造
    My_string(const My_string & other);//拷贝构造
    My_string & operator=(const My_string &other);
    ~My_string();//析构函数
    bool My_empty();//判空
    void push_back(char value);//尾插
    void pop_back();//尾删
    char * data ();
    char &at(int index);
    int get_len();
    int get_size();
    void Double_String();//双倍扩容
    void show();
};

#endif // MY_STRINGHEAD_H

功能函数

复制代码
#include "my_stringhead.h"
    My_string::My_string(const char *src)//有参构造
    {
        this->size = strlen(src);
        this->len=this->size;
        this->ptr = new char[this->size+1];
        strcpy(ptr,src);
    }
     My_string::My_string(const My_string & other):size(other.size),len(other.len)//拷贝构造
    {
        this->ptr = new char[this->size+1];
        strcpy(this->ptr,other.ptr);
    }
    My_string & My_string::operator=(const My_string &other)//拷贝赋值
    {
        if(this!=&other)
        {
            this->size = other.size;
            this->len = other.len;
            strcpy(this->ptr,other.ptr);
        }
        return *this;
    }
     My_string::~My_string()//析构函数
    {
        delete this->ptr;
    }
   bool My_string:: My_empty()//判空
    {
        return len==0;
    }
    void My_string:: push_back(char value)//尾插
    {
        this->size++;
        char *Newptr=new char[this->size];
        strcpy(Newptr,ptr);
        delete this->ptr;
        this->ptr=Newptr;
        this->ptr[this->len]=value;
        this->len++;
        this->ptr[this->len]='\0';
    }
    void My_string::pop_back()//尾删
    {
        this->size--;
        this->len--;
        this->ptr[this->len]='\0';
    }
    char & My_string::at(int index)
    {
        if(index>=0&&index<this->len)
        {
            return this->ptr[index];
        }
        else
        {
            cout<<"输入不合法"<<endl;
            exit(-1);
        }
    }
    char *My_string::data ()//转换为c风格字符串
    {
        return this->ptr;
    }
    int  My_string::get_len()
    {
        return this->len;
    }
    int My_string:: get_size()
    {
        return this->len;
    }
    void My_string::Double_String()//双倍扩容
    {
        if(this->len+1>=this->size)
        {
            this->size*=2;
            char *Newptr=new char[this->size];
            strcpy(Newptr,ptr);
            delete this->ptr;
            this->ptr=Newptr;
        }
    }
    void My_string:: show()
    {
        cout<<this->ptr<<endl;
    }

主函数

复制代码
#include "my_stringhead.h"

int main()
{
    My_string str("hello world");
    str.show();
    My_string str1;
    str1.operator = (str);
    str1.show();
    return 0;
}

2.思维导图

相关推荐
GreenTea3 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
码匠许师傅3 小时前
【C++ 面试真题】聊聊 C++ 的移动语义与右值引用
java·c++·面试
2603_965148113 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
fqq34 小时前
力扣刷题前置Java语法
算法·leetcode·职场和发展
小小龙学IT5 小时前
DuckDB 深度实战:用 C++ 在进程内跑一个「分析型数据库」
数据库·c++
知无不研6 小时前
lambda表达式的使用(3)
开发语言·c++·lambda
2501_906565126 小时前
数学之美探究
算法
oier_Asad.Chen6 小时前
【洛谷题解/AcWing题解/OI学习笔记】洛谷P2868【USACO07DEC】Sightseeing Cows G(01分数规划求最大比率)
算法·图论·spfa·二分·负环
cfm_29146 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型
C++ 老炮儿的技术栈8 小时前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐