模拟实现string类,重载运算符“+”、“-”、“+=”、“==”、“!=”、“<”、“<=”、“>”、“>=”、“[]”、“>>”、“<<”,实现两个字符串的赋值、连接、相等的判断、不等的判断、大小比较、访问、输入和输出运算。
时间: 2023-11-05 15:08:07 浏览: 83
以下是一个简单的模拟实现string类的代码:
```c++
#include <iostream>
#include <cstring>
class String {
public:
// 构造函数
String() : m_data(nullptr), m_size(0) {}
String(const char* str) : m_data(nullptr), m_size(0) {
if (str) {
m_size = std::strlen(str);
m_data = new char[m_size + 1];
std::strcpy(m_data, str);
}
}
String(const String& other) : m_data(nullptr), m_size(other.m_size) {
if (other.m_data) {
m_data = new char[m_size + 1];
std::strcpy(m_data, other.m_data);
}
}
// 析构函数
~String() {
if (m_data) {
delete[] m_data;
m_data = nullptr;
}
}
// 重载运算符
String operator+(const String& other) const {
String newString;
newString.m_size = m_size + other.m_size;
newString.m_data = new char[newString.m_size + 1];
std::strcpy(newString.m_data, m_data);
std::strcat(newString.m_data, other.m_data);
return newString;
}
String operator-(const String& other) const {
String newString;
newString.m_size = m_size - other.m_size;
newString.m_data = new char[newString.m_size + 1];
std::strcpy(newString.m_data, m_data);
std::strtok(newString.m_data, other.m_data);
return newString;
}
String& operator+=(const String& other) {
m_size += other.m_size;
char* newData = new char[m_size + 1];
std::strcpy(newData, m_data);
std::strcat(newData, other.m_data);
delete[] m_data;
m_data = newData;
return *this;
}
bool operator==(const String& other) const {
return std::strcmp(m_data, other.m_data) == 0;
}
bool operator!=(const String& other) const {
return std::strcmp(m_data, other.m_data) != 0;
}
bool operator<(const String& other) const {
return std::strcmp(m_data, other.m_data) < 0;
}
bool operator<=(const String& other) const {
return std::strcmp(m_data, other.m_data) <= 0;
}
bool operator>(const String& other) const {
return std::strcmp(m_data, other.m_data) > 0;
}
bool operator>=(const String& other) const {
return std::strcmp(m_data, other.m_data) >= 0;
}
char operator[](int index) const {
return m_data[index];
}
char& operator[](int index) {
return m_data[index];
}
friend std::ostream& operator<<(std::ostream& os, const String& str);
friend std::istream& operator>>(std::istream& is, String& str);
private:
char* m_data;
int m_size;
};
std::ostream& operator<<(std::ostream& os, const String& str) {
os << str.m_data;
return os;
}
std::istream& operator>>(std::istream& is, String& str) {
char buffer[1024];
is >> buffer;
str = buffer;
return is;
}
int main() {
String str1("Hello");
String str2("World");
String str3 = str1 + str2;
std::cout << str3 << std::endl;
str3 += "!";
std::cout << str3 << std::endl;
std::cout << (str1 == str2) << std::endl;
std::cout << (str1 != str2) << std::endl;
std::cout << (str1 < str2) << std::endl;
std::cout << (str1 <= str2) << std::endl;
std::cout << (str1 > str2) << std::endl;
std::cout << (str1 >= str2) << std::endl;
std::cout << str1[0] << std::endl;
std::cout << str2[1] << std::endl;
String str4;
std::cin >> str4;
std::cout << str4 << std::endl;
return 0;
}
```
注意,这只是一个简单的实现,还有很多细节问题需要考虑,比如内存泄漏、异常处理、Unicode编码等等。
阅读全文