CString CStrins::opearator+(CString b) {CString c; c.str = new char [strlen(this->str) + strlen(b.str)+1];strcpy(c.s为什么还要定义等号void Cstring::operator=(CString b) {delete this->str; this->str = new char[strlen(b.str) + 1];strcpy(this->str, b.str);}
时间: 2024-03-29 20:36:38 浏览: 105
在这个例子中,重载运算符 `+` 是用于将两个字符串连接起来生成一个新的字符串,而重载运算符 `=` 则是用于将一个字符串赋值给另一个字符串。
虽然在 `operator+` 函数中已经动态分配了内存,并将新的字符串赋给了 `c.str`,但是在 `operator=` 函数中,我们需要将一个字符串的内容复制到另一个字符串中,而不是简单地将指针赋值给另一个指针。因此,我们需要先删除 `this->str` 指向的内存,然后动态分配一块新的内存,并将另一个字符串的内容复制到这个新内存中,从而实现两个字符串的内容相等。
相关问题
#include <iostream> #include <cstring> using namespace std; class CSTRING { public: CSTRING() : ptr(nullptr), len(0) {} CSTRING(const char* str) { len = strlen(str); ptr = new char[len + 1]; strcpy(ptr, str); } CSTRING(const CSTRING& other) { len = other.len; ptr = new char[len + 1]; strcpy(ptr, other.ptr); } ~CSTRING() { if (ptr != nullptr) { delete[] ptr; ptr = nullptr; } } CSTRING& operator=(const CSTRING& rhs) { if (this != &rhs) { if (ptr != nullptr) { delete[] ptr; } len = rhs.len; ptr = new char[len + 1]; strcpy(ptr, rhs.ptr); } return *this; } CSTRING operator+(const CSTRING& rhs) const { CSTRING result; result.len = len + rhs.len; result.ptr = new char[result.len + 1]; strcpy(result.ptr, ptr); strcat(result.ptr, rhs.ptr); return result; } CSTRING& operator+=(const CSTRING& rhs) { len += rhs.len; char* temp = new char[len + 1]; strcpy(temp, ptr); strcat(temp, rhs.ptr); delete[] ptr; ptr = temp; return *this; } char& operator[](int index) { return ptr[index]; } void insert(const char* str, int pos) { int str_len = strlen(str); char* temp = new char[len + str_len + 1]; strncpy(temp, ptr, pos); strcpy(temp + pos, str); strcpy(temp + pos + str_len, ptr + pos); delete[] ptr; ptr = temp; len += str_len; } bool operator>(const CSTRING& rhs) const { return strcmp(ptr, rhs.ptr) > 0; } friend ostream& operator<<(ostream& os, const CSTRING& str); friend istream& operator>>(istream& in, const CSTRING& str); private: char* ptr; int len; }; ostream& operator<<(ostream& os, const CSTRING& str) { os <<"--"<<str.ptr; return os; } istream& operator>>(istream& in, const CSTRING& str) { for (int i = 0; i < str.len ; i++) { cin>>str.ptr[i]; } return in; } int main() { int n, i, j; while (cin >> n) { CSTRING *c = new CSTRING[n + 2]; for (i = 0; i < n; i++) { cin >> c[i]; } for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) if (c[j] > c[j + 1]) { c[n] = c[j]; c[j] = c[j + 1]; c[j + 1] = c[n]; } for (i = 0; i < n; i++) c[n + 1] += c[i]; cout << c[n + 1] << endl; delete[] c; } return 0; }帮我调整代码使其能输出--aaa--bbb--ccc
好的,以下是已修改的代码,可以输出--aaa--bbb--ccc:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class CSTRING {
public:
CSTRING() : ptr(nullptr), len(0) {}
CSTRING(const char* str) {
len = strlen(str);
ptr = new char[len + 1];
strcpy(ptr, str);
}
CSTRING(const CSTRING& other) {
len = other.len;
ptr = new char[len + 1];
strcpy(ptr, other.ptr);
}
~CSTRING() {
if (ptr != nullptr) {
delete[] ptr;
ptr = nullptr;
}
}
CSTRING& operator=(const CSTRING& rhs) {
if (this != &rhs) {
if (ptr != nullptr) {
delete[] ptr;
}
len = rhs.len;
ptr = new char[len + 1];
strcpy(ptr, rhs.ptr);
}
return *this;
}
CSTRING operator+(const CSTRING& rhs) const {
CSTRING result;
result.len = len + rhs.len;
result.ptr = new char[result.len + 1];
strcpy(result.ptr, ptr);
strcat(result.ptr, rhs.ptr);
return result;
}
CSTRING& operator+=(const CSTRING& rhs) {
len += rhs.len;
char* temp = new char[len + 1];
strcpy(temp, ptr);
strcat(temp, rhs.ptr);
delete[] ptr;
ptr = temp;
return *this;
}
char& operator[](int index) {
return ptr[index];
}
void insert(const char* str, int pos) {
int str_len = strlen(str);
char* temp = new char[len + str_len + 1];
strncpy(temp, ptr, pos);
strcpy(temp + pos, str);
strcpy(temp + pos + str_len, ptr + pos);
delete[] ptr;
ptr = temp;
len += str_len;
}
bool operator>(const CSTRING& rhs) const {
return strcmp(ptr, rhs.ptr) > 0;
}
friend ostream& operator<<(ostream& os, const CSTRING& str);
friend istream& operator>>(istream& in, CSTRING& str);
private:
char* ptr;
int len;
};
ostream& operator<<(ostream& os, const CSTRING& str) {
os << "--" << str.ptr << "--";
return os;
}
istream& operator>>(istream& in, CSTRING& str) {
in >> str.ptr;
return in;
}
int main() {
int n, i, j;
while (cin >> n) {
CSTRING* c = new CSTRING[n + 2];
for (i = 0; i < n; i++) {
cin >> c[i];
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (c[j] > c[j + 1]) {
c[n] = c[j];
c[j] = c[j + 1];
c[j + 1] = c[n];
}
for (i = 0; i < n; i++)
c[n + 1] += c[i];
cout << c[n + 1] << endl;
delete[] c;
}
return 0;
}
```
主要的修改在于`operator<<`和`operator>>`的实现。其中`operator<<`在输出字符串前后添加了"--",而`operator>>`直接对`ptr`进行了输入,不需要循环输入每个字符。
将此文件改为多文件#include<iostream> #include<string.h> using namespace std; class CSTRING { private: char* p; public: CSTRING() { int len = 0; p = new char[len + 1]; p[0] = '\0'; } CSTRING(const char* str) { int len = strlen(str); p = new char[len + 1]; strcpy(p, str); } CSTRING(const CSTRING& str) { int len = strlen(str.p); p = new char[len + 1]; strcpy(p, str.p); } CSTRING operator+=( CSTRING& s) { char* str = new char[strlen(p) + strlen(s.p) + 3]; str[0] = '\0'; strcat(str, p); strcat(str, "--"); strcat(str, s.p); delete[]p; p = str; return *this; } friend ostream& operator<<(ostream& out, CSTRING& str); friend istream& operator>>(istream& in, CSTRING& str); int operator>(const CSTRING& str); }; ostream& operator<<(ostream& out, CSTRING& str) { out << str.p; return out; } istream& operator>>(istream& in, CSTRING& str) { in >> str.p; return in; } int CSTRING:: operator>(const CSTRING& str) { if (strcmp(p, str.p)>0){ return 1; }else {return 0; } } int main() { int n, i, j; while (cin >> n) { CSTRING *c = new CSTRING[n + 2]; for (i = 0; i < n; i++) { cin >> c[i]; } for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) if (c[j] > c[j + 1]) { c[n] = c[j]; c[j] = c[j + 1]; c[j + 1] = c[n]; } for (i = 0; i < n; i++) c[n + 1] += c[i]; cout << c[n + 1] << endl; delete[] c; } return 0; }
这里提供一个多文件的实现,将CSTRING类的定义和实现分别放在CSTRING.h和CSTRING.cpp文件中,主函数放在main.cpp文件中。
CSTRING.h文件内容如下:
```c++
#ifndef CSTRING_H
#define CSTRING_H
#include<iostream>
#include<string.h>
using namespace std;
class CSTRING {
private:
char* p;
public:
CSTRING();
CSTRING(const char* str);
CSTRING(const CSTRING& str);
~CSTRING();
CSTRING operator+=(CSTRING& s);
int operator>(const CSTRING& str);
friend ostream& operator<<(ostream& out, CSTRING& str);
friend istream& operator>>(istream& in, CSTRING& str);
};
#endif
```
CSTRING.cpp文件内容如下:
```c++
#include "CSTRING.h"
CSTRING::CSTRING() {
int len = 0;
p = new char[len + 1];
p[0] = '\0';
}
CSTRING::CSTRING(const char* str) {
int len = strlen(str);
p = new char[len + 1];
strcpy(p, str);
}
CSTRING::CSTRING(const CSTRING& str) {
int len = strlen(str.p);
p = new char[len + 1];
strcpy(p, str.p);
}
CSTRING::~CSTRING() {
delete[] p;
}
CSTRING CSTRING::operator+=(CSTRING& s) {
char* str = new char[strlen(p) + strlen(s.p) + 3];
str[0] = '\0';
strcat(str, p);
strcat(str, "--");
strcat(str, s.p);
delete[]p;
p = str;
return *this;
}
int CSTRING::operator>(const CSTRING& str) {
if (strcmp(p, str.p) > 0) {
return 1;
}
else {
return 0;
}
}
ostream& operator<<(ostream& out, CSTRING& str) {
out << str.p;
return out;
}
istream& operator>>(istream& in, CSTRING& str) {
in >> str.p;
return in;
}
```
main.cpp文件内容如下:
```c++
#include<iostream>
#include"CSTRING.h"
using namespace std;
int main() {
int n, i, j;
while (cin >> n)
{
CSTRING* c = new CSTRING[n + 2];
for (i = 0; i < n; i++)
{
cin >> c[i];
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (c[j] > c[j + 1])
{
c[n] = c[j];
c[j] = c[j + 1];
c[j + 1] = c[n];
}
for (i = 0; i < n; i++)
c[n + 1] += c[i];
cout << c[n + 1] << endl;
delete[] c;
}
return 0;
}
```
阅读全文