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.思维导图

相关推荐
狐狐生风几秒前
Python UV 完整安装教程
开发语言·python·uv
Kiyra4 分钟前
限流不是加个计数器就行:用 Lua 脚本实现多维度原子限流
开发语言·人工智能·网络协议·职场和发展·架构·lua·ai-native
雨落在了我的手上6 分钟前
初识java(二):数据类型与变量
java·开发语言
2301_8152795210 分钟前
实战分享实现 C++ 管理类单例模式:特点与最佳实践
javascript·c++·单例模式
洛水水14 分钟前
【力扣100题】22. 矩阵置零
算法·leetcode·矩阵
Liangwei Lin14 分钟前
LeetCode 78. 子集
数据结构·算法·leetcode
xcjbqd017 分钟前
提升Python编程效率的五大特性
开发语言·python
平凡但不平庸的码农22 分钟前
Go GMP 调度模型详解
开发语言·后端·golang
多加点辣也没关系27 分钟前
数据结构与算法|第二十四章:算法思维总结与实战
算法·代理模式
2401_8784545328 分钟前
js的复习(一)
开发语言·javascript·ecmascript