9.23 My_string.cpp

devtools/2024/9/24 5:10:34/

在这里插入图片描述

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.ppmy.cn/devtools/116342.html

相关文章

解构拼duoduo电商api数据海量详情商品自动化

拼多多作为中国领先的社交电商平台&#xff0c;为商家和开发者提供了丰富的 API 接口以获取商品详情数据&#xff0c;实现商品价格监控自动化。这些 API 接口旨在帮助商家和开发者在开发过程中快速、准确地获取所需数据&#xff0c;提高开发效率和业务运营水平。 获取商品详情…

verilog中非阻塞多个if 优先级判断。

always(posedge clk)beginz < 0;if(sel0)z < a;if(sel1)z < b;if(sel2)z < c;if(sel3)z < d;end 比如上述代码&#xff0c;最后一级的优先级最高。

centos7安装docker DokcerCompose

一, 安装docker 1.更新yum源 yum下载很慢&#xff0c;一直出现正在尝试其它镜像&#xff0c;更改yum地址为阿里云镜像即可 1&#xff09;下载了阿里云提供的CentOS 7的Yum源配置文件&#xff0c;并将其覆盖到系统中的 /etc/yum.repos.d/CentOS-Base.repo 文件。 wget -O /et…

Webpack 常见配置项

1. entry 指定一个或多个入口点&#xff0c;Webpack 从这里开始构建依赖图。 entry: {main: ./src/index.js,admin: ./src/admin.js }2. output 指定输出文件的路径和名称。 output: {filename: [name].bundle.js,path: path.resolve(__dirname, dist),publicPath: /assets…

YOLOv9改进策略【注意力机制篇】| 2024 SCI TOP FCAttention 即插即用注意力模块,增强局部和全局特征信息交互

一、本文介绍 本文记录的是基于FCAttention模块的YOLOv9目标检测改进方法研究。FCAttention是图像去雾领域新提出的模块能够有效整合全局和局部信息、合理分配权重的通道注意力机制,使得网络能够更准确地强调有用特征,抑制不太有用的特征,在目标检测领域中同样有效。 专栏目…

idea插件开发系列1-环境搭建

前言 还记着10多年前有幸接触了eclipse插件开发&#xff0c;10多年后的今天有开发了idea的插件&#xff0c;真是一个轮回&#xff01; 为什么要学习idea插件开发呢&#xff1f; 目前公司使用自己的MVC框架&#xff0c;没有相应的idea插件支持&#xff08;如类似mybatis插件可…

xml中的转义字符

文章目录 xml中的转义字符 xml中的转义字符 &amp;对应的字符是& <对应的字符是< >对应的字符是> &quot;对应的字符是" &apos;对应的字符是转义的实体引用虽然简单易用&#xff0c;但是需要记忆&#xff0c;而且如果字符串中包含大量的特殊字…

分发饼干00

题目链接 分发饼干 题目描述 注意点 1 < g[i], s[j] < 2^31 - 1目标是满足尽可能多的孩子&#xff0c;并输出这个最大数值 解答思路 可以先将饼干和孩子的胃口都按升序进行排序&#xff0c;随后根据双指针 贪心&#xff0c;将当前满足孩子胃口的最小饼干分配给该孩…