#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
class String
{
public:
String()
:_pstr(new char[1]())
{
_pstr[0] = '\0';
}
String(const char *pstr)
:_pstr(new char[strlen(pstr)+1]())
{
strcpy(_pstr,pstr);
}
String(const String & rhs)
:_pstr(new char[strlen(rhs.c_str())+1]())
{
strcpy(_pstr,rhs.c_str());
}
~String(){
if(_pstr){
delete [] _pstr;
_pstr = nullptr;
}
}
String &operator=(const String &rhs){
if(this != &rhs){
delete [] _pstr;
_pstr = new char[strlen(rhs.c_str())+1]();
strcpy(_pstr,rhs.c_str());
}
return *this;
}
String &operator=(const char *pstr){
if(this->_pstr != pstr){
delete [] _pstr;
_pstr = new char[strlen(pstr)+1]();
strcpy(_pstr,pstr);
}
return *this;
}
String &operator+=(const String & rhs){
char *tmp = new char[strlen(this->c_str())+strlen(rhs.c_str())+1]();
strcpy(tmp,_pstr);
strcat(tmp,rhs._pstr);
delete _pstr;
_pstr = tmp;
tmp = nullptr;
return *this;
}
String &operator+=(const char *pstr){
char *tmp = new char[strlen(this->c_str())+strlen(pstr)+1]();
strcpy(tmp,_pstr);
strcat(tmp,pstr);
delete _pstr;
_pstr = tmp;
tmp = nullptr;
return *this;
}
char &operator[](std::size_t index){
if(index < this->size()){
return _pstr[index];
}else{
cout << "out of range!" << '\n';
static char nullchar = '\0';
return nullchar;
}
}
const char &operator[](std::size_t index) const{
if(index < this->size()){
return _pstr[index];
}else{
cout << "out of range!" << '\n';
static char nullchar = '\0';
return nullchar;
}
}
std::size_t size() const{
return strlen(_pstr);
}
const char* c_str() const{
return _pstr;
}
friend String operator+(const String &, const String &);
friend String operator+(const String &, const char *);
friend String operator+(const char *, const String &);
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const String &);
friend bool operator<(const String &, const String &);
friend bool operator>(const String &, const String &);
friend bool operator<=(const String &, const String &);
friend bool operator>=(const String &, const String &);
friend std::ostream &operator<<(std::ostream &os, const String &s);
friend std::istream &operator>>(std::istream &is, String &s);
private:
char * _pstr;
};
String operator+(const String & lhs, const String & rhs){
char *tmp = new char[strlen(lhs.c_str())+strlen(rhs.c_str())+1]();
strcpy(tmp,lhs._pstr);
strcat(tmp,rhs._pstr);
return String(tmp);
}
String operator+(const String & lhs, const char *pstr){
char *tmp = new char[strlen(lhs.c_str())+strlen(pstr)+1]();
strcpy(tmp,lhs.c_str());
strcat(tmp,pstr);
return String(tmp);
}
String operator+(const char *pstr, const String &rhs){
char *tmp = new char[strlen(rhs.c_str())+strlen(pstr)+1]();
strcpy(tmp,pstr);
strcat(tmp,rhs.c_str());
return String(tmp);
}
bool operator==(const String & lhs, const String & rhs){
return !strcmp(lhs.c_str(),rhs.c_str());
}
bool operator!=(const String & lhs, const String & rhs){
return strcmp(lhs.c_str(),rhs.c_str());
}
bool operator<(const String & lhs, const String & rhs){
if(strcmp(lhs.c_str(),rhs.c_str()) <0){
return true;
}
return false;
}
bool operator>(const String & lhs, const String & rhs){
if(strcmp(lhs.c_str(),rhs.c_str())>0){
return true;
}
return false;
}
bool operator<=(const String &lhs, const String &rhs){
if(strcmp(lhs.c_str(),rhs.c_str())>0){
return false;
}
return true;
}
bool operator>=(const String &lhs, const String &rhs){
if(strcmp(lhs.c_str(),rhs.c_str())<0){
return false;
}
return true;
}
std::ostream &operator<<(std::ostream &os, const String &s){
os << s.c_str();
return os;
}
std::istream &operator>>(std::istream &is, String &s){
char buf[1024];
is >> buf;
delete [] s._pstr;
s._pstr = new char[strlen(buf)+1]();
strcpy(s._pstr,buf);
return is;
}
void test0(){
String s1("hello");
String s2 = s1;
String s3 = " world";
s2 += s3;
cout << "s2 : "<< s2 << '\n';
s2 += " www";
cout << "s2 : "<< s2 << '\n';
cout<< "s2[0] : " << s2[0] << '\n';
String s4 = s1 + s3;
cout << "s4: "<< s4 << '\n';
String s5 = s1 + " www";
cout << "s5: " << s5 << '\n';
String s6 = "hello" + s3;
cout << "s6: "<< s6 << '\n';
cout << "s4 == s6: " << (s4==s6) << '\n';
cout << "s4!=s6: " << (s4!=s6) <<'\n';
cout << "s4 < s5: " << (s4 < s5) << '\n';
cout << "s4 > s5: " << (s4 > s5) << '\n';
cout << "s4 <= s6: " << (s4 <= s6) << '\n';
cout << "s4 >= s6: " << (s4 >= s6) << '\n';
std::cin >> s1;
cout << "s1: " << s1 << '\n';
}
int main(){
test0();
return 0;
}
改进:
//动态获取从键盘输入数据的长度再写入。
std::istream &operator>>(istream &is, String &rhs)
{
if(rhs._pstr)
{
delete [] rhs._pstr;
rhs._pstr = nullptr;
}
//动态获取从键盘输入数据的长度
vector<char> buffer;
char ch;
while((ch = is.get()) != '\n'&&ch != ' '&&ch != '\t')
{
buffer.push_back(ch);
}
rhs._pstr = new char[buffer.size() + 1]();
strncpy(rhs._pstr, &buffer[0], buffer.size());
return is;
}