<二>自己实现简单的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 ...
随机推荐
- 函数索引引用的函数必须是immutable类型
用户在使用中,可能会用到基于函数的索引,但是函数是非 immutable 类型的,导致函数索引无法创建.如: test=# create index ind_t1 on t1(to_char(crea ...
- 02-MyBatisPlus入门
快速开始参考:https://baomidou.com/pages/226c21/ 测试项目: mybatis_plus 数据库:mybatis_plus 一.创建并初始化数据库 1.创建数据库: m ...
- 接入Twitter和Facebook分享踩坑记录
准备工作 1.首先需要在HTML的head添加下述meta标签内容,在分享时,Twitter和Facebook会爬取该网站页面的meta内容,然后生成分享卡片. 2.按照下述配置完成后,需要把内容发布 ...
- Traefik 2.0 实现灰度发布
文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247484478&idx=1&sn=238390dc ...
- 第三章:模版层 - 1:Django模板语言详解
本节将介绍Django模版系统的语法.Django模版语言致力于在性能和简单性上取得平衡. 如果你有过其它编程背景,或者使用过一些在HTML中直接混入程序代码的语言,那么你需要记住,Django的模版 ...
- ubuntu开启sshd
SSH分客户端openssh-client和openssh-server 如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-g ...
- 3.在 Kubernetes 上安装 Gitlab CI Runner
结合文章:1. 在 Kubernetes 上安装 Gitlab ,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13852854.html 总结: 结合开头的文章 ...
- 字符串反码A
while True: try: string=input() if string!="!": res="" for i in string: if i.isu ...
- PAT (Basic Level) Practice 1018 锤子剪刀布 分数 20
大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入 ...
- 标题,ico动态化
//获取ico元素 var link = document.querySelector("link[rel*='icon']"); link.href = "image/ ...