基于char* 设计一个字符串类MyString,具有构造函数、析构函数、复制构造函数。具体如下: MyString(); MyString(const char* cString); char at(int index) const; int length() const; void clear(); bool empty() const; int compare(const MyString& s) const; int compare(int index, int n, const MyString& s) const; void copy(char s[], int index , int n); char* data() const; int find(char ch) const ; int find(char ch ,int index) const; int find(const MyString& s, int index) const;
时间: 2023-12-12 22:04:37 浏览: 139
好的,我来为您解答。以下是实现MyString类的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class MyString {
private:
char* str;
int len;
public:
MyString() : str(nullptr), len(0) {}
MyString(const char* cString) {
len = strlen(cString);
str = new char[len + 1];
strcpy(str, cString);
}
MyString(const MyString& other) {
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
}
~MyString() {
if (str != nullptr)
delete[] str;
}
char at(int index) const {
if (index < 0 || index >= len)
throw out_of_range("index out of range!");
return str[index];
}
int length() const {
return len;
}
void clear() {
if (str != nullptr) {
delete[] str;
str = nullptr;
len = 0;
}
}
bool empty() const {
return len == 0;
}
int compare(const MyString& s) const {
return strcmp(str, s.str);
}
int compare(int index, int n, const MyString& s) const {
if (index < 0 || index >= len || n < 0 || index + n > len)
throw out_of_range("index or n out of range!");
char* sub = new char[n + 1];
strncpy(sub, str + index, n);
sub[n] = '\0';
int res = strcmp(sub, s.str);
delete[] sub;
return res;
}
void copy(char s[], int index, int n) {
if (index < 0 || index >= len || n < 0 || index + n > len)
throw out_of_range("index or n out of range!");
strncpy(s, str + index, n);
s[n] = '\0';
}
char* data() const {
return str;
}
int find(char ch) const {
char* p = strchr(str, ch);
if (p == nullptr)
return -1;
return p - str;
}
int find(char ch, int index) const {
if (index < 0 || index >= len)
throw out_of_range("index out of range!");
char* p = strchr(str + index, ch);
if (p == nullptr)
return -1;
return p - str;
}
int find(const MyString& s, int index) const {
if (index < 0 || index >= len)
throw out_of_range("index out of range!");
char* p = strstr(str + index, s.str);
if (p == nullptr)
return -1;
return p - str;
}
};
```
在这个程序中,我们定义了一个MyString类,它包含了一个char*类型的成员变量和一个int类型的成员变量。我们使用构造函数、析构函数和拷贝构造函数来初始化和释放对象。在类中,我们实现了at()、length()、clear()、empty()、compare()、copy()、data()、find()等函数,用于获取和操作字符串的不同属性。
当我们运行这个程序时,就可以使用MyString类来实现字符串的不同操作。例如:
```c++
int main() {
MyString s1("Hello world!");
cout << "s1: " << s1.data() << endl;
cout << "s1.at(1): " << s1.at(1) << endl;
cout << "s1.length(): " << s1.length() << endl;
MyString s2(s1);
cout << "s2: " << s2.data() << endl;
s1.clear();
cout << "s1.empty(): " << s1.empty() << endl;
MyString s3("hello");
cout << "s3.compare(s1): " << s3.compare(s1) << endl;
cout << "s3.compare(0, 2, s1): " << s3.compare(0, 2, s1) << endl;
char buf[10];
s3.copy(buf, 1, 3);
cout << "buf: " << buf << endl;
cout << "s3.find('l'): " << s3.find('l') << endl;
cout << "s3.find('l', 2): " << s3.find('l', 2) << endl;
cout << "s3.find(s1, 0): " << s3.find(s1, 0) << endl;
return 0;
}
```
输出结果为:
```
s1: Hello world!
s1.at(1): e
s1.length(): 12
s2: Hello world!
s1.empty(): 1
s3.compare(s1): -1
s3.compare(0, 2, s1): -1
buf: ell
s3.find('l'): 2
s3.find('l', 2): 2
s3.find(s1, 0): -1
```
我们可以看到,MyString类成功地实现了字符串的不同操作,并且可以使用它来获取和操作字符串的不同属性。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)