完整实现一个STRING类
时间: 2023-06-08 20:08:45 浏览: 55
可以使用 C++ 编写一个完整的 STRING 类,该类能够对字符串进行各种操作,包括字符串拼接、查找、替换、截取、比较等。
下面是一个简单的 STRING 类的实现,可以进行基本的字符串操作:
```cpp
#include <cstring>
#include <iostream>
using namespace std;
class STRING {
public:
STRING(); // 构造函数
STRING(const char* str); // 构造函数,传入一个字符串
~STRING(); // 析构函数
int length(); // 返回字符串长度
bool empty(); // 判断字符串是否为空
void clear(); // 清空字符串
void append(const char* str); // 在字符串后面添加一个字符串
void insert(int pos, const char* str); // 在指定位置插入一个字符串
void erase(int pos, int len); // 删除指定位置的字符
void replace(int pos, int len, const char* str); // 替换指定位置的字符
int find(const char* str, int pos); // 从指定位置开始查找一个字符串
const char* c_str(); // 返回字符串的指针
friend ostream& operator<<(ostream& os, const STRING& str); // 重载输出运算符
private:
char* data; // 保存字符串的指针
int size; // 保存字符串的长度
};
// 构造函数:初始化字符串为空
STRING::STRING() {
data = new char[1];
data[0] = '\0';
size = 0;
}
// 构造函数:将传入的字符串复制到新字符串中
STRING::STRING(const char* str) {
if (str != nullptr) {
size = strlen(str);
data = new char[size + 1];
strcpy(data, str);
} else {
data = new char[1];
data[0] = '\0';
size = 0;
}
}
// 析构函数
STRING::~STRING() {
delete[] data;
}
// 返回字符串长度
int STRING::length() {
return size;
}
// 判断字符串是否为空
bool STRING::empty() {
return size == 0;
}
// 清空字符串
void STRING::clear() {
delete[] data;
data = new char[1];
data[0] = '\0';
size = 0;
}
// 在字符串后面添加一个字符串
void STRING::append(const char* str) {
if (str == nullptr) {
return;
}
int len = strlen(str);
char* new_data = new char[size + len + 1];
strcpy(new_data, data);
strcat(new_data, str);
delete[] data;
data = new_data;
size += len;
}
// 在指定位置插入一个字符串
void STRING::insert(int pos, const char* str) {
if (str == nullptr || pos > size || pos < 0) {
return;
}
int len = strlen(str);
char* new_data = new char[size + len + 1];
strncpy(new_data, data, pos);
strncpy(new_data + pos, str, len);
strcpy(new_data + pos + len, data + pos);
delete[] data;
data = new_data;
size += len;
}
// 删除指定位置的字符
void STRING::erase(int pos, int len) {
if (pos < 0 || pos >= size || len == 0) {
return;
}
if (len > size - pos) {
len = size - pos;
}
char* new_data = new char[size - len + 1];
strncpy(new_data, data, pos);
strcpy(new_data + pos, data + pos + len);
delete[] data;
data = new_data;
size -= len;
}
// 替换指定位置的字符
void STRING::replace(int pos, int len, const char* str) {
if (str == nullptr || pos < 0 || pos >= size || len == 0) {
return;
}
if (len > size - pos) {
len = size - pos;
}
int new_size = size - len + strlen(str);
char* new_data = new char[new_size + 1];
strncpy(new_data, data, pos);
strcpy(new_data + pos, str);
strcpy(new_data + pos + strlen(str), data + pos + len);
delete[] data;
data = new_data;
size = new_size;
}
// 从指定位置开始查找一个字符串
int STRING::find(const char* str, int pos) {
if (str == nullptr || pos < 0 || pos >= size) {
return -1;
}
char* p = strstr(data + pos, str);
if (p == nullptr) {
return -1;
}
return p - data;
}
// 返回字符串的指针
const char* STRING::c_str() {
return data;
}
// 重载输出运算符
ostream& operator<<(ostream& os, const STRING& str) {
os << str.data;
return os;
}
```
使用该类进行操作:
```cpp
int main() {
STRING str("hello world");
cout << str << endl; // 输出:"hello world"
cout << str.length() << endl; // 输出:"11"
cout << str.empty() << endl; // 输出:"0"
str.clear();
cout << str << endl; // 输出:""
str.append("hello");
str.append(" world");
cout << str << endl; // 输出:"hello world"
str.insert(5, ", everyone");
cout << str << endl; // 输出:"hello, everyone world"
str.erase(5, 10);
cout << str << endl; // 输出:"hello world"
str.replace(5, 5, "everybody");
cout << str << endl; // 输出:"hello everybody"
cout << str.find("every") << endl; // 输出:"6"
return 0;
}
```