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

【C++】将myString类中能够实现的操作都实现一遍

myString.h

#ifndef MYSTERAM_H
#define MYSTERAM_H
#include <iostream>
#include<cstring>
using namespace std;
class myString
{
private:char *str;  //字符串int size;   //字符串容量char error[20] = "error";
public://无参构造myString():size(10){size = 10;str = new  char[size]();}//有参构造myString(const char *s){size = strlen(s);    //计算传入的字符串长度str = new char[size];//创建空间为strlen(s)的空间strcpy(str,s);       //拷贝形参给实参}//析构函数~myString(){delete []str;}//为字符串赋值myString operator=(char *s);//访问指定字符,有边界检查char &at(int index);//访问指定字符char &operator[](int index)const;//返回指向字符串首字符的指针char *data()const;//返回字符串不可修改的C字符数组版本char c_str()const;//检查字符串是否为空bool empty();//返回字符数int mysize();//返回字符数int length();//返回当前对象分配的存储空间能保存的字符数量int capacity();//清空内容void clear();//后附字符到结尾bool push_back(char s);//移除末尾字符bool pop_back();//后附字符到结尾bool append(const char s);//后附字符到结尾myString &operator+=(const char s);//连接两个字符串或者一个字符串和一个字符myString &operator+(const char *s);//判断两个字符串是否相等bool operator==(const char *s);//判断两个字符串!=bool operator!=(const char *s);//判断两个字符串<bool operator<(const char *s);//判断两个字符串>bool operator>(char *s);//判断两个字符串<=bool operator<=(const char *s);//判断两个字符串>=bool operator>=(const char *s);//展示void show();friend ostream & operator<<(ostream &L,const myString &s);friend istream & operator>>(istream &in, myString &s);};
//执行字符串的流输入
ostream & operator<<(ostream &L,const myString &s);//执行字符串的流输出
istream & operator>>(istream &in, myString &s);#endif // MYSTERAM_H

myString.cpp

#ifndef MYSTERAM_H
#define MYSTERAM_H
#include <iostream>
#include<cstring>
using namespace std;
class myString
{
private:char *str;  //字符串int size;   //字符串容量char error[20] = "error";
public://无参构造myString():size(10){size = 10;str = new  char[size]();}//有参构造myString(const char *s){size = strlen(s);    //计算传入的字符串长度str = new char[size];//创建空间为strlen(s)的空间strcpy(str,s);       //拷贝形参给实参}//析构函数~myString(){delete []str;}//为字符串赋值myString operator=(char *s);//访问指定字符,有边界检查char &at(int index);//访问指定字符char &operator[](int index)const;//返回指向字符串首字符的指针char *data()const;//返回字符串不可修改的C字符数组版本char c_str()const;//检查字符串是否为空bool empty();//返回字符数int mysize();//返回字符数int length();//返回当前对象分配的存储空间能保存的字符数量int capacity();//清空内容void clear();//后附字符到结尾bool push_back(char s);//移除末尾字符bool pop_back();//后附字符到结尾bool append(const char s);//后附字符到结尾myString &operator+=(const char s);//连接两个字符串或者一个字符串和一个字符myString &operator+(const char *s);//判断两个字符串是否相等bool operator==(const char *s);//判断两个字符串!=bool operator!=(const char *s);//判断两个字符串<bool operator<(const char *s);//判断两个字符串>bool operator>(char *s);//判断两个字符串<=bool operator<=(const char *s);//判断两个字符串>=bool operator>=(const char *s);//展示void show();friend ostream & operator<<(ostream &L,const myString &s);friend istream & operator>>(istream &in, myString &s);};
//执行字符串的流输入
ostream & operator<<(ostream &L,const myString &s);//执行字符串的流输出
istream & operator>>(istream &in, myString &s);#endif // MYSTERAM_H

main.cpp

#include <mySteram.h>int main()
{myString s;//无参构造cout<<"字符串长度"<<s.length()<<endl;cout<<"字符串长度"<<s.mysize()<<endl;cout<<"开辟空间大小"<<s.capacity()<<endl;cout<<"*****************************************"<<endl;myString ss("zhenxinye");//有参构造ss.show();cout<<ss.length()<<endl;cout<<ss.mysize()<<endl;cout<<"开辟空间大小"<<ss.capacity()<<endl;cout<<"*****************************************"<<endl;cout<<"结尾加上字符 a ";ss.append('a');ss.show();cout<<"*****************************************"<<endl;cout<<"结尾加上字符 a ";ss.append('a');ss.show();cout<<"*****************************************"<<endl;cout<<"结尾加上字符 b ";ss+='b';ss.show();cout<<"*****************************************"<<endl;cout<<"删除最后一个字符:";ss.pop_back();ss.show();cout<<"*****************************************"<<endl;cout<<"拼接字符串 hello world:";ss = ss+" hello world";ss.show();cout<<"*****************************************"<<endl;cout<<"ss == zhenxinyeaa hello world?"<<endl;ss == "zhenxinyeaa hello world";cout<<"*****************************************"<<endl;cout<<"cout<<ss "<<ss;cout<<"*****************************************"<<endl;cout<<"cin>>ss "<<endl;cin>>ss;cout<<"--------"<<endl;ss.show();return 0;
}

效果展示


http://www.mrgr.cn/news/18260.html

相关文章:

  • ARM————体系结构
  • Python精选200Tips:6-10
  • Pytorch中不同的Norm归一化详细讲解
  • 自己动手写CPU_step6.1_算数运算指令
  • Fabric.js中fabric.Textbox的深入解析
  • python语言基础(六)--深浅拷贝、闭包与装饰器
  • RDnL自定义按钮按点击的层级不同来隐藏按钮
  • 技术速递|使用 MSTest.Analyzers 增强您的测试体验
  • 【html】新建一个html并且在浏览器运行
  • 【实战教程】用 Next.js 和 shadcn-ui 打造现代博客平台
  • 视频编码标准化组织介绍
  • 嵌入式OTG硬件电路分析
  • 使用神卓互联内网穿透开发支付宝支付回调环境(Java版)
  • 022集—— 字符串按ascii码转数字——C#学习笔记
  • dfs 解决 部分矩阵洪流/floodfill算法题(水流问题、扫雷游戏、衣橱整理、C++)
  • 【mysql】mysql目录结构和源码和mysql基础练习
  • 362_C++_异步添加到队列中后(添加队列的任务数量限制30个),采用定时执行的任务,一个个顺序执行队列中的任务
  • Fabric.js Canvas:核心配置与选项解析
  • 教学能力知识
  • 培训第四十一天(docker-compose一键部署项目,haproxy容器代理多个web或java容器)