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

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

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

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

  1. #include <iostream>
  2. using namespace std;
  3. class MyString {
  4. public:
  5. //构造函数
  6. MyString(const char * pSource = nullptr) {
  7. if (pSource != nullptr) {
  8. pString = new char[strlen(pSource) + 1];
  9. strcpy(pString, pSource);
  10. }
  11. else {
  12. pString = new char[1];
  13. pString[0] = '\0';
  14. }
  15. cout << "MyString构造函数,对象地址="<<this << endl;
  16. }
  17. //拷贝构造
  18. MyString(const MyString & _rValue) {
  19. pString = new char[strlen(_rValue.pString) + 1];
  20. strcpy(pString, _rValue.pString);
  21. cout << "MyString拷贝构造函数" << endl;
  22. }
  23. //赋值函数
  24. MyString & operator=(const MyString & _rValue) {
  25. if (this == &_rValue) {
  26. return *this;
  27. }
  28. delete[] this->pString;
  29. this->pString = nullptr;
  30. char * _tpString = new char[strlen(_rValue.pString) + 1];
  31. strcpy(_tpString, _rValue.pString);
  32. cout << "MyString赋值函数" << endl;
  33. }
  34. //可编辑
  35. char & operator[](int index) {
  36. int len = strlen(this->pString);
  37. if (index<0) { return pString[0]; }
  38. else if (index>len) { return pString[index]; }
  39. else { return pString[index]; }
  40. }
  41. //不可编辑
  42. const char operator[](int index) const {
  43. int len = strlen(this->pString);
  44. if (index<0) { return pString[0]; }
  45. else if (index>len) { return pString[index]; }
  46. else { return pString[index]; }
  47. }
  48. bool operator>(const MyString & _rValue) const {
  49. return strcmp(this->pString, _rValue.pString)>0;
  50. }
  51. bool operator<(const MyString & _rValue) const {
  52. return strcmp(this->pString, _rValue.pString)<0;
  53. }
  54. bool operator==(const MyString & _rValue) const {
  55. return strcmp(this->pString, _rValue.pString)==0;
  56. }
  57. int length() const {
  58. return strlen(this->pString);
  59. }
  60. ~MyString() {
  61. if (this->pString != nullptr) {
  62. delete[] this->pString;
  63. this->pString = nullptr;
  64. cout << "MyString析构函数" << this << endl;
  65. }
  66. }
  67. const char * c_str() const { return this->pString; }
  68. private:
  69. char * pString ;
  70. friend MyString operator+ (const MyString & s1, const MyString &s2) ;
  71. friend ostream & operator<<(ostream & out, const MyString &s) ;
  72. };
  73. MyString operator+(const MyString & s1, const MyString &s2) {
  74. /*
  75. 方式1 这段代码有 内存泄漏问题 tp 没有释放掉
  76. int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
  77. char *tp = new char[newLength + 1];//重新申请空间
  78. strcpy(tp, s1.pString);
  79. strcat(tp, s2.pString);
  80. MyString s(tp);
  81. cout << "operator+ = " << &s << endl;
  82. return s;
  83. */
  84. /*
  85. 方式2 对比方式1 效果更高
  86. */
  87. MyString s;
  88. int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
  89. s.pString = new char[newLength + 1];//重新申请空间
  90. strcpy(s.pString, s1.pString);
  91. strcat(s.pString, s2.pString);
  92. cout << "operator+ = " << &s << endl;
  93. return s;
  94. }
  95. ostream & operator<<(ostream & out, const MyString &s) {
  96. cout << s.pString << endl;
  97. return out;
  98. }
  99. void test() {
  100. MyString s1("12345");
  101. MyString s2("6789ABC");
  102. cout << s1 << endl;
  103. cout << s2 << endl;
  104. MyString s3 = s1 + s2;
  105. cout << s3 << endl;
  106. cout << "s3 = " << &s3 << endl;
  107. for (int i = 0; i < s3.length(); i++) {
  108. cout << s3[i] << endl;
  109. }
  110. cout <<"--------------------" << endl;
  111. s3[0] = 'W';
  112. for (int i = 0; i < s3.length(); i++) {
  113. cout << s3[i] << endl;
  114. }
  115. const MyString s4("hello");
  116. for (int i = 0; i < s4.length(); i++) {
  117. cout << s4[i] << endl;
  118. }
  119. }
  120. int main() {
  121. test();
  122. system("pause");
  123. return 0;
  124. }

<二>自己实现简单的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. Mysql_索引总结笔记

    Mysql 索引总结 1. 聚簇索引 InnoDB 引擎使用的就是聚簇索引,就是主键的索引,是一种数据的存储方式.所有的数据都是存储在索引的叶子结点上(与MySAM 引擎不同,MySAM是传统方式), ...

  2. 华南理工大学 Python第7章课后小测-1

    1.(单选)以下程序对字典进行排序,按字典键值从小到大排序,空白处的代码是(  ): dt={'b':6, 'c':2, 'a':4} s=sorted(dt.items(),key=_____) p ...

  3. day38-IO流05

    JavaIO流05 4.常用的类04 4.4节点流和处理流03 4.4.8打印流-PrintStream和PrintWriter 打印流只有输出流,没有输入流 1.简单介绍及应用 PrintStrea ...

  4. Linux安装Minio

    Linux安装Minio 一.安装包方式安装 1.下载minio 1.1 手动下载:https://docs.min.io/docs/minio-quickstart-guide.html ​ 访问上 ...

  5. opencv videocapture

    import time import cv2 import numpy as np from os import path import pickle ''' 关于camera id 此处需要稍微说几 ...

  6. Django 之模版层

    一.模板简介 将前端页面和Python 的代码分离是一种的开发模式. 为此 Django专门提供了模板系统 (Template System,即模板层)来实现这种模式. Django 的模板 = HT ...

  7. Shell分析日志文件

    文章转载自:https://mp.weixin.qq.com/s/o63aIM2p9rc2OjhxiC6wgA 1.查看有多少个IP访问: awk '{print $1}' log_file|sort ...

  8. Docker 容器日志管理

    Docker 日志分为两类: Docker 引擎日志(也就是 dockerd 运行时的日志), 容器的日志,容器内的服务产生的日志. 一 .Docker 引擎日志 Docker 引擎日志一般是交给了 ...

  9. 在K8S中安装jenkins

    以 NFS 为存储卷的示例,将在 NFS 存储卷上创建 Jenkins 目录,然后创建 NFS 类型的 PV.PVC. 1.NFS 存储卷创建 Jenkins 目录 进入 NFS Server 服务器 ...

  10. 关于pwd命令小技巧-确认当前工作目录的绝对路径中是否包含软链接目录名

    Linux中任何一个命令,当你用心研究到深处时,也许总能有着新的发现或者有趣的用途,如下方的pwd命令 对于pwd命令,大家都知道是用于打印当前的工作目录路径,而且是绝对路径 pwd命令两个选项的,默 ...