C++(9.23)

主函数

cpp 复制代码
#include"head.h"
int main()
{
    My_string s2("hello");
    My_string s3(5,'A');
    s2.show();
    s3.show();
    My_string s1=s2;
    s1.show();
    My_string s4;
    s4=s3;
    s4.show();
    if(s4.empty_if())
    {
        cout<<"该字符数组为空"<<endl;
    }
    else
    {
        cout<<"该字符数组不为空"<<endl;
    }
     s1.push_back('!');
     s1.show();

     s1.pop_back();
     s1.show();
     cout << "第一个位置是 : " << s1.at(1) << endl;
     s1.clear();
     s1.show();
     cout << "s2当前的长度是 : " << s2.get_length()<<"s3当前的最大容量是"<<s3.get_size()<<endl;

    return 0;
}

头文件:

cpp 复制代码
#ifndef HEAD_H
#define HEAD_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);
             void show();
             bool empty_if();
             void push_back(char value);
             void pop_back();
             char &at(int index);
             void clear();
            int get_length();
             int get_size();
             ~My_string();
};
#endif // HEAD_H

源文件:

cpp 复制代码
#include"head.h"
using namespace std;

My_string::My_string():size(15)
{
    this->ptr =new char[size];
    this->ptr [0]='\0';
    this->len=0;
}
My_string::My_string(const char *src)
{
    this->len=strlen(src);
    this->size=this->len+1;
    this->ptr =new char[size];
    strcpy(this->ptr,src);
}
My_string::My_string(int num,char value)
{
    this->len=num;
    this->size=this->len+1;
    this->ptr =new char[num];
    for(int i=0;i<5;i++)
    {
    this->ptr[i]=value;
    }
}
My_string::My_string(const My_string &other)
{
    this->len = other.len;
    this->size = other.size;
    this->ptr = new char[size];  // 分配新的内存
    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;
               this->ptr = new char[size];
               strcpy(this->ptr ,other.ptr);              //深拷贝
           }
           return *this;                  //返回值自身的引用
}
void My_string::show()
{
    cout<<this->ptr<<endl;
}
My_string::~My_string()
{
    delete this->ptr;
}

bool My_string::empty_if()
{
    return this->len==0;
}
void My_string::push_back(char value)
   {

       ptr[len] = value;           // 在尾部添加字符
       ptr[len+1] = '\0';  // 确保字符串以空字符结尾
       this->len++;
   }
   void My_string::pop_back()
   {
       if (this->len > 0)                  // 确保字符串非空
       {

           ptr[len-1] = '\0'; // 更新终止符
           this->len--;
       }
   }
   char &My_string::at(int index)
      {
          return ptr[index-1];             // 返回引用以支持修改
      }
   void My_string::clear()
     {
         this->len = 0;                       // 重置长度为 0
         ptr[0] = '\0';                 // 以空字符结束
     }
   int My_string::get_length()
      {
          return this->len;
      }
   int My_string::get_size()
       {
           return this->size;
       }
相关推荐
光影少年2 分钟前
react离线缓存、图片缓存方案
开发语言·前端·javascript·react native·react.js·缓存·前端框架
鸿芯微控科技3 分钟前
MFC关断后还有流量怎么办?零流量、阀门泄漏、压差与Python分析
c++·python·mfc·质量流量控制器·关断泄漏·零流量测试
SomeB1oody7 分钟前
【RustyML入门】3.7. 循环层
开发语言·后端·机器学习·rust·教程
言乐63 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
WWJA王文举10 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
zhanghaha131411 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
ShineWinsu12 小时前
对于C++:C++11中lambda、function、bind的解析
c++·面试·笔试·开发·lambda·bind·function
码匠许师傅12 小时前
【C++ 面试真题】聊聊 C++ 的拷贝构造与拷贝赋值
java·c++·面试
qq_4480111612 小时前
C语言中的动态内存分配
c语言·开发语言·php
旖旎夜光13 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口