我们结合运算符重载知识实现string 类

在自己实现的String类中可以参考C++中string的方法

例如构造,加法,大小比较,长度,[] 等操作.

当前的MyString 类中,暂时不加入迭代器,我们将在下一节中加入迭代器的代码.

#include <iostream>
using namespace std; class MyString { public: //构造函数
MyString(const char * pSource = nullptr) {
if (pSource != nullptr) {
pString = new char[strlen(pSource) + 1];
strcpy(pString, pSource);
}
else {
pString = new char[1];
pString[0] = '\0';
}
cout << "MyString构造函数,对象地址="<<this << endl;
} //拷贝构造
MyString(const MyString & _rValue) { pString = new char[strlen(_rValue.pString) + 1];
strcpy(pString, _rValue.pString);
cout << "MyString拷贝构造函数" << endl;
} //赋值函数
MyString & operator=(const MyString & _rValue) { if (this == &_rValue) {
return *this;
}
delete[] this->pString;
this->pString = nullptr; char * _tpString = new char[strlen(_rValue.pString) + 1];
strcpy(_tpString, _rValue.pString); cout << "MyString赋值函数" << endl; } //可编辑
char & operator[](int index) { int len = strlen(this->pString);
if (index<0) { return pString[0]; }
else if (index>len) { return pString[index]; }
else { return pString[index]; } } //不可编辑
const char operator[](int index) const { int len = strlen(this->pString);
if (index<0) { return pString[0]; }
else if (index>len) { return pString[index]; }
else { return pString[index]; } } bool operator>(const MyString & _rValue) const {
return strcmp(this->pString, _rValue.pString)>0;
}
bool operator<(const MyString & _rValue) const {
return strcmp(this->pString, _rValue.pString)<0;
}
bool operator==(const MyString & _rValue) const {
return strcmp(this->pString, _rValue.pString)==0;
} int length() const {
return strlen(this->pString);
} ~MyString() {
if (this->pString != nullptr) {
delete[] this->pString;
this->pString = nullptr;
cout << "MyString析构函数" << this << endl;
}
} const char * c_str() const { return this->pString; } private:
char * pString ;
friend MyString operator+ (const MyString & s1, const MyString &s2) ;
friend ostream & operator<<(ostream & out, const MyString &s) ; }; MyString operator+(const MyString & s1, const MyString &s2) { /*
方式1 这段代码有 内存泄漏问题 tp 没有释放掉
int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
char *tp = new char[newLength + 1];//重新申请空间
strcpy(tp, s1.pString);
strcat(tp, s2.pString); MyString s(tp);
cout << "operator+ = " << &s << endl;
return s;
*/ /*
方式2 对比方式1 效果更高
*/
MyString s;
int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
s.pString = new char[newLength + 1];//重新申请空间
strcpy(s.pString, s1.pString);
strcat(s.pString, s2.pString);
cout << "operator+ = " << &s << endl;
return s;
} ostream & operator<<(ostream & out, const MyString &s) {
cout << s.pString << endl;
return out;
} void test() { MyString s1("12345");
MyString s2("6789ABC"); cout << s1 << endl;
cout << s2 << endl; MyString s3 = s1 + s2;
cout << s3 << endl;
cout << "s3 = " << &s3 << endl; for (int i = 0; i < s3.length(); i++) {
cout << s3[i] << endl;
} cout <<"--------------------" << endl;
s3[0] = 'W';
for (int i = 0; i < s3.length(); i++) {
cout << s3[i] << endl;
} const MyString s4("hello"); for (int i = 0; i < s4.length(); i++) {
cout << s4[i] << endl;
} }
int main() { test(); system("pause");
return 0;
}

<二>自己实现简单的string的更多相关文章

  1. 二维码简单Demo

    二维码简单Demo 一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...

  2. kafka原理和实践(二)spring-kafka简单实践

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  3. APS.NET MVC4生成解析二维码简单Demo

    一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

  4. MongoDB学习:(二)MongoDB简单使用

    MongoDB学习:(二)MongoDB简单使用 MongoDB使用: 执行mongodb的操作之前,我们需要运行命令,来进入操作命令界面 >mongo 提示该错误,说明我们系统缺少一个补丁,该 ...

  5. linux设备驱动归纳总结(十二):简单的数码相框【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-116926.html linux设备驱动归纳总结(十二):简单的数码相框 xxxxxxxxxxxxxx ...

  6. scrapy爬虫学习系列二:scrapy简单爬虫样例学习

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  7. 实验二:MAL——简单后门 by:赵文昊

    实验二:MAL--简单后门 一.后门是什么? 哪里有后门呢? 编译器留后门 操作系统留后门 最常见的当然还是应用程序中留后门 还有就是潜伏于操作系统中或伪装为特定应用的专用后门程序. 二.认识netc ...

  8. spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略

    spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...

  9. phpqrcode生成动态二维码简单实例

    这是一个利用phpqrcode生成动态二维码简单实例,比微信官方提供的接口还要好用.二维码是动态的,不用生成图片,可自定义二维码大小,间隙,跳转地址等. 参数设置: include_once 'php ...

  10. pytho创建二维码简单版

    pytho创建二维码简单版 import qrcode aa = qrcode.make("https://github.com/phygerr/") aa.save('C:\Us ...

随机推荐

  1. JS中如何删除某个父元素下的所有子元素?

    JS中如何删除某个父元素下的所有子元素?这里我介绍几种方法: 1.通过元素的 innerHTML 属性来删除 这种方式我觉得是最有方便的,直接找到你想要的父元素,直接令其 element.innerH ...

  2. C#,启动exe程序并传参(参数间带&符号)方法

    入参格式例如:C:\\Users\\Administrator\\Desktop\\测试\\测试\\bin\\Debug\\测试.exe type=1^&card_no=123 public ...

  3. 谣言检测——(GCAN)《GCAN: Graph-aware Co-Attention Networks for Explainable Fake News Detection on Social Media》

    论文信息 论文标题:GCAN: Graph-aware Co-Attention Networks for Explainable Fake News Detection on Social Medi ...

  4. Nessus-8.11.1-x64.msi安装包

    希望能给那些和我一样迷茫受挫的小伙伴们一些帮助,这玩意儿下载挺慢的,我把安装包分享出来,如果有博客园账号的,点个赞呗,CSDN那些用着别人的软件还要积分,呸! 08-18更新,截止到现在,已更新到最新 ...

  5. Apache开启目录浏览功能的正确姿势

    部分代码是抄的网友的,哪儿抄的忘了,我是自己修改了点,并非我本人原创,觉得侵权联系我,立马删 在宝塔面板中,打开网站的设置,在配置文件那里添加如下内容 Directory "文件绝对路径&q ...

  6. Codeforces Round #821(Div.2) (A-C) 题解

    Codeforces Round #821(Div.2) (A-C) A.Consecutive Sum 大致题意 给定一组共 n 个数据 ,如果俩个数的下标在 mod k 意义下同余,则可以交换a[ ...

  7. 【微服务】- Nacos - 注册中心

    微服务 - 注册中心 - Nacos 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 上一 ...

  8. Kubernetes(k8s)通过环境变量将 Pod 信息呈现给容器

    Downward API 有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器: 环境变量 卷文件 这两种呈现 Pod 和 Container 字段的方式统称为 Downward ...

  9. DeepHyperX代码理解-HamidaEtAl

    代码复现自论文<3-D Deep Learning Approach for Remote Sensing Image Classification> 先对部分基础知识做一些整理: 一.局 ...

  10. SQL通用语法和SQL分类

    SQL通用语法 1.SQL 语句可以单行或多行书写,以分号结尾 2.可使用空格和缩进来增强语句的可读性 3.MySQL 数据库的SQL语句不区分大小写,关键字建议使用大写 4.3种注释 单行注释: - ...