String类

举栗子,通过讲解String类,了解C++类的设计

1. 构造函数

  • string(); //无参构造
  • string(const char*);
  • string(const string& that); //拷贝构造

    2. 赋值

  • string& operator=(const string& s); //把字符串s赋值给当前字符串

  • string& operator=(const char* s); //用字符串赋值

    3. 容量

  • int size()const; //当前字符串的长度

  • bool empty() const; //测试是否为空字符串
  • int max_size() const; //可以存放的最大字符个数

    4. 元素访问

  • char& operator; //返回下标为n的字符引用,不执行边界检查

  • char& at(int n); //返回下标为n的字符引用,执行边界检查
  • const char* c_str()const; /返回指向内部字符数组的const指针
  • string substr(int pos = 0, int n = npos)const; //返回pos开始的n个字符组成的子串,string::npos是一个很大的常数,用来表示不存在的位置

    5. 字符串操作

    • string& insert(int pos, const string& s); //在pos位置插入字符串s
    • void clear(); //移除string的所有字符。
    • string& erase(int pos = 0, int n = npos); //删除pos开始的n个字符,返回修改后的字符串。
    • string& operator+=(const string& s); //把字符串s连接到当前字符串的结尾
    • string& replace(int start, int n, const char* s); //将从start开始的n个字符用字符串s替换
    • void resize(int len, char c); //将当前字符串大小设置为len,空白部分用字符串
    • int copy(char* s, int n, int pos = 0) const; //把当前字符串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
    • int find(const char* s, int pos = 0) const; //从pos开始搜索字符串s,返回找到的首个字符串s的首字符位置,或若找不到这种字符串则为string::npos

      6. 全局函数

  • string operator+(const string& s1, const string& s2); //连接s1和s2

  • bool operator==(const string& s1, const string& s2)const; //比较两个字符串是否相等
  • bool operator!=(const string& s1, const string& s2)const; //比较两个字符串是否相等
  • bool operator>(const string& s1, const string& s2)const; //比较两个字符串大小
  • bool operator<(const string& s1, const string& s2)const; //比较两个字符串大小
  • int stoi(const string& str, int* pos = 0, int base = 10); //pos用来保存转换后的字符的个数
  • double stod(const string& str, double* pos = 0);
  • string to_string(int value);
  • string to_string(double value);

    7. 示例代码:

    ```cpp

    include

    using namespace std;

int main(){ string str; string str1(“hello”); string str2(str1);

  1. str = str1;
  2. str = "world";
  3. cout << "str : " << str << endl;
  4. cout << "str1 : " << str1 << endl;
  5. cout << "str2 : " << str2<< endl;
  6. cout << "size : " << str.size() << endl;
  7. cout << "empty : " << str.empty() << endl;
  8. cout << "max_size : " << str.max_size() << endl;
  9. cout << "[] : " << str[3] << endl;
  10. cout << "at : " << str.at(3) << endl;
  11. cout << "[] : " << str[7] << endl;
  12. //cout << "at : " << str.at(7) << endl;
  13. cout << "c_str : " << str.c_str() << endl;
  14. cout << "c_str : " << (void*)str.c_str() << endl;
  15. cout << "substr : " << str.substr(1, 3) << endl;
  16. cout << "substr : " << str.substr(1) << endl;
  17. cout << "insert : " << str.insert(1, string("abc")) << endl;
  18. //str.clear();
  19. cout << "str : " << str << endl;
  20. str.resize(10, 'x');
  21. cout << "str : " << str << endl;
  22. char strArr[4] = {0};
  23. str.copy(strArr, 4, 3);
  24. cout << "copy : " << strArr << endl;
  25. int n;
  26. n = stoi(string("123"));
  27. cout << "n = " << n << endl;
  28. n = stoi(string("123"), 0, 16);
  29. cout << "n = " << hex << n << endl;
  30. str = to_string(123);
  31. cout << "str : " << str << endl;
  32. return 0;

}

  1. <a name="qVTke"></a>
  2. ## 8. 手写String类
  3. <a name="JWMHs"></a>
  4. ### - main.cpp
  5. ```cpp
  6. #include "mystring.h"
  7. int main(int argc, const char *argv[]){
  8. MyString str1("Hu");
  9. cout << str1 << endl;
  10. cout << str1.size() << endl;
  11. cout << str1[1] << endl;
  12. MyString str2 = str1;
  13. cout << str2 << endl;
  14. MyString str3("Mingdong");
  15. cout << str3 << endl;
  16. cout << str3.size() << endl;
  17. cout << str3[1] << endl;
  18. cout << "---------------" << endl;
  19. cout << str1 + str3 << endl;
  20. MyString newStr;
  21. cin >> newStr;
  22. cout << newStr << endl;
  23. cout << newStr.size() << endl;
  24. return 0;
  25. }

- mystring.h

  1. #ifndef __MYSTRING_H__
  2. #define __MYSTRING_H__
  3. using namespace std;
  4. #include <iostream>
  5. #include <cstring>
  6. class MyString {
  7. private:
  8. char* mstr;
  9. public:
  10. MyString(); //无参构造
  11. MyString(const char* str); //有参构造
  12. MyString(const MyString& that); //拷贝构造
  13. ~MyString(); //析构
  14. int size(); //计算字符串的字符个数
  15. void clear(); //清空字符串的字符
  16. MyString& operator=(const MyString& other); //字符串赋值运算
  17. MyString& operator+=(const MyString& other); //字符串+=运算
  18. char operator[](unsigned int n); //字符串下标运算
  19. //友元函数
  20. friend ostream& operator<<(ostream& out, const MyString& string); //字符串输出
  21. friend istream& operator>>(istream& in, MyString& string); //字符串输入
  22. friend MyString operator+(const MyString& string1, const MyString& string2); //字符串+运算
  23. friend bool operator==(const MyString& string1, const MyString& string2); //字符串==运算
  24. friend bool operator!=(const MyString& string1, const MyString& string2); //字符串!=运算
  25. };
  26. #endif

- mystring.cpp

  1. #include "mystring.h"
  2. MyString::MyString() //无参构造
  3. {
  4. mstr = new char[1];
  5. mstr[0] = '\0';
  6. }
  7. MyString::MyString(const char* str) //有参构造
  8. {
  9. if(str == NULL)
  10. {
  11. mstr = new char[1];
  12. mstr[0] = '\0';
  13. }
  14. else
  15. {
  16. int len = strlen(str);
  17. mstr = new char[len + 1];
  18. strcpy(mstr, str);
  19. }
  20. }
  21. MyString::MyString(const MyString& that) //拷贝构造
  22. {
  23. int len = strlen(that.mstr);
  24. mstr = new char[len + 1];
  25. strcpy(mstr, that.mstr);
  26. }
  27. MyString::~MyString() //析构
  28. {
  29. delete[] mstr;
  30. }
  31. int MyString::size() //计算字符串的字符个数
  32. {
  33. return strlen(mstr);
  34. }
  35. void MyString::clear() //清空字符串的字符
  36. {
  37. int len = strlen(mstr);
  38. if(len == 0)
  39. {
  40. return;
  41. }
  42. else
  43. {
  44. for(int i = 0; i < len; i++)
  45. {
  46. mstr[i] = '\0';
  47. }
  48. return;
  49. }
  50. }
  51. MyString& MyString::operator=(const MyString& other) //字符串赋值运算
  52. {
  53. if(this == &other)
  54. {
  55. return *this;
  56. }
  57. else
  58. {
  59. delete[] mstr;
  60. int len = strlen(other.mstr);
  61. mstr = new char[len + 1];
  62. strcpy(mstr, other.mstr);
  63. return *this;
  64. }
  65. }
  66. MyString& MyString::operator+=(const MyString& other) //字符串+=运算
  67. {
  68. MyString old = *this;
  69. delete[] mstr;
  70. int len = strlen(old.mstr) + strlen(other.mstr);
  71. mstr = new char[len + 1];
  72. strcpy(mstr, old.mstr);
  73. strcat(mstr, other.mstr);
  74. return *this;
  75. }
  76. char MyString::operator[](unsigned int n) //字符串下标运算
  77. {
  78. return mstr[n];
  79. }
  80. //友元函数的定义实现
  81. ostream& operator<<(ostream& out, const MyString& string) //字符串输出
  82. {
  83. out << string.mstr;
  84. return out;
  85. }
  86. istream& operator>>(istream& in, MyString& string) //字符串输入
  87. {
  88. in >> string.mstr;
  89. return in;
  90. }
  91. MyString operator+(const MyString& string1, const MyString& string2) //字符串+运算
  92. {
  93. MyString string = string1;
  94. string += string2;
  95. return string;
  96. }
  97. bool operator==(const MyString& string1, const MyString& string2) //字符串==运算
  98. {
  99. return !strcmp(string1.mstr, string2.mstr);
  100. }
  101. bool operator!=(const MyString& string1, const MyString& string2) //字符串!=运算
  102. {
  103. return strcmp(string1.mstr, string2.mstr);
  104. }