代码:
#include <iostream>
using namespace std;
class CStr
{
private:
char* data;
int len;
public:
CStr(const char* d = 0)
{
//cout << "construct----------" << endl;
if (d == 0)
{
data = 0;
len = 0;
}
else
{
len = 0;
while (d[len] != '\0')
len++;
data = new char[len + 1];//多申请一个字节给\0
for (int i = 0; i < len; i++)
data[i] = d[i];
data[len] = '\0'; //结束符
}
}
~CStr()
{
//cout << "disconstruct....." << endl;
//析构函数中不要delete data;否则+运算回得不到结果
}
CStr& operator = (CStr& s)
{
//cout << "=:" << endl;
delete[] data;
len = s.len;
data = new char[len + 1];
for (int i = 0; i < len; i++)
data[i] = s.data[i];
data[len] = '\0';
return *this;
}
CStr operator +(const CStr& a)
{
//cout << "+" << endl;
char* p = new char[len + a.len + 1];
int i = 0;
for (; i < len; i++)
p[i] = data[i];
for (int j = 0; j < a.len; j++)
p[i + j] = a.data[j];
p[len + a.len] = '\0';
CStr t(p);
delete[] p;
return t;
}
CStr operator +=(const CStr& a)
{
this->data = (char*)realloc(this->data, len + a.len + 1); //扩容
for (int j = 0; j < a.len; j++)
this->data[len + j] = a.data[j];
this->data[len + a.len] = '\0';
len += a.len; //修正长度
return *this;
}
bool operator >(const CStr& a)
{
int t = len > a.len ? a.len : len;
for (int i = 0; i < t; i++)
{
if (data[i] > a.data[i])
return true;
}
return false;
}
bool operator <(const CStr& a)
{
int t = len > a.len ? a.len : len;
for (int i = 0; i < t; i++)
{
if (data[i] < a.data[i])
return true;
}
return false;
}
bool operator ==(const CStr& a)
{
if (len != a.len)
return false;
for (int i = 0; i < len; i++)
{
if (data[i] != a.data[i])
return false;
}
return true;
}
char operator [](int index)
{
if (index < len)
return data[index];
else
return '\0';
}
friend ostream& operator <<(ostream& out, const CStr& a)
{
out << a.data;
return out;
}
};
int main()
{
char buf[100] = { 0 };
cout << "请输入一个字符串创建CStr 实例a:" << endl;
cin >> buf;
CStr a(buf);
cout << "请输入一个字符串创建CStr 实例b:" << endl;
cin >> buf;
CStr b(buf);
CStr c;
c = a;
cout << "CStr c=a; c:" << c << endl;
CStr d = a + b;
cout << "CStr d=a+b; d:" << d << endl;
cout << "a>b:" << (a > b) << endl;
cout << "请输入要获取的实例a的元素下标:";
int index;
cin >> index;
cout << "a[" << index << "]=" << a[index] << endl;
return 0;
}
基于new Bing的回答:
#include <cstring>
#include <iostream>
class CStr {
public:
CStr() : str(nullptr), len(0) {}
CStr(const char* s) : str(nullptr), len(0) {
if (s) {
len = strlen(s);
str = new char[len+1];
strcpy(str, s);
}
}
CStr(const CStr& s) : str(nullptr), len(s.len) {
if (s.str) {
str = new char[len+1];
strcpy(str, s.str);
}
}
~CStr() {
if (str) delete[] str;
}
CStr& operator=(const char* s) {
if (str) delete[] str;
str = nullptr;
len = 0;
if (s) {
len = strlen(s);
str = new char[len+1];
strcpy(str, s);
}
return *this;
}
CStr& operator=(const CStr& s) {
if (this != &s) {
if (str) delete[] str;
str = nullptr;
len = s.len;
if (s.str) {
str = new char[len+1];
strcpy(str, s.str);
}
}
return *this;
}
CStr operator+(const char* s) const {
CStr newStr;
newStr.len = len + strlen(s);
newStr.str = new char[newStr.len+1];
strcpy(newStr.str, str);
strcat(newStr.str, s);
return newStr;
}
CStr operator+(const CStr& s) const {
CStr newStr;
newStr.len = len + s.len;
newStr.str = new char[newStr.len+1];
strcpy(newStr.str, str);
strcat(newStr.str, s.str);
return newStr;
}
CStr& operator+=(const char* s) {
len += strlen(s);
char* newStr = new char[len+1];
strcpy(newStr, str);
strcat(newStr, s);
if (str) delete[] str;
str = newStr;
return *this;
}
CStr& operator+=(const CStr& s) {
len += s.len;
char* newStr = new char[len+1];
strcpy(newStr, str);
strcat(newStr, s.str);
if (str) delete[] str;
str = newStr;
return *this;
}
bool operator<(const CStr& s) const {
return strcmp(str, s.str) < 0;
}
bool operator>(const CStr& s) const {
return strcmp(str, s.str) > 0;
}
bool operator<=(const CStr& s) const {
return strcmp(str, s.str) <= 0;
}
bool operator>=(const CStr& s) const {
return strcmp(str, s.str) >= 0;
}
bool operator==(const CStr& s) const {
return strcmp(str, s.str) == 0;
}
char operator[](int index) const {
if (index < 0 || index >= len) {
return '\0';
}
return str[index];
}
friend std::ostream& operator<<(std::ostream
以上代码实现了一个名为CStr的类,用于表示字符串,并定义了赋值运算符、连接运算符、关系运算符和字符提取运算符。
该类中有三个成员变量:一个char类型指针str用于指向动态分配的字符数组,一个整型变量len表示字符串的长度,一个默认构造函数和一个从const char*和另一个CStr对象构造的构造函数。析构函数用于释放动态分配的字符数组。
赋值运算符(=)被实现为两个版本:一个接受const char*参数,另一个接受CStr参数。这些函数会释放当前字符串的内存(如果有)并重新分配内存来存储新字符串,然后将新字符串复制到新的内存中,并返回一个指向该对象的引用。
连接运算符(+)被实现为两个版本:一个接受const char*参数,另一个接受CStr参数。这些函数创建一个新的CStr对象,该对象包含当前字符串和另一个字符串的连接,并返回该对象。
复合连接运算符(+=)也被实现为两个版本:一个接受const char*参数,另一个接受CStr参数。这些函数将另一个字符串连接到当前字符串上,并返回一个指向该对象的引用。
关系运算符(<、>、<=、>=、==)被实现为比较两个CStr对象的字符串内容,并返回一个布尔值。
字符提取运算符([])接受一个整数索引,返回当前字符串中该索引处的字符。如果该索引越界,则返回一个空字符。
还有一个友元函数std::ostream& operator<<,用于将CStr对象输出到标准输出流。
不知道你这个问题是否已经解决, 如果还没有解决的话: