include <iostream>
include <string.h>
using namespace std;
class mystring{
public:
mystring(){
_str = new char[1];
*_str = '\0';
}
mystring(char *s){
int len = strlen(s);
_str = new char[len+1];
strcpy(_str, s);
}
mystring(const char* s){
if(s == nullptr){
_str = new char[1];
}else{
_str = new char[strlen(s) + 1];
strcpy(_str, s);
}
}
//copy constructor
mystring(const mystring &another){
_str = new char[strlen(another._str) + 1];
strcpy(_str, another._str);
}
mystring &operator=(const mystring &another){
if(this == &another){
return *this;
}
delete []this->_str;
int len = strlen(another._str);
_str = new char[len+1];
strcpy(_str, another._str);
return *this;
}
mystring &operator+(const mystring &another){
int catLen = strlen(this->_str);
int srcLen = strlen(another._str);
int len = catLen + srcLen;
this->_str = static_cast<char *>(realloc(this->_str, len+1));
memset(this->_str+catLen,0,srcLen+1);
strcat(this->_str, another._str);
return *this;
}
bool operator == (const mystring &another){
return strcmp(this->_str, another._str) == 0;
}
bool operator > (const mystring &another){
return strcmp(this->_str, another._str) > 0;
}
bool operator < (const mystring &another){
return strcmp(this->_str, another._str) < 0;
}
char &operator[](int idx){
return _str[idx];
}
~mystring(){
delete [] _str;
}
private:
char *_str;
};
int main()
{
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。