c++实现string内的operator一系列函数
运行结果如下
同班同学代码写的很漂亮,个人仿照其风格如下
#include <iostream>
#include <cstring>
using namespace std;
class my_string
{
public:
my_string(); //无参构造
my_string(const char *s); //有参构造
my_string(const char *&s); // 拷贝构造
my_string(const my_string &other); // 拷贝构造
~my_string(); // 析构
my_string &operator=(const char *s); // 拷贝赋值
my_string &operator=(const my_string &other); // 拷贝赋值
friend ostream &operator<<(ostream &output, const my_string &myString); // 输出重载
friend istream &operator>>(istream &input, const my_string &myString); // 输入重载
friend const my_string operator+(const my_string &l, const my_string &r); // +重载
friend my_string &operator+=(my_string &l, const my_string &r); // +=重载
friend bool operator==(const my_string &l, const my_string &r); // ==重载
friend bool operator!=(const my_string &l, const my_string &r); // !=重载
friend bool operator<(const my_string &l, const my_string &r); // <重载
friend bool operator>(const my_string &l, const my_string &r); // >重载
friend bool operator<=(const my_string &l, const my_string &r); // <=重载
friend bool operator>=(const my_string &l, const my_string &r); // >=重载
char &operator[](int pos); // []重载
bool d_empty() const; // 判空
int d_size() const; // 长度
const char *cdstr() const; // my_string字符串转换为c风格
char &at(int pos) const; // 按下标获取字符
private:
char *dstr;
int _size;
};
//无参构造
my_string::my_string() : _size(10), dstr(new char) {
strcpy(dstr, "");
}
//有参构造
my_string::my_string(const char *s)
{
this->_size = strlen(s);
this->dstr = new char[this->_size + 1];
strcpy(this->dstr, s);
}
// 拷贝构造
my_string::my_string(const char *&s)
{
this->_size = strlen(s);
this->dstr = new char[this->_size + 1];
strcpy(this->dstr, s);
}
//拷贝构造
my_string::my_string(const my_string &other)
{
if (this != &other) {
this->_size = other._size;
this->dstr = new char[other._size + 1];
strcpy(this->dstr, other.dstr);
}
}
//拷贝赋值
my_string &my_string::operator=(const char *s)
{
this->_size = strlen(s);
delete (this->dstr);
this->dstr = new char[this->_size + 1];
strcpy(this->dstr, s);
return *this;
}
//拷贝赋值
my_string &my_string::operator=(const my_string &other)
{
if (this != &other) {
this->_size = other._size;
delete (this->dstr);
this->dstr = new char[this->_size + 1];
strcpy(this->dstr, other.dstr);
}
return *this;
}
//析构函数
my_string::~my_string() { delete (this->dstr); }
//判空
bool my_string::d_empty() const { return (this->_size == 0) ? true : false; }
//获取长度
int my_string::d_size() const { return this->_size; }
//将my_string类型转换为c风格字符串
const char *my_string::cdstr() const { return this->dstr; }
//按下标获取字符
char &my_string::at(int pos) const { return this->dstr[pos]; }
//输出运算符重载
ostream &operator<<(ostream &output, const my_string &myString)
{
output << myString.dstr;
return output;
}
//输入运算符重载
istream &operator>>(istream &input, const my_string &myString)
{
input >> myString.dstr;
return input;
}
//加法运算符重载@return
const my_string operator+(const my_string &l, const my_string &r)
{
my_string ret = l;
strcat(ret.dstr, r.dstr);
return ret;
}
//加等运算符重载
my_string &operator+=(my_string &l, const my_string &r)
{
// 备份
char tmp[1024];
strcpy(tmp, l.dstr);
// 重新开辟
delete (l.dstr);
l.dstr = new char[l.d_size() + r.d_size() + 1];
// 赋值
strcpy(l.dstr, tmp);
strcat(l.dstr, r.dstr);
return l;
}
//==运算符重载
bool operator==(const my_string &l, const my_string &r) {
return !strcmp(l.dstr, r.dstr);
}
// !=运算符重载
bool operator!=(const my_string &l, const my_string &r) {
return strcmp(l.dstr, r.dstr);
}
// <运算符重载
bool operator<(const my_string &l, const my_string &r) {
return (l.dstr - r.dstr) < 0 ? true : false;
}
// >运算符重载
bool operator>(const my_string &l, const my_string &r) {
return (l.dstr - r.dstr) > 0 ? true : false;
}
// <=运算符重载
bool operator<=(const my_string &l, const my_string &r) {
return (l.dstr - r.dstr) <= 0 ? true : false;
}
// >=运算符重载
bool operator>=(const my_string &l, const my_string &r) {
return (l.dstr - r.dstr) >= 0 ? true : false;
}
// []运算符重载
char &my_string::operator[](int pos) {
return this->dstr[pos];
}
void printStr(const char *msg, my_string &str);
// 打印字符串
void printStr(const char *msg, my_string &str)
{
cout << msg << " = ";
// 判空
if (str.d_empty()) {
cout << str.at(0) << endl;
return;
}
// 按长度遍历
for (int i = 0; i < str.d_size(); i++) {
// 按下标输出
cout << str.at(i);
}
cout << endl;
}
int main(int argc, char const *argv[])
{
// 无参构造
my_string s0; //未赋值,为空
// 有参构造
my_string s1("hqyj sh");
// 拷贝构造
my_string s2 = "very good";
my_string s3 = s2;
//分别输出4次的字符串
cout<< endl;
cout << "s0 = " << s0 << endl;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout<< endl;
// 拷贝赋值 ,s4的两次函数赋值
my_string s4;
s4 = s1;
cout << "=的第一次重载 s4 = " << s4 << endl;
cout<< endl;
s4 = "nice";
cout << "=的第二次重载 s4 = " << s4 << endl;
cout<< endl;
// +重载
cout << "+重载 s2 + s1 = " << s2 + s1 << endl;
cout<< endl;
// +=重载
s2 += s1;
cout << "+=重载 s2 = " << s2 << endl;
cout<< endl;
// []
cout << "[]重载 s4[1] = " << s4[1] << endl;
cout<< endl;
cout << "******************************************" << endl;
cout<< endl;
// ==
cout << "s1 = " << s1<< endl;
cout << "s2 = " << s2<< endl;
cout << boolalpha;
cout << "==重载 s1 == s2 是: " << (s1 == s2) << endl;
cout<< endl;
// !=
cout << "!=重载 s1 != s2 是: " << (s1 != s2) << endl;
cout<< endl;
// <
cout << "<重载 s1 < s2 是: " << (s1 < s2) << endl;
cout<< endl;
// >
cout << ">重载 s1 > s2 是: " << (s1 > s2) << endl;
cout<< endl;
// <=
cout << "<=重载 s1 <= s2 是: " << (s1 <= s2) << endl;
cout<< endl;
// >=
cout << ">=重载 s1 >= s2 是: " << (s1 >= s2) << endl;
cout<< endl;
// 转换c风格
cout << "******************************************" << endl;
cout<< endl;
const char *s5 = s1.cdstr();
cout << "c风格字符串 s5 = " << s5 << endl;
cout<< endl;
return 0;
}