实现字符串的各种运算方式
#include <iostream>
#include <string.h>
using namespace std;
class mystring // 自定义string类
{
private:
char *str; // char* c语言风格的的字符串类型
size_t size; // 字符串所占字节大小
public:
mystring(); // 无参构造函数
mystring(const char *s); // 有参构造函数
mystring(const mystring &other); // 拷贝构造函数
~mystring(); // 析构函数
size_t mysize(); // mysize函数类似于size函数
const char *myc_stc(); // 转换为c风格的字符串
char *my_at(int pos, char c);
ostream &operator<<(ostream &L) // <<
{
L << this->str;
return L;
}
istream &operator>>(ostream &O) // >> 未实现
{
// this->str >> O;
// return O;
}
/*各种运算符重载*/
const mystring operator+(const mystring &R); // +
// mystring operator=(const mystring &R); // = ******没能实现******
const bool operator==(const mystring &R); // ==
const bool operator!=(const mystring &R); // !=
const bool operator<(const mystring &R); // <
const bool operator>(const mystring &R); // >
mystring operator+=(mystring &R); // +=
char &operator[](const int pos) // []
{
return this->str[pos];
}
}
mystring::mystring() : size(32) // 无参构造函数,默认申请32字节长度
{
str = new char[size];
cout << "调用了无参构造函数" << endl;
}
mystring::mystring(const char *s) : size(32) // 有参构造函数,默认申请32字节长度
{
int len = strlen(s);
for (int i = 1; size < len; i++)
{
size *= i;
}
str = new char[size + 1];
strcpy(str, s);
cout << "调用了有参构造函数"
<< "str= " << str << endl;
}
mystring::~mystring() // 析构函数
{
delete[] str; // 释放str所占用的空间
str = nullptr; // 将str指向空指针
cout << "调用了析构函数" << endl;
}
mystring::mystring(const mystring &other) : size(other.size) // 拷贝构造函数,构造函数后加冒号是初始化表达式
{
str = new char[size];
strcpy(str, other.str);
}
size_t mystring::mysize() // 返回字符串长度
{
return this->size;
}
const char *mystring::myc_stc()
{
return this->str;
}
char *mystring::my_at(int pos, char c)
{
if (pos <= 0 || pos > (int)strlen(str))
{
cout << "input pos-sub error..." << endl; // 输出越界的提示
}
else
{
str[pos] = c;
return str;
}
}
const bool mystring::operator!=(const mystring &R) // 重载 != 运算符
{
cout << "调用了 重载 != 运算符" << endl;
if (this->str != R.str)
return true;
return false;
}
const bool mystring::operator==(const mystring &R) // 重载 == 运算符
{
cout << "调用了 重载 == 运算符" << endl;
if (strcmp(this->str, R.str) == 0)
return true;
return false;
}
const mystring mystring::operator+(const mystring &R) // +
{
const int len = this->size + R.size;
char temp[len + 1] = "";
sprintf(temp, "%s%s", this->str, R.str);
cout << "调用了 重载 + 运算符" << endl;
return temp;
}
const bool mystring::operator<(const mystring &R) // <
{
cout << "调用了 重载 < 运算符" << endl;
if (strcmp(this->str, R.str) < 0)
return true;
return false;
}
const bool mystring::operator>(const mystring &R) // >
{
cout << "调用了 重载 > 运算符" << endl;
if (strcmp(this->str, R.str) > 0)
return true;
return false;
}
// mystring mystring::operator=(const mystring &R) // =
// {
// cout << "调用了 重载 = 运算符" << endl;
// str = new char[R.size];
// strcpy(str, R.str);
// return str;
// }
mystring mystring::operator+=(mystring &R) // +=
{
cout << "调用了 重载 += 运算符" << endl;
// 判断str的容量是否符合
// char *temp ;
// strcpy(temp, str);
// int len = size + R.size;
// while (size < len)
// {
// this->size *= 2;
// }
// delete[] str;
// str = new char[size];
// strcpy(str, temp);
strcat(str, R.str);
cout << "str= " << str << endl;
return str;
}
void test01()
{
mystring s1("hello");
mystring s2("world");
s1 += s2;
printf("%s\n", s1);
}
int main()
{
test01();
system("pause");
return 0;
}