题目
仿 String
类 ,写一个 MyString
类
代码
main.c
#include
#include "myString.h"
using namespace std;
void printStr(const char *msg, MyString &str);
int main(int argc, char const *argv[])
{
// 无参构造
MyString s0;
// 有参构造
MyString s1("hello");
// 拷贝构造
MyString s2 = "world!";
MyString s3 = s2;
cout << "s0 = " << s0 << endl;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "=========================" << endl;
// 拷贝赋值
s3 = s1;
cout << "s3 = s1 ==> s3 = " << s3 << endl;
s3 = "ok";
cout << "s3 = \"ok\" ==> s3 = " << s3 << endl;
// +重载
s3 = s1 + " " + s2;
cout << "s3 = s1 + \" \" + s2; ==> s3 = " << s3 << endl;
// +=重
s3 += "###";
cout << "s3 += \"###\" ==> s3 = " << s3 << endl;
s3 += s1;
cout << "s3 += s1; ==> s3 = " << s3 << endl;
// []
cout << "s4[1] = " << s3[1] << endl;
cout << "=========================" << endl;
// ==
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << boolalpha << endl;
cout << "s1 == s2 ==> " << (s1 == s2) << endl;
// !=
cout << "s1 != s2 ==> " << (s1 != s2) << endl;
// <
cout << "s1 < s2 ==> " << (s1 < s2) << endl;
// >
cout << "s1 > s2 ==> " << (s1 > s2) << endl;
// <=
cout << "s1 <= s2 ==> " << (s1 <= s2) << endl;
// >=
cout << "s1 >= s2 ==> " << (s1 >= s2) << endl;
// 转换c风格
cout << "=========================" << endl;
const char *s5 = s1.c_str();
cout << "c风格字符串 s5 = " << s5 << endl;
return 0;
}
/**
* @brief 打印字符串
* @param msg 自定义的信息
* @param str 要打印的字符串对象
* @return
*/
void printStr(const char *msg, MyString &str)
{
cout << msg << " = ";
// 判空
if (str.isEmpty()) {
cout << str.at(0) << endl;
return;
}
// 按长度遍历
for (int i = 0; i < str.size(); i++) {
// 按下标输出
cout << str.at(i);
}
cout << endl;
}
MyString.h
#ifndef _MY_STRING_H_
#define _MY_STRING_H_
#include
using namespace std;
class MyString
{
public:
MyString();
MyString(const char *s);
MyString(const char *&s); // 拷贝构造
MyString(const MyString &other); // 拷贝构造
~MyString(); // 析构
MyString &operator=(const char *s); // 拷贝赋值
MyString &operator=(const MyString &other); // 拷贝赋值
friend ostream &operator<<(ostream &output, const MyString &myString); // 输出重载
friend istream &operator>>(istream &input, const MyString &myString); // 输入重载
friend const MyString operator+(const MyString &l, const MyString &r); // +重载
friend MyString &operator+=(MyString &l, const MyString &r); // +=重载
friend bool operator==(const MyString &l, const MyString &r); // ==重载
friend bool operator!=(const MyString &l, const MyString &r); // !=重载
friend bool operator<(const MyString &l, const MyString &r); // <重载
friend bool operator>(const MyString &l, const MyString &r); // >重载
friend bool operator<=(const MyString &l, const MyString &r); // <=重载
friend bool operator>=(const MyString &l, const MyString &r); // >=重载
char &operator[](int pos); // []重载
bool isEmpty() const; // 判空
int size() const; // 长度
const char *c_str() const; // MyString字符串转换为c风格
char &at(int pos) const; // 按下标获取字符
private:
char *_str;
int _size;
};
#endif //_MY_STRING_H_
MyString.cpp
#include "myString.h"
#include
/**
* @brief 无参构造
* @param 无
* @return 无
*/
MyString::MyString() : _size(0), _str(new char) { strcpy(_str, ""); }
/**
* @brief 有参构造
* @param s 字符串
* @return 无
*/
MyString::MyString(const char *s)
{
this->_size = strlen(s);
this->_str = new char[this->_size + 1];
strcpy(this->_str, s);
}
/**
* @brief 拷贝构造
* @param s 字符串
* @return 无
*/
MyString::MyString(const char *&s)
{
this->_size = strlen(s);
this->_str = new char[this->_size + 1];
strcpy(this->_str, s);
}
/**
* @brief 拷贝构造
* @param other 目标对象
* @return 无
*/
MyString::MyString(const MyString &other)
{
if (this != &other) {
this->_size = other._size;
this->_str = new char[other._size + 1];
strcpy(this->_str, other._str);
}
}
/**
* @brief 拷贝赋值
* @param s 目标字符串
* @return 源对象
*/
MyString &MyString::operator=(const char *s)
{
this->_size = strlen(s);
delete (this->_str);
this->_str = new char[this->_size + 1];
strcpy(this->_str, s);
return *this;
}
/**
* @brief 拷贝赋值
* @param other 目标对象
* @return 源对象
*/
MyString &MyString::operator=(const MyString &other)
{
if (this != &other) {
this->_size = other._size;
delete (this->_str);
this->_str = new char[this->_size + 1];
strcpy(this->_str, other._str);
}
return *this;
}
/**
* @brief 析构函数
* @param 无
* @return 无
*/
MyString::~MyString() { delete (this->_str); }
/**
* @brief 判空
* @param 无
* @return ture表示空 false表示非空
*/
bool MyString::isEmpty() const { return (this->_size == 0) ? true : false; }
/**
* @brief 获取长度
* @param 无
* @return 长度
*/
int MyString::size() const { return this->_size; }
/**
* @brief 将MyString类型转换为c风格字符串
* @param
* @return
*/
const char *MyString::c_str() const { return this->_str; }
/**
* @brief 按下标获取字符
* @param pos 下标
* @return 下标对应字符
*/
char &MyString::at(int pos) const { return this->_str[pos]; }
/**
* @brief 输出运算符重载
* @param output 输出流对象
* @param myString 要重载输出的对象
* @return 输出流对象
*/
ostream &operator<<(ostream &output, const MyString &myString)
{
output << myString._str;
return output;
}
/**
* @brief 输入运算符重载
* @param input 输入流对象
* @param myString 要重载输出的对象
* @return 输入流对象
*/
istream &operator>>(istream &input, const MyString &myString)
{
input >> myString._str;
return input;
}
/**
* @brief 加法运算符重载
* @param l 左值
* @param r 右值
* @return 零时变量
*/
const MyString operator+(const MyString &l, const MyString &r)
{
MyString ret = l;
strcat(ret._str, r._str);
return ret;
}
/**
* @brief 加等运算符重载
* @param l 左值
* @param r 右值
* @return 零时变量
*/
MyString &operator+=(MyString &l, const MyString &r)
{
// 备份
char tmp[1024];
strcpy(tmp, l._str);
// 重新开辟
delete (l._str);
l._str = new char[l.size() + r.size() + 1];
// 赋值
strcpy(l._str, tmp);
strcat(l._str, r._str);
return l;
}
/**
* @brief ==运算符重载
* @param l 左值
* @param r 右值
* @return 相等返回true 不等返回false
*/
bool operator==(const MyString &l, const MyString &r) { return !strcmp(l._str, r._str); }
/**
* @brief !=运算符重载
* @param l 左值
* @param r 右值
* @return 不等返回true 相等返回false
*/
bool operator!=(const MyString &l, const MyString &r) { return strcmp(l._str, r._str); }
/**
* @brief <运算符重载
* @param l 左值
* @param r 右值
* @return 左值较小返回true 左值较大返回false
*/
bool operator<(const MyString &l, const MyString &r) { return (l._str - r._str) < 0 ? true : false; }
/**
* @brief >运算符重载
* @param l 左值
* @param r 右值
* @return 左值较大返回true 左值较小返回false
*/
bool operator>(const MyString &l, const MyString &r) { return (l._str - r._str) > 0 ? true : false; }
/**
* @brief <=运算符重载
* @param l 左值
* @param r 右值
* @return 左值<=右值返回true 左值>右值返回false
*/
bool operator<=(const MyString &l, const MyString &r) { return (l._str - r._str) <= 0 ? true : false; }
/**
* @brief >=运算符重载
* @param l 左值
* @param r 右值
* @return 左值>=右值返回true 左值<右值返回false
*/
bool operator>=(const MyString &l, const MyString &r) { return (l._str - r._str) >= 0 ? true : false; }
/**
* @brief []运算符重载
* @param pos 下标
* @return 下标字符
*/
char &MyString::operator[](int pos) {
return this->_str[pos];
}