// MyString.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include <iostream> #include <string> using namespace std; class MyString { public: //三个重载的构造函数 MyString(); MyString(const char* str); MyString(const MyString& str); //析构函数 ~MyString(); //重载运算符 MyString& operator = (const MyString& str); friend ostream& operator << (ostream& os, const MyString& str); //重载输出操作符 friend istream& operator >> (istream& is, MyString& str); //重载输入操作符 friend MyString operator + (const MyString& str1,const MyString& str2); //重载加号操作符,注意返回引用不行,为啥呢? friend MyString operator += (MyString& str1, const MyString& str2); //重载+=操作符 friend bool operator == (const MyString& str1, const MyString& str2); //重载相等操作符 friend bool operator != (const MyString& str1, const MyString& str2); //重载不相等操作符 //功能函数 int getLenth(); //取得string长度函数 MyString substr(int pos, int len); //求string的字串 bool empty(); //判断string是否为空 private: char* p; int len; }; //默认构造函数,初始化为空串 MyString::MyString() { len = 0; p = new char[len + 1]; p[0] = '\0'; } //构造函数,用一个字符串初始化 MyString::MyString(const char* str) { len = strlen(str); p = new char[len + 1]; strcpy(p, str); } //复制构造函数,用另外一个string初始化 MyString::MyString(const MyString& str) { len = str.len; p = new char[len + 1]; strcpy(p, str.p); } //析构函数 MyString::~MyString() { delete [] p; } //重载赋值操作符( = ) MyString& MyString::operator = (const MyString& str) { if (this->p == str.p) { return *this; } delete [] p; len = str.len; p = new char[len + 1]; strcpy(p, str.p); return *this; } //重载输出操作符( << ) ostream& operator << (ostream& os, const MyString& str) { os << str.p; return os; } //重载输入操作符( >> ) istream& operator >> (istream& is, MyString& str) { char ch; char c[100]; int i(0); while (is >> ch) { c[i] = ch; i ++; } c[i] = '\0'; delete [] str.p; str.p = new char[i + 1]; str.len = i; strcpy(str.p, c); return is; } //重载加号操作符( + ) MyString operator + (const MyString& str1, const MyString& str2) { MyString str; delete [] str.p; str.len = str1.len + str2.len; str.p = new char[str.len + 1]; strcpy(str.p, str1.p); strcat(str.p, str2.p); return str; } //重载相加赋值操作符( += ) MyString operator += (MyString& str1, const MyString& str2) { str1 = str1 + str2; return str1; } //重载相等操作符 bool operator == (const MyString& str1, const MyString& str2) { if (strcmp(str1.p, str2.p) == 0) { return true; } return false; } //重载不相等操作符 bool operator != (const MyString& str1, const MyString& str2) { if (strcmp(str1.p, str2.p) == 0) { return false; } return true; } //取得string长度函数 int MyString::getLenth() { int i(0); char* p = this->p; while (*p != NULL) { p ++; i ++; } return i; } //求string的字串 MyString MyString::substr(int pos, int count) { MyString str; if(pos < 0||count < 0 || pos > len) //参数不合理,不取子串 { str.len = 0; str.p[0] = '\0'; } else { if(pos + count - 1 >= this->len) //字符串不够长 count = len-pos; str.p = new char[count + 1]; str.len = count; for(int i = 0,j = pos - 1; i<count; i++, j++) str.p[i] = p[j]; str.p[str.len] = '\0'; } return str; } //判断string是否为空 bool MyString::empty() { if (len == 0) { return true; } return false; }
MyString实现
最新推荐文章于 2022-08-12 00:03:10 发布