<二>自己实现简单的string
我们结合运算符重载知识实现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的更多相关文章
- 二维码简单Demo
二维码简单Demo 一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...
- kafka原理和实践(二)spring-kafka简单实践
系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...
- APS.NET MVC4生成解析二维码简单Demo
一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...
- MongoDB学习:(二)MongoDB简单使用
MongoDB学习:(二)MongoDB简单使用 MongoDB使用: 执行mongodb的操作之前,我们需要运行命令,来进入操作命令界面 >mongo 提示该错误,说明我们系统缺少一个补丁,该 ...
- linux设备驱动归纳总结(十二):简单的数码相框【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-116926.html linux设备驱动归纳总结(十二):简单的数码相框 xxxxxxxxxxxxxx ...
- scrapy爬虫学习系列二:scrapy简单爬虫样例学习
系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...
- 实验二:MAL——简单后门 by:赵文昊
实验二:MAL--简单后门 一.后门是什么? 哪里有后门呢? 编译器留后门 操作系统留后门 最常见的当然还是应用程序中留后门 还有就是潜伏于操作系统中或伪装为特定应用的专用后门程序. 二.认识netc ...
- spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略
spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...
- phpqrcode生成动态二维码简单实例
这是一个利用phpqrcode生成动态二维码简单实例,比微信官方提供的接口还要好用.二维码是动态的,不用生成图片,可自定义二维码大小,间隙,跳转地址等. 参数设置: include_once 'php ...
- pytho创建二维码简单版
pytho创建二维码简单版 import qrcode aa = qrcode.make("https://github.com/phygerr/") aa.save('C:\Us ...
随机推荐
- 不当使用 union all 导致的SQL解析时间过长的问题优化
在帮助用户优化应用过程中,发现用户大量使用union all 导致SQL解析非常缓慢的问题.考虑到这个问题很有代表意义,我觉得很有必要对于问题进行总结. 一.用户例子 WITH company_use ...
- Spring集成测试
Spring 集成测试 需要再类的头部加入 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath ...
- 部署Netlify站点博客
Netlify站点部署静态博客 今天尝试把站点部署在Netlify上,因为部署在GitHub Pages上,国内访问速度太慢了,所以就尝试一下别的站点,部署成功之后发现速度还是不太行,后边继续找找原因 ...
- 搭建Elasitc stack集群需要注意的日志问题
文章转载自:https://blog.csdn.net/u013613428/article/details/84943577 {{uploading-image-736853.png(uploadi ...
- Prometheus样本采集器汇总
系统基本信息 Linux Servers: Exporter https://github.com/prometheus/node_exporter Dashboard https://grafana ...
- Linux下从零开始创建lvm虚拟磁盘阵列+脚本化解决方案
逻辑卷管理器(英语:Logical Volume Manager,缩写为LVM),又译为逻辑卷宗管理器.逻辑扇区管理器.逻辑磁盘管理器,是Linux核心所提供的逻辑卷管理(Logical volume ...
- 使用 Skywalking Agent,这里使用sidecar 模式挂载 agent
文章转载自:https://bbs.huaweicloud.com/blogs/315037 方法汇总 Java 中使用 agent ,提供了以下三种方式供你选择 使用官方提供的基础镜像 将 agen ...
- k8s中使用到的各种证书图示
- 洛谷P4630 [APIO2018] Duathlon 铁人两项 (圆方树)
圆方树大致理解:将每个点双看做一个新建的点(方点),该点双内的所有点(圆点)都向新建的点连边,最后形成一棵树,可以给点赋予点权,用以解决相关路径问题. 在本题中,方点点权赋值为该点双的大小,因为两个点 ...
- MySQL之安装(linux两种版本版本安装)
LinuxMySQL安装(Mysql5.5版本) 第一种 有安装包的安装方式 1.下载地址: http://dev.mysql.com/downloads/mysql 2.检查当前系统是否安装过mys ...