• day 0912


    1. #include
    2. #include
    3. using namespace std;
    4. class myString
    5. {
    6. private:
    7. char *str; //记录c风格的字符串
    8. int size; //记录字符串的实际长度
    9. public:
    10. //无参构造
    11. myString():size(10)
    12. {
    13. str = new char[size]; //构造一个长度为10的字符串
    14. strcpy(str," "); //赋值为空串
    15. }
    16. //有参构造
    17. myString(const char *s)
    18. {
    19. size = strlen(s);
    20. str =new char[size+1];
    21. strcpy(str,s);
    22. }
    23. //拷贝构造
    24. myString(const myString &other)
    25. {
    26. size = strlen(other.str);
    27. str =new char[size+1];
    28. strcpy(str,other.str);
    29. }
    30. //析构函数
    31. ~myString ()
    32. {
    33. delete str;
    34. str = nullptr;
    35. }
    36. //拷贝赋值函数
    37. myString & operator=(const myString &other)
    38. {
    39. if(this != &other)
    40. {
    41. if(this->str != NULL)
    42. {
    43. delete str;
    44. }
    45. this->str = new char[strlen(other.str)+1];
    46. strcpy(str,other.str);
    47. }
    48. return *this;
    49. }
    50. //判空函数
    51. bool str_empty()
    52. {
    53. return str == nullptr;
    54. }
    55. //size函数
    56. int str_size()
    57. {
    58. return size;
    59. }
    60. //c_str函数
    61. char *c_str()
    62. {
    63. char *c_string = new char[size +1];
    64. strcpy(c_string, str);
    65. return c_string;
    66. }
    67. //at函数
    68. char &at(int pos)
    69. {
    70. if(pos<0||pos>size-1)
    71. return*str;
    72. return *(str+pos);
    73. }
    74. //加号运算符重载
    75. const myString operator+(const myString &s)
    76. {
    77. myString tmp;
    78. tmp.size = size + s.size;
    79. tmp.str = new char[tmp.size+1];
    80. strcpy(tmp.str,str);
    81. char *dstr = tmp.str + size;
    82. strcpy(dstr, s.str);
    83. dstr = nullptr;
    84. return tmp;
    85. }
    86. //加等于运算符重载
    87. const myString operator+=(const myString &s)
    88. {
    89. myString tmp(*this);
    90. size = tmp.size + s.size;
    91. delete str;
    92. str = new char[size+1];
    93. strcpy(str,tmp.str);
    94. char *dstr = str + tmp.size;
    95. strcpy(dstr, s.str);
    96. dstr = nullptr;
    97. return *this;
    98. }
    99. //关系运算符重载(>)
    100. bool operator >(const myString &s)
    101. {
    102. int i=0;
    103. for(;(*(str+i)==*(s.str+i) && *(str+i)!=0 && *(s.str+i)!=0);i++);
    104. return *(str+i)-*(s.str+i)>0;
    105. }
    106. //中括号运算符重载
    107. char &operator[](int pos)
    108. {
    109. if(pos<0||pos>size-1)
    110. return *str;
    111. return *(str+pos);
    112. }
    113. //定义成员函数
    114. void show()
    115. {
    116. cout<
    117. }
    118. };
    119. int main()
    120. {
    121. myString s1("hello ");
    122. myString s2("world");
    123. myString s3;
    124. s3 = s1+s2;
    125. s3.show();
    126. myString s4;
    127. s4 = s3;
    128. s4.show();
    129. s1+=s2;
    130. s1.show();
    131. s1[0]='H';
    132. s1.show();
    133. s1.at(6)='W';
    134. s1.show();
    135. return 0;
    136. }

  • 相关阅读:
    android设计模式-builder模式
    开源项目管理工具Helper的安装及汉化
    设计模式之中介者模式
    【LeetCode】55. 跳跃游戏
    Java Native尝试
    《思科 - GNS3 - Pythonping》
    Leetcode43字符串相乘
    元宇宙,小荷才露尖尖角
    使用grid来根据屏幕宽度计算每行可以放下多少个div盒子
    TSINGSEE青犀基于AI视频智能分析的客流统计分析场景方案
  • 原文地址:https://blog.csdn.net/m0_73743794/article/details/132821497