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

9.23 My_string.cpp

在这里插入图片描述

my_string.h

#ifndef MY_STRING_H
#define MY_STRING_H#include <iostream>
#include <cstring>using namespace std;class My_string
{
private:char *ptr;         //指向字符数组的指针int size;           //字符串的最大容量int len;            //字符串当前容量public://无参构造My_string():size(20){cout<<"****************无参构造***********"<<endl;this->ptr = new char[size];this->ptr[0] = '\0';            //表示串为空串this->len = 0;}//有参构造My_string(const char* src){cout<<"****************一个参数有参构造***********"<<endl;this->len = strlen(src);this->size = len + 1;this->ptr = new char[size];strcpy(this->ptr,src);}My_string(int num,char value){cout<<"****************两个参数有参构造***********"<<endl;this->len = num;this->size = len + 1;this->ptr = new char[len+1];int i=0;while(i<num){this->ptr[i]=value;i++;}this->ptr[num]='\0';}//拷贝构造My_string (const My_string &other){cout<<"****************拷贝构造***********"<<endl;len = other.len;size = other.size;ptr = new char[size];strcpy(ptr, other.ptr);}//拷贝赋值My_string &operator=(const My_string &other){cout<<"****************拷贝赋值***********"<<endl;if (this == &other) {return *this;  // 直接返回当前对象}delete[] ptr;this->len = other.len;this->size = other.size;this->ptr = new char[size];strcpy(ptr, other.ptr);return *this;}//析构函数~My_string(){cout<<"****************析构函数***********"<<endl;delete []this->ptr;}//显示内容void show();//判空void isempty();//判满void isfull();//尾插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 resize();};#endif // MY_STRING_H

my_string.cpp

#include "my_string.h"
#include <cstring>
void My_string::show(){cout<<ptr<<endl;
}
//判空
void My_string::isempty(){if(this->len==0){cout<<"字符串为空"<<endl;}return;
}
//判满
void My_string::isfull(){if(this->len>=this->size){cout<<"字符串满"<<endl;resize();cout<<"重新分配空间"<<endl;}else{cout<<"字符串未满"<<endl;}return;
}//尾插
void My_string::push_back(char value){this->isfull();this->ptr[len]=value;len++;ptr[len]='\0';
}//尾删
void My_string::pop_back(){this->len=this->len-1;ptr[len]='\0';
}
//at函数实现
char & My_string::at(int index){return ptr[index];
}
//清空函数
void My_string::clear(){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;
}//君子函数:二倍扩容
void My_string::resize() {size *= 2;char* new_ptr = new char[size];strcpy(new_ptr, ptr);delete[] ptr;ptr = new_ptr;}

main.cpp

#include "my_string.h"int main(){My_string p1;My_string p2("hello");p2.show();My_string p3(5,'A');p3.show();My_string p4 = p2;p4.show();p3=p2;p3.show();p2.show();cout<<"p2的第3个字符"<<p2.at(3)<<endl;cout<<"p2C风格的字符串"<<p2.data()<<endl;p2.isfull();cout<<"p2的最大长度:"<<p2.get_size()<<endl;cout<<"p2现在的长度:"<<p2.get_length()<<endl;p2.clear();p2.show();p2.push_back('W');p2.show();p2.pop_back();p2.show();}
http://www.lryc.cn/news/444865.html

相关文章:

  • 【android10】【binder】【3.向servicemanager注册服务】
  • Java — LeetCode 面试经典150题(一)
  • Python酷玩之旅_mysql-connector
  • 7.搭建个人金融数据库之快速获取股票列表和基本信息!
  • Nginx基础详解1(单体部署与集群部署、负载均衡、正反代理、nginx安装)
  • 等保一体机如何帮你应对网络攻击
  • CVE-2024-1112 Resource Hacker 缓冲区溢出分析
  • WebGL渲染与创建2D内容
  • ArcGIS Desktop使用入门(三)图层右键工具——拓扑(下篇:地理数据库拓扑)
  • LeetCode题练习与总结:二叉树的最近公共祖先--236
  • uni-app 多环境配置
  • 【d48】【Java】【力扣】LCR 123. 图书整理 I
  • 【MySQL】InnoDB 索引为什么使用B+树而不用跳表?
  • 【学习笔记】TLS/SSL握手之Records
  • 【MySQL】创建新账号新数据库并授权
  • Nginx反向代理简介,作用及配置;Nginx负载均衡简介,作用及配置;
  • SAP MIGO M7146不支持移动原因
  • vue使用PDF.JS踩的坑--部署到服务器上显示pdf.mjs viewer.mjs找不到资源
  • 重型工程车辆数据集
  • 【Kubernetes】常见面试题汇总(三十三)
  • ubuntu安装无线网卡驱动(非虚拟机版)
  • 保障电气安全的电气火灾监控系统主要组成有哪些?
  • gitlab集成CI/CD,shell方式部署
  • UE学习篇ContentExample解读-----------Blueprint_Mouse_Interaction
  • 得物App荣获新奖项,科技创新助力高质量发展
  • 傅里叶变换(对称美)
  • 基于单片机与 PC 机通信的数据采集控制系统设计
  • MyBatis参数处理
  • Beyond 5.5旗舰版和高级版激光软件
  • python爬虫/引用requests/基本使用