C++ 写自己的string类(作风::懒)
2016-02-26 23:22
323 查看
#include <iostream> #include<cstdlib> #include <conio.h> using namespace std; class Cstring { private: unsigned int len; char *st; explicit Cstring(const unsigned int i) { len = 0; st = new char[i+1]; return; } public: ~Cstring() { delete []st; st = 0; } explicit Cstring() { len = 0; st = new char[len+1]; st[0] = '\0'; } Cstring(const Cstring &s) { len = s.len; st = new char[len + 1]; for ( int i = 0; i < len; i++) { st[i] = s[i]; } st[len] = '\0'; return; } explicit Cstring(const char *const ch) { len = strlen(ch); st = new char[len + 1]; for ( int i = 0; i < len; i++) { st[i] = ch[i]; } st[len] = '\0'; return; } explicit Cstring(const char ch) { len = 1; st = new char[len + 1]; st[0] = ch; st[1] = '\0'; return; } char &operator[](unsigned int i)const { if (i > len - 1) { return st[len - 1]; } else return st[i]; } char &operator[](unsigned int i) { if (i > len - 1) { return st[len - 1]; } else return st[i]; } Cstring &operator=(const char * const ch) { if (strcmp(st, ch) == 0) return *this; char *p = st; delete []p; p = 0; len = strlen(ch); st = new char[len + 1]; for (unsigned int i = 0; i < len; i++) { st[i] = ch[i]; } st[len] = '\0'; return *this; } Cstring &operator=(const Cstring &s) { if (this == &s) return *this; char *p = st; delete[]p; p = 0; len = s.len; st = new char[len + 1]; for ( int i = 0; i < len; i++) { st[i] = s[i]; } st[len] = '\0'; return *this; } const Cstring operator+(const Cstring &s) { unsigned int temlen = len + s.len; Cstring temp(temlen); for (unsigned int i = 0; i < len; i++) { temp.st[i] = st[i]; } for (unsigned int j = 0; j < s.len; j++) { temp.st[len + j] = s.st[j]; } temp.len = temlen; temp.st[temp.len] = '\0'; return temp; } const Cstring operator+(const char *const ch) { unsigned int templen = len + strlen(ch); Cstring temp(templen); for (unsigned int i = 0; i < len; i++) { temp.st[i] = st[i]; } for (unsigned int j = 0; j < strlen(ch); j++) { temp.st[len + j] = ch[j]; } temp.len = templen; temp.st[temp.len] = '\0'; return temp; } friend ostream&operator<<(ostream &o, const Cstring &s) { o << s.st; return o; } friend istream&operator>>(istream &in, Cstring &s) { char ch; int i = 0; while ((ch = cin.get())!='\n') { if (!i) { delete[]s.st; s.st = 0; s.len = i+1; s.st = (char*)realloc(s.st, (s.len+1)*sizeof(char)); s.st[i] = ch; s.st[i+1] = '\0'; i++; } else { s.len = i + 1; s.st = (char*)realloc(s.st, (s.len + 1)*sizeof(char)); s.st[i] = ch; s.st[i + 1] = '\0'; i++; } } return in; } }; int main() { Cstring s; s = "123333333"; s[4] = '1'; Cstring ss(s); cout << ss << endl; system("pause"); return 0; }
相关文章推荐
- 学C++ 写面向对象贪吃蛇(作风一贯::懒)
- C++模板简单学习
- iOS开发系列--C语言之预处理
- Basic C++ Container classes summary(Array, Vector, List)
- 学C++第一天 面向对象写2杈
- iOS开发系列--C语言之存储方式和作用域
- 【转】C++11 标准新特性: 右值引用与转移语义
- 刚学C语言时理解性的写的2杈
- C++: char* 与 string类型转换
- protobuf、prototxt的使用/Windows/VS
- C语言中memset函数详解
- C++中explicit关键字的作用
- C++栈的初始化,入栈,出栈,获取栈顶元素等操作
- C 和 C++ 混合编程
- C++: string 中find函数的用法以及string::npos的含义
- C语言宏定义和宏定义函数
- C++实现普通队列,循环队列的基本操作(初始化,入队,出队,获取队列首元素等)
- 1065. A+B and C (64bit) (20)
- 线性表—顺序存储(C语言)
- C\C++中内存分几个区