string实现
实现 string
2023-09-11 14:21:02 时间
class my_string{ friend ostream& operator<< (ostream&,my_string&); public: my_string():data(NULL) { } my_string(const char* str) { int n = strlen(str); data = new char[n+1]; strcpy(data,str); } my_string(const my_string &other):data(NULL) { *this = other; } my_string& operator=( const my_string& other) { if(this == &other) return *this; delete []data; int n = other.size(); data = new char[n+1]; strcpy(data,other.data); return *this; } my_string operator+(const my_string& other)const { int n = strlen(data); int m = strlen(other.data); my_string newstr; newstr.data = new char[m+n+1]; for(int i = 0; i < n; i++) newstr[i] = data[i]; for(int i = n; i < n+m; i++) newstr[i] = other.data[i-n]; newstr[m+n] = NULL; return newstr; } bool operator==(const my_string& other) { return !strcmp(data,other.data); } char& operator[](int pos) { assert(pos < size()); return data[pos]; } int size()const { return strlen(data); } ~my_string() { delete[] data; } private: char *data; }; ostream& operator<<(ostream& os,my_string& str) { os << str.data; return os; }
相关文章
- 文本文件合并(C++实现)
- 实现string类的操作符重载 + = > < == != >> <<
- 谷歌地图控件,通过扩展实现GMAP的自定义绘图系统
- Java实现 LeetCode 699 掉落的方块(线段树?)
- java实现拍7游戏
- java实现第三届蓝桥杯星期几
- Java实现 蓝桥杯VIP 算法训练 最长字符串
- Java实现 洛谷 P1049 装箱问题
- 实现注销 关机 重启计算机
- Pytorch之CNN:从代码认知CNN经典架构—基于Pytorch框架的代码实现经典轻量化卷积神经网络的算法集合(SqueezeNet/MobileNet3/ShuffleNet)
- Python不用理解进程,线程实现多任务就是这么简单
- LeetCode【8】. String to Integer (atoi) --java实现
- 【华为机试Python实现】HJ10 字符个数统计(简单)
- C++ string实现大数相加
- 技术分享 | 接口自动化测试,如何实现多套环境的自动化测试?