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

C++QT day4

仿照string类,完成myString类

#include <iostream>
#include <cstring>
using namespace std;
class myString
{private:char *str;          //记录c风格的字符串int size;            //记录字符串的实际长度public://无参构造myString():size(10){str = new char[size];         //构造出一个长度为10的字符串strcpy(str,"");         //赋值为空串}//有参构造myString(const char *s)          //string  s("hello world"){size = strlen(s);str = new char[size+1];strcpy(str, s);}//拷贝构造myString(const myString &other):str(new char(*other.str)),size(other.size){cout<<"拷贝构造函数"<<endl;}//析构函数~myString(){delete str;//释放成员指针的空间cout<<"mySting::析构函数"<<endl;}//拷贝赋值函数myString &operator=(const myString &other){if(this!=&other){this->size=other.size;if(this->str!=NULL){delete this->str;}this->str=new char(*other.str);}cout<<"myString::拷贝赋值函数"<<endl;}//判空函数bool my_empty(){if(0==my_size()){return true;}return false;}//size函数int my_size(){if(*str!=0){str++;size++;}return size;}//c_str函数char *c_str(){return this->str;}//at函数char &at(int pos){if(pos >= 0 && pos< size){return this->str[pos];}else{cout<<"数组越界"<<endl;}}//加号运算符重载const myString operator+ (const myString &R)const{myString c;c.str=strcat(this->str, R.str);return c;}//加等于运算符重载myString operator+=(const myString &other){strcat(str,other.str);size+=other.size;return *this;}//关系运算符重载(>)bool operator> (const myString &R)const{if(strcmp(this->str,R.str) > 0){return true;}elsereturn false;}//中括号运算符重载char & operator[](int i){if(i >= 0 && i< size){return this->str[i];}else{cout<<"数组越界"<<endl;}}
};
int main()
{myString mystr("hello");myString mystr1("world");myString mystr2=mystr+mystr1;cout<<mystr.at(1)<<endl;cout<<mystr[1]<<endl;return 0;
}

思维导图

http://www.lryc.cn/news/163973.html

相关文章:

  • Python中的 if __name__ ==‘main‘
  • github 创建自己的分支 并下载代码
  • 算法:贪心---跳一跳
  • 机器学习入门教学——梯度下降、梯度上升
  • BUUCTF Reverse/[羊城杯 2020]login(python程序)
  • indexDB localForage
  • Spring Boot开发时Java对象和Json对象互转
  • C++ 多态
  • LeetCode 之 二分查找
  • 【性能测试】中间件优化
  • 【算法】查找类——二分查找算法
  • Ansible FIle模块,使用Ansible File模块进行文件管理
  • 索尼mp4变成rsv修复案例(ILME-FX3)
  • 抓拍摄像机开关量控制4K高清手机远程看图建筑生长定时缩时相机
  • c++使用http请求-drogon框架
  • 幼儿棒球运动宣传介绍·野球6号位
  • grpc多语言通信之GO和DART
  • 基于FPGA的RGB图像转Ycbcr实现,包括tb测试文件以及MATLAB辅助验证
  • centos 编译安装的php多版本 切换
  • Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例
  • 2023数学建模国赛E题黄河水沙监测数据分析完整代码分析+处理结果+思路文档
  • 玩转Mysql系列 - 第19篇:游标详解
  • 【量化分析】Python 布林线( Bollinger)概念
  • MySQL的权限管理与远程访问
  • 在Qt创建的UI中放一个显示点云的窗口(PCL+QT5)
  • element ui el-table分页多选功能
  • 理解网络通信的基础:OSI七层模型与TCP/IP五层模型
  • Python爬虫-爬取文档内容,如何去掉文档中的表格,并保存正文内容
  • 【使用Cpolar和Qchan搭建自己的个人图床】
  • flutter解决多个类名重名问题