自己写string类(C++)
简介:这是作者当初自己根据C++中的string数据类型,根据自己的理解自己仿写的一个STRING类。
代码如下:
// String 类 class STRING { public: friend istream& operator >> (istream& cin, STRING& str); // 输入,输出运算符的重载 friend ostream& operator << (ostream& cout, STRING& str); STRING() : len(0), str(new char[1]) { str[0] = '\0'; } // STRING 类的构造函数,通过成员初始化表给成员赋值 可以减少一些不不要的操作 提高效率 最开始 // 就开辟一个单位的空间然后用来存放 '\0'表示什么都没有就可以了 STRING(const char* str); //拷贝构造函数 STRING(int len, char sym); // 拷贝构造函数的重载 STRING& operator = (const STRING& str); // 赋值 ~STRING() { delete[] str; } friend bool operator > (const STRING& str1, const STRING& str2); // 比较函数 friend bool operator < (const STRING& str1, const STRING& str2); friend bool operator >= (const STRING& str1, const STRING& str2); friend bool operator <= (const STRING& str1, const STRING& str2); friend bool operator == (const STRING& str1, const STRING& str2); friend bool operator != (const STRING& str1, const STRING& str2); void Input(); void Output() { printf("%s", str); } int Length() { return len; } void Set(const char* str); const char* Get(); protected: int len; char* str; }; istream& operator >> (istream& cin, STRING& str) // 输入 { cin.getline(str.str, 10000); return cin; } ostream& operator << (ostream& cout, STRING& str) // 输出 { cout << str.str; return cout; } STRING::STRING(const char* str) : // 拷贝构造 len(strlen(str)), str(new char[len + 1]) // 利用成员初始化表来把新的str的信息复制过来 { strcpy(this->str, str); // 字符串复制函数 } STRING::STRING(int len, char sym) : // 拷贝构造重载 作用是将STRING初始化为 len 个 sym字符 len(len), str(new char[len + 1]) { int k; for (k = 0; k < len; k++) { str[k] = sym; } str[k] = '\0'; // 这一步末尾加'\0' } STRING& STRING::operator = (const STRING& str) // 赋值 { this->len = strlen(str.str); delete[] this->str; this->str = new char[str.len + 1]; strcpy(this->str, str.str); return *this; } // 一堆比较函数 bool operator > (const STRING& str1, const STRING& str2) { return strcmp(str1.str, str2.str) > 0; } bool operator < (const STRING& str1, const STRING& str2) { return strcmp(str1.str, str2.str) < 0; } bool operator >= (const STRING& str1, const STRING& str2) { return strcmp(str1.str, str2.str) >= 0; } bool operator <= (const STRING& str1, const STRING& str2) { return strcmp(str1.str, str2.str) <= 0; } bool operator == (const STRING& str1, const STRING& str2) { return strcmp(str1.str, str2.str) == 0; } bool operator != (const STRING& str1, const STRING& str2) { return strcmp(str1.str, str2.str) != 0; } void STRING::Set(const char* str) // 通过set函数对STRING类的私有成员进行操作 { len = strlen(str); // 这里要先确定输入的字符串的长度 delete[] this->str; // 然后销毁原来的字符串 this->str = new char[len + 1]; // 重新开辟一段空间 这个空间的大小是len + 1 因为字符串末尾还有个'\0' strcpy(this->str, str); // 动用strcpy 函数将输入的str拷贝过来 } const char* STRING::Get() // 获取成员str的函数 { return str; } void STRING::Input() // 字符串的输入函数 { char* str = new char[10000]; cin.getline(str, 10000); // 注意这个函数的书写方式 c++的编译器里面有一些是不支持gets的所以一般用cin.getline() len = strlen(str); // 这些都是小细节 将原来的内存释放 不仅节约内存 还防止内存泄漏 delete[] this->str; this->str = new char[len + 1]; strcpy(this->str, str); }