String系列——运算符重载

曾经终败给现在 2022-08-03 04:26 198阅读 0赞

本节实现所有运算符的重载。意图很简单,对于运算符重载不清楚的可以查看相关运算符重载的写法。
操作符重载很多,可以选择看。

定义

  1. #pragma once
  2. class MString{
  3. public:
  4. //构造函数
  5. MString():m_len(0),m_str(0){}
  6. MString(const MString& s);
  7. MString(const char* s);
  8. MString(size_t n);
  9. ~MString();
  10. //赋值操作符,实现对象赋值
  11. MString& operator=(const MString& s);
  12. //赋值操作符,实现字符串赋值
  13. MString& operator=(const char* s);
  14. //转换成c类型字符串
  15. const char* c_str(){ return m_str; }
  16. //判断字符是否为空
  17. bool empty(){ return m_len>0? true:false; }
  18. //返回字符串长度
  19. size_t length(){ return m_len; }
  20. //返回字符串大小
  21. size_t size(){
  22. return m_len;}
  23. //删除pos开始的n个字符,不能为左值
  24. const MString& erase(size_t pos, size_t n);
  25. //对象末尾添加字符串s
  26. MString& append(const MString& s);
  27. //返回从pos开始的n个字符
  28. MString substring(size_t pos,size_t n);
  29. //从位置pos开始插入字符串s
  30. MString& insert(size_t pos,const MString& s);
  31. MString& insert(size_t pos,const char* s);
  32. //把字符串s中从pos开始的n个字符返回赋给当前字符串
  33. MString& assign(const MString& s, size_t,size_t);
  34. MString& assign(const char* s, size_t,size_t);
  35. //-------操作符重载-----------------------------------------
  36. /*---很多参数为MString对象的都可以再重载一个char*类型的方法--------------*/
  37. //比较操作符
  38. bool operator==(const MString& s2){ return strcmp(m_str,s2.m_str) == 0; }
  39. bool operator>=(const MString& s2){ return strcmp(m_str,s2.m_str) >= 0; }
  40. bool operator<=(const MString& s2){ return strcmp(m_str,s2.m_str) <= 0; }
  41. bool operator>(const MString& s2) { return strcmp(m_str,s2.m_str) > 0; }
  42. bool operator<(const MString& s2) { return strcmp(m_str,s2.m_str) < 0; }
  43. bool operator!=(const MString& s2){ return strcmp(m_str,s2.m_str) != 0; }
  44. //操作符,取地址
  45. MString& operator&();
  46. //操作符,取值
  47. MString& operator*();
  48. //操作符 ,->
  49. MString* operator->();
  50. //自增,自减
  51. MString& operator++(); //前缀
  52. MString& operator++(int); //后缀
  53. MString& operator--(); //前缀
  54. MString& operator--(int); //后缀
  55. //转换操作符,一般一个类只有一个转换操作符。
  56. //operator char*()const;
  57. //调用操作符
  58. int operator() (MString& s1, MString& s2);
  59. //操作符s[n],允许作为左值
  60. char& operator[](size_t n);
  61. //操作符s[n],不允许作为左值
  62. const char& operator[](const size_t n) const;
  63. //重载 s1 += s;
  64. MString& operator+=(const MString& s);
  65. //重载 s1 += s;
  66. MString& operator+=(const char* s);
  67. //重载 s = s1+s2 改变被加对象
  68. MString& operator+(const MString& s);
  69. //重载 s = s1+s2 不改变被加对象
  70. friend MString operator+(const MString& s1, const MString& s2);
  71. //重载输入输出
  72. friend ostream& operator<<(ostream& out,const MString& str);
  73. friend istream& operator>>(istream& in,MString& str);
  74. private:
  75. size_t m_len;
  76. char* m_str;
  77. };

实现

  1. //------操作符重载-----------------------------------------------
  2. //操作符,取地址
  3. MString& MString::operator&()
  4. {
  5. return *this;
  6. }
  7. //操作符,取值
  8. MString& MString::operator*()
  9. {
  10. return *this;
  11. }
  12. //操作符 ,->
  13. MString* MString::operator->()
  14. {
  15. return this;
  16. }
  17. //自增,自减
  18. MString& MString::operator++() //前缀
  19. {
  20. ++m_str;
  21. return *this;
  22. }
  23. MString& MString::operator++(int)//后缀
  24. {
  25. MString* ret = new MString(*this);
  26. ++*this;
  27. return *ret;
  28. }
  29. MString& MString::operator--() //前缀
  30. {
  31. --m_str;
  32. return *this;
  33. }
  34. MString& MString::operator--(int) //后缀
  35. {
  36. MString* ret = new MString(*this);
  37. ++*this;
  38. return *ret;
  39. }
  40. //转换操作符,一般一个类只有一个转换操作符。不能同时和[]重载运算符同时出现,会歧义。
  41. //MString::operator char*()const
  42. //{
  43. // return m_str;
  44. //}
  45. //调用操作符,在STL叫做适配器,很少使用。
  46. int MString::operator() (MString& s1, MString& s2)
  47. {
  48. return strcmp(s1->m_str,s2->m_str);
  49. }
  50. //操作符s[n],允许作为左值
  51. char& MString::operator[](size_t n)
  52. {
  53. assert(n>=0&&n<=m_len);
  54. return m_str[n];
  55. }
  56. //操作符s[n],不允许作为左值
  57. const char& MString::operator[](const size_t n) const
  58. {
  59. assert(n>=0&&n<=m_len);
  60. return m_str[n];
  61. }
  62. //重载 s1 += s;
  63. MString& MString::operator+=(const MString& s)
  64. {
  65. if (!s.m_len){
  66. return *this;
  67. }
  68. m_len = m_len + s.m_len;
  69. char* tmp_str = new char[m_len +1];
  70. strcpy(tmp_str,this->m_str);
  71. strcat(tmp_str,s.m_str);
  72. m_str[m_len] = '\0';
  73. if (m_str != NULL)
  74. {
  75. delete[] m_str;
  76. }
  77. m_str = tmp_str;
  78. return *this;
  79. }
  80. //重载 s1 += s;
  81. MString& MString::operator+=(const char* s)
  82. {
  83. if (strlen(s) < 1){
  84. return *this;
  85. }
  86. m_len = m_len + strlen(s);
  87. char* tmp_str = new char[m_len +1];
  88. strcpy(tmp_str,this->m_str);
  89. strcat(tmp_str,s);
  90. tmp_str[m_len] = '\0';
  91. if (m_str != NULL)
  92. {
  93. delete[] m_str;
  94. }
  95. m_str = tmp_str;
  96. return *this;
  97. }
  98. //重载 s = s1+s2 改变被加对象
  99. MString& MString::operator+(const MString& s)
  100. {
  101. if (s.m_len < 1)
  102. {
  103. return *this;
  104. }
  105. char* tmp_str;
  106. m_len += s.m_len;
  107. tmp_str = new char[m_len+1];
  108. strcpy(tmp_str, m_str);
  109. strcat(tmp_str, s.m_str);
  110. tmp_str[m_len] = '\0';
  111. if (m_str != NULL)
  112. {
  113. delete[] m_str;
  114. }
  115. m_str = tmp_str;
  116. return *this;
  117. }
  118. //重载 s = s1+s2 不改变被加对象
  119. MString operator+(const MString& s1, const MString& s2)
  120. {
  121. MString* tmp = new MString(s1.m_len+s2.m_len);
  122. strcpy(tmp->m_str, s1.m_str);
  123. strcat(tmp->m_str, s2.m_str);
  124. tmp->m_str[tmp->m_len] = '\0';
  125. return *tmp;
  126. }
  127. //重载输入输出
  128. ostream& operator<<(ostream& out,const MString& str)
  129. {
  130. out << str.m_str;
  131. return out;
  132. }
  133. istream& operator>>(istream& in,MString& str)
  134. {
  135. //遇到空格,就代表输入终止
  136. char* buf = new char[256];
  137. if(in >> buf){
  138. delete[] str.m_str;
  139. str.m_len = strlen(buf);
  140. str.m_str = new char[str.m_len+1];
  141. strcpy(str.m_str, buf);
  142. }
  143. return in;
  144. }

本节是上一节的延续,代码都经过测试。

发表评论

表情:
评论列表 (有 0 条评论,198人围观)

还没有评论,来说两句吧...

相关阅读

    相关 运算符重载

    一、运算符重载。 什么是运算符重载? 运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 二、加号运算符重载。 作用: 实现两个自定

    相关 运算符重载

    实现函数运算符的实质:编写一个函数,该函数以“operator运算符号”为函数名,其定义了重载的运算符将要执行的操作。(给自己的类进行运算) 运算符重载有两种形式:重载为类的

    相关 运算符重载

    c++的一大特性就是重载(overload),通过重载可以把功能相似的几个函数合为一个,使得程序更加简洁、高效。在c++中不止函数可以重载,运算符也可以重载。由于一般数据类型间