1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <bits/stdc++.h>
class String { public: String(const char *str = nullptr); String(const String &other); String(String &&other); ~String();
String &operator=(const String &other); String &operator=(String &&other);
private: char *m_data; };
String::String(const char *str) { if (str == nullptr) { m_data = new char[1]; *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length + 1]; strcpy(m_data, str); } }
String::String(const String &other) { int length = strlen(other.m_data); m_data = new char[length + 1]; strcpy(m_data, other.m_data); }
String::String(String &&other) { m_data = other.m_data; other.m_data = nullptr; }
String::~String() { delete[] m_data; }
String &String::operator=(const String &other) { if (this != &other) { delete[] m_data; int length = strlen(other.m_data); m_data = new char[length + 1]; strcpy(m_data, other.m_data); } return *this; }
String &String::operator=(String &&other) { if (this != &other) { delete[] m_data; m_data = other.m_data; other.m_data = nullptr; } return *this; }
int main() { String s1; String s2("Hello"); String s3(s2);
String s4(std::move(s3)); String s5; s5 = s4;
String s6; s6 = std::move(s5);
system("pause"); return 0; }
|
strlen和sizeof的区别
int length = strlen(str); 计算了 str 指向的字符串的长度,并将结果存储在 length 变量中。
sizeof 获取的是指针的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include <bits/stdc++.h>
class String { public: String(const char *str = nullptr); String(const String &other); String(String &&other); ~String();
String &operator=(const String &other); String &operator=(String &&other);
public: char *m_data; int m_length; };
String::String(const char *str) { if (str == nullptr) { m_data = nullptr; m_length = 0; } else { int length = strlen(str); m_data = new char[length]; strcpy(m_data, str); m_length = length; } }
String::String(const String &other) { int length = strlen(other.m_data); m_data = new char[length]; strcpy(m_data, other.m_data); m_length = length; }
String::String(String &&other) { m_data = other.m_data; m_length = other.m_length; other.m_data = nullptr; }
String::~String() { delete[] m_data; }
String &String::operator=(const String &other) { if (this != &other) { delete[] m_data; int length = strlen(other.m_data); m_data = new char[length]; strcpy(m_data, other.m_data); m_length = length; } return *this; }
String &String::operator=(String &&other) { if (this != &other) { delete[] m_data; m_data = other.m_data; other.m_data = nullptr; m_length = other.m_length; } return *this; }
int main() { String s1; String s2("Hello");
for (int i = 0; i < s2.m_length; i++) { std::cout << s2.m_data[i]; } std::cout << std::endl;
String s3(s2);
String s4(std::move(s3)); String s5; s5 = s4;
String s6; s6 = std::move(s5);
system("pause"); return 0; }
|