博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 一个自己实现的字符串类
阅读量:6993 次
发布时间:2019-06-27

本文共 3922 字,大约阅读时间需要 13 分钟。

hot3.png

StringEx是一个自己实现的字符串类,实现了最基本的初始化和赋值功能,重载了流运算符和加法运算符。编译环境是Windows下的 MinGW 5.1.6

#include
#include
using namespace std;//字符串类class StringEx{public: //构造函数 StringEx(char* s = NULL) { this -> m_Data = s; } StringEx(const StringEx &Other) { m_Data = new char[strlen(Other.m_Data) + 1]; if(this -> m_Data) { strcpy(this -> m_Data, Other.m_Data); } } //以输出 char* ToString() { return m_Data; } //设置字符串的值 void SetString(char* s) { delete[] m_Data; m_Data = new char[strlen(s) + 1]; if(this -> m_Data) { strcpy(this -> m_Data, s); } } //在MinGW中这个函数与上面的函数: // StringEx(const StringEx &Other) //只要声明一个即可实现赋值 StringEx operator = (StringEx &Other) { if(this == &Other) { return *this; } delete[] this -> m_Data; this -> m_Data = new char[strlen(Other.m_Data + 1)]; if(this -> m_Data) { strcpy(this -> m_Data, Other.m_Data); } return *this; } //重载加法运算符,将两个字符串连接 StringEx operator + (StringEx &Another) { if(Another.ToString() == NULL) { return *this; } char* s = new char[strlen(this -> m_Data) + strlen(Another.m_Data) + 1]; int i, j; for(i = 0, j = 0; i < strlen(this -> m_Data); i++, j++) { s[j] = m_Data[j]; } for(i = 0; i < strlen(Another.m_Data); i++, j++) { s[j] = Another.m_Data[i]; } s[j] = '\0'; return StringEx(s); } //重载加法运算符,将字符串类与字符串连接 StringEx operator + (char* s2) { if(s2 == NULL) { return *this; } char* s = new char[strlen(this -> m_Data) + strlen(s2) + 1]; int i, j; for(i = 0, j = 0; i < strlen(this -> m_Data); i++, j++) { s[j] = m_Data[j]; } for(i = 0; i < strlen(s2); i++, j++) { s[j] = s2[i]; } s[j] = '\0'; return StringEx(s); } //析构函数 ~StringEx(void) { delete m_Data; }private: //字符串内容 char *m_Data;};//重载加法运算符,将字符串与字符串类连接StringEx operator + (char* s1, StringEx &Another){ if(s1 == NULL) { return Another; } char* s2 = Another.ToString(); char* s = new char[strlen(s1) + strlen(s2) + 1]; int i, j; for(i = 0, j = 0; i < strlen(s1); i++, j++) { s[j] = s1[j]; } for(i = 0; i < strlen(Another.ToString()); i++, j++) { s[j] = s2[i]; } s[j] = '\0'; return StringEx(s);}//重载流插入运算符ostream& operator << (ostream& output, StringEx& se){ output << se.ToString(); return output;}//重载流提取运算符istream& operator >> (istream& input, StringEx& se){ //char* s = "Galatea4321?"; char *s = new char[1024]; scanf("%s", s); se.SetString(s); return input;}int main(){ cout << "字符串类StringEx" << endl << endl; //3种赋值 //以printf形式输出 "Tsybius2014!" StringEx x = StringEx("Tsybius2014!_1"); printf("%s\n", x.ToString()); //1 StringEx y = "Tsybius2014!_2"; printf("%s\n", y.ToString()); //2 StringEx z = x; printf("%s\n\n", z.ToString()); //3 //以cout形式输出 "Tsybius2014!" cout << x << endl; cout << y << endl; cout << z << endl << endl; //通过cin读取输入的字符串 cout << "请输入一个字符串" << endl; StringEx a; cin >> a; cout << a << endl << endl; //连接两个字符串 StringEx ta = StringEx("Tsybius1234"); cout << "ta:" << ta << endl; StringEx tb = StringEx("4321Galatea"); cout << "tb:" << tb << endl; StringEx tc = ta + tb; //两个StringEx类连接 cout << "tc:" << tc << endl; StringEx td = ta + "4321Galatea"; //StringEx类与字符串连接 cout << "td:" << td << endl; StringEx te = "Tsybius1234" + tb; //字符串与StringEx类连接 cout << "te:" << te << endl; return 0;}

程序运行结果

210113_ATai_1425762.png

END

转载于:https://my.oschina.net/Tsybius2014/blog/315700

你可能感兴趣的文章
前端框架VUE----对象的单体模式
查看>>
New Concept English three(17)
查看>>
New Concept English three (53)
查看>>
CSS Hack
查看>>
Polysh实现多服务器批量执行shell
查看>>
矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
查看>>
jquery ajax中error返回错误解决办法
查看>>
maven核心,pom.xml详解
查看>>
Python2处理字符集问题
查看>>
互联网“平滑数据迁移”架构技术实践
查看>>
论相貌
查看>>
python爬虫和数据分析、数据挖掘
查看>>
我理解的数据库事务
查看>>
D. Frets On Fire 前缀和+二分
查看>>
solrnet - document
查看>>
第十一节: 封装通用的定时调度框架,实现新增、删除、开启、暂停计划任务:...
查看>>
checkbox阻止事件
查看>>
关于HTTP协议学习(二)
查看>>
(转)asp.net 高质量缩略图
查看>>
【面经】阿里学长小谈面试
查看>>