string类的简单实现
我们知道String类是C++库中原先定义好的一个类。它包含了一个对象的构造方法、析构 方法、赋值运算符的重载、拷贝构造函数、输出运算符重载、加法重载、比较运算符重载等等。
除此之外,String类的实现常常在面试中被提及到。
- 以下代码简单的实现了String类的一些基本的成员方法及成员函数。
#include<iostream>
#include<assert.h>
using namespace std;
class CString
{
public:
CString();
CString(const char * str);//普通构造函数
CString(const CString &src);//拷贝构造函数
~CString();//析构函数
CString &operator=(const CString &src);//=运算符的构造函数
bool operator==(const CString &src);//逻辑运算符
bool operator==(const char *src);
bool operator>=(const CString &src);
bool operator>=(const char *src);
char operator[](int pos);//按指定位置取得字符
CString operator+(const CString &src);//+运算符的构造函数
CString operator+(const char *src);//+运算符重载
ostream & operator<<(ostream &out);//输出运算符的构造函数
istream & operator>>(istream &in);//输入运算符的构造函数
private:
char *_str;
friend CString operator+(const char *str,const CString &src);//加号运算符的重载函数
friend ostream & operator<<(ostream &out,CString &src);//输出运算符的重载函数
friend istream & operator>>(istream &out,CString &src);//输出运算符的重载函数
};
CString::CString()
{
_str = new char['\0'];
assert(_str != NULL);
}
CString::CString(const char * str)
{
int len = strlen(str);
_str = new char[len + 1];
assert(_str != NULL);
memset(_str, 0, len + 1);
strcpy(_str, str);
}
CString::CString(const CString &src)
{
int len = strlen(src._str);
_str = new char[len + 1];
assert(_str != NULL);
memset(_str, 0, len + 1);
strcpy(_str, src._str);
}
CString::~CString()
{
delete[]_str;
_str = NULL;
}
CString &CString::operator = (const CString &src)
{
if (this != &src)
{
delete[]_str;
int len = strlen(src._str);
_str = new char[len + 1];
assert(_str != NULL);
memset(_str, 0, len + 1);
strcpy(_str, src._str);
}
return *this;
}
bool CString::operator==(const CString &src)
{
if (strlen(_str) == strlen(src._str))
{
if (strcmp(_str, src._str) == 0)
return true;
else
return false;
}
else
return false;
}
bool CString::operator==(const char *src)
{
if (strlen(_str) == strlen(src))
{
if (strcmp(_str, src) == 0)
return true;
else
return false;
}
else
return false;
}
bool CString::operator>=(const CString &src)
{
if (strcmp(_str, src._str) >= 0)
return true;
else
return false;
}
bool CString::operator>=(const char *src)
{
if (strcmp(_str, src) >= 0)
return true;
else
return false;
}
char CString::operator[](int pos)
{
if (pos >= 0 && pos <= strlen(_str))
return _str[pos];
}
CString CString::operator+(const CString &src)
{
int len = strlen(_str) + strlen(src._str);
char *p = new char[len + 1];
memset(p, 0, len + 1);
strcpy(p, _str);
strcat(p, src._str);
CString str(p);
delete[]p;
return str;
}
CString CString::operator+(const char *src)
{
int len = strlen(_str) + strlen(src);
char *p = new char[len + 1];
memset(p, 0, len + 1);
strcpy(p, _str);
strcat(p, src);
CString str(p);
delete[]p;
return str;
}
ostream & CString::operator<<(ostream &out)
{
out << _str << endl;
return out;
}
istream & CString::operator>>(istream &in)
{
in >> _str;
return in;
}
CString operator+(const char *str,const CString &src)
{
int len = strlen(str)+strlen(src._str);
char *p = new char[len+1];
memset(p,0,len+1);
strcpy(p,str);
strcat(p,src._str);
CString Str(p);
delete []p;
return Str;
}
ostream & operator<<(ostream &out,CString &src)
{
out<<src._str;
return out;
}
istream & operator>>(istream &in,CString &src)
{
in>>src._str;
return in;
}
int main()
{
CString str1("hello");
CString str2("world");
cout<<str1<<endl;
str2.operator<<(cout);
CString str3;
str3 = str1+str2;
str3.operator<<(cout);
str3 = str1+"judgec";
str3.operator<<(cout);
str3 = "judgec"+str1;
str3.operator<<(cout);
cout<< str2[1]<<endl;
if(str1 == "hello")
{
cout<<"Yes!"<<endl;
}
return 0;
}
运行结果: