StringEx是一个自己实现的字符串类,实现了最基本的初始化和赋值功能,重载了流运算符和加法运算符。编译环境是Windows下的 MinGW 5.1.6
#include#include using namespace std;//字符串类class StringEx{public: //构造函数 StringEx(char* s = NULL) { this -> m_Data = s; } StringEx(const StringEx &Other) { m_Data = new char[strlen(Other.m_Data) + 1]; if(this -> m_Data) { strcpy(this -> m_Data, Other.m_Data); } } //以输出 char* ToString() { return m_Data; } //设置字符串的值 void SetString(char* s) { delete[] m_Data; m_Data = new char[strlen(s) + 1]; if(this -> m_Data) { strcpy(this -> m_Data, s); } } //在MinGW中这个函数与上面的函数: // StringEx(const StringEx &Other) //只要声明一个即可实现赋值 StringEx operator = (StringEx &Other) { if(this == &Other) { return *this; } delete[] this -> m_Data; this -> m_Data = new char[strlen(Other.m_Data + 1)]; if(this -> m_Data) { strcpy(this -> m_Data, Other.m_Data); } return *this; } //重载加法运算符,将两个字符串连接 StringEx operator + (StringEx &Another) { if(Another.ToString() == NULL) { return *this; } char* s = new char[strlen(this -> m_Data) + strlen(Another.m_Data) + 1]; int i, j; for(i = 0, j = 0; i < strlen(this -> m_Data); i++, j++) { s[j] = m_Data[j]; } for(i = 0; i < strlen(Another.m_Data); i++, j++) { s[j] = Another.m_Data[i]; } s[j] = '\0'; return StringEx(s); } //重载加法运算符,将字符串类与字符串连接 StringEx operator + (char* s2) { if(s2 == NULL) { return *this; } char* s = new char[strlen(this -> m_Data) + strlen(s2) + 1]; int i, j; for(i = 0, j = 0; i < strlen(this -> m_Data); i++, j++) { s[j] = m_Data[j]; } for(i = 0; i < strlen(s2); i++, j++) { s[j] = s2[i]; } s[j] = '\0'; return StringEx(s); } //析构函数 ~StringEx(void) { delete m_Data; }private: //字符串内容 char *m_Data;};//重载加法运算符,将字符串与字符串类连接StringEx operator + (char* s1, StringEx &Another){ if(s1 == NULL) { return Another; } char* s2 = Another.ToString(); char* s = new char[strlen(s1) + strlen(s2) + 1]; int i, j; for(i = 0, j = 0; i < strlen(s1); i++, j++) { s[j] = s1[j]; } for(i = 0; i < strlen(Another.ToString()); i++, j++) { s[j] = s2[i]; } s[j] = '\0'; return StringEx(s);}//重载流插入运算符ostream& operator << (ostream& output, StringEx& se){ output << se.ToString(); return output;}//重载流提取运算符istream& operator >> (istream& input, StringEx& se){ //char* s = "Galatea4321?"; char *s = new char[1024]; scanf("%s", s); se.SetString(s); return input;}int main(){ cout << "字符串类StringEx" << endl << endl; //3种赋值 //以printf形式输出 "Tsybius2014!" StringEx x = StringEx("Tsybius2014!_1"); printf("%s\n", x.ToString()); //1 StringEx y = "Tsybius2014!_2"; printf("%s\n", y.ToString()); //2 StringEx z = x; printf("%s\n\n", z.ToString()); //3 //以cout形式输出 "Tsybius2014!" cout << x << endl; cout << y << endl; cout << z << endl << endl; //通过cin读取输入的字符串 cout << "请输入一个字符串" << endl; StringEx a; cin >> a; cout << a << endl << endl; //连接两个字符串 StringEx ta = StringEx("Tsybius1234"); cout << "ta:" << ta << endl; StringEx tb = StringEx("4321Galatea"); cout << "tb:" << tb << endl; StringEx tc = ta + tb; //两个StringEx类连接 cout << "tc:" << tc << endl; StringEx td = ta + "4321Galatea"; //StringEx类与字符串连接 cout << "td:" << td << endl; StringEx te = "Tsybius1234" + tb; //字符串与StringEx类连接 cout << "te:" << te << endl; return 0;}
程序运行结果
END