C++ String类简单实现
来源:互联网 发布:csgo战绩查询软件 编辑:程序博客网 时间:2024/06/05 12:40
#ifndef MYSTRING_H#define MYSTRING_H#include<iostream>using namespace std;class MyString {/*重载<<*/ friend ostream& operator<<(ostream& _cout, MyString& _MyString) { _cout << _MyString.string_address; return _cout; }/*重载>>*/ friend istream& operator>>(istream& _cin, MyString& _MyString) { if (_MyString.string_address) { free(_MyString.string_address); _MyString.string_address = NULL; } _MyString.string_address = (char*)calloc(1, 1024); if (!_MyString.string_address) cout << "分配内存失败" << endl; _cin >> _MyString.string_address; return _cin; }public:/*默认无参构造*/ MyString() :string_address(NULL) { }/*默认有参构造*/ MyString(const char* const_string) { string_address = (char*)calloc(1, strlen(const_string) + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, const_string); }/*拷贝构造*/ MyString(const MyString& other_MyString) { string_address = (char*)calloc(1, strlen(other_MyString.string_address) + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, other_MyString.string_address); }/*析构函数*/ ~MyString() { if (string_address){ free(string_address); string_address = NULL; } }/*普通成员函数*/ const char* c_str() const { return string_address; }/*获取字符串长度*/ size_t length() { return strlen(string_address); } size_t size() { return strlen(string_address); }/*重载=*/ MyString& operator=(const MyString& _MyString) { if (string_address) { free(string_address); string_address = NULL; } string_address = (char*)calloc(1, strlen(_MyString.string_address) + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, _MyString.string_address); return *this; } MyString& operator=(const char* const_string) { if (string_address) { free(string_address); string_address = NULL; } string_address = (char*)calloc(1, strlen(const_string) + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, const_string); return *this; }/*重载[]*/ char& operator[](size_t index) { if (index >= length()) return string_address[length() - 1]; return string_address[index]; }/*重载+=*/ MyString& operator+=(MyString& _MyString) { if (string_address){ if (_MyString.string_address){ string_address = (char*)realloc(string_address, length() + strlen(_MyString.string_address) + 1); if (!string_address) cout << "内存分配失败" << endl; strcat(string_address, _MyString.string_address); } } else { if (_MyString.string_address){ string_address = (char*)calloc(1, strlen(_MyString.string_address + 1)); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, _MyString.string_address); } } return *this; } MyString& operator+=(const char* const_string) { if (string_address) { if (const_string) { string_address = (char*)realloc(string_address,length() + strlen(const_string) + 1); if (!string_address) cout << "内存分配失败" << endl; strcat(string_address, const_string); } } else { if (const_string) { string_address = (char*)calloc(1, strlen(const_string) + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, const_string); } } return *this; }/*重载+*/ MyString operator+(MyString& _MyString) { if (string_address) { if (_MyString.string_address) { string_address = (char*)realloc(string_address, length() + _MyString.length() + 1); if (!string_address) cout << "内存分配失败" << endl; strcat(string_address, _MyString.string_address); } } else { if (_MyString.string_address) { string_address = (char*)calloc(1, _MyString.length() + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, _MyString.string_address); } } return *this; } MyString operator+(const char* const_string) { if (string_address) { if (const_string) { string_address = (char*)realloc(string_address, length() + strlen(const_string) + 1); if (!string_address) cout << "内存分配失败" << endl; strcat(string_address, const_string); } } else { if (const_string) { string_address = (char*)calloc(1, strlen(const_string) + 1); if (!string_address) cout << "内存分配失败" << endl; strcpy(string_address, const_string); } } return *this; }/*重载 ==*/ bool operator==(MyString& _MyString) { if (string_address) if (_MyString.string_address) if (!strcmp(string_address, _MyString.string_address)) return true; else if (!_MyString.string_address) return true; return false; } bool operator==(const char* const_string) { if (string_address) if (const_string) if (!strcmp(string_address, const_string)) return true; else if (!const_string) return true; return false; }/*重载!=*/ bool operator!=(MyString& _MyString) { if (string_address) if (_MyString.string_address) if (strcmp(string_address, _MyString.string_address)) return true; else if (_MyString.string_address) return true; return false; } bool operator!=(const char* const_string) { if (string_address) if (const_string) if (strcmp(string_address, const_string)) return true; else if (const_string) return true; return false; }private: char* string_address;};#endif
阅读全文
1 0
- C++string类的简单实现
- c++string类的简单实现
- 实现简单String类
- String类简单实现
- 【C/C++】String类实现
- string类的简单实现
- string类的简单实现
- String 类的简单实现
- 简单的string类实现
- C++简单实现string类
- C++ string类简单实现
- string类的简单实现
- string 类的简单实现
- 简单string 类的实现
- 简单的String类实现
- string类的简单实现
- C++ String类简单实现
- String类的简单实现
- 程序员,如何从平庸走向理想?
- 6、this关键字
- 自律者方得自由
- jquery提交带上传文件的form表单
- Flume日志采集
- C++ String类简单实现
- New Document1
- Ubuntu下安装mysql
- eMP开发环境配置说明
- log4j2 升级问题 (包含log4j与slf4j版本)
- Exception之The valid characters are defined in RFC 7230 and RFC 3986
- 小心 Spring MVC 使用陷阱
- C++静态成员函数和静态数据成员
- firewalld防火墙的一些操作