String类的部分实现
来源:互联网 发布:杜老师的c编程密码 编辑:程序博客网 时间:2024/06/07 05:11
#include<string.h>#include<iostream>#include<Windows.h>using namespace std;class String{char *str1;public:String(){}String (const char *s){str1 = new char[strlen(s) + 1];strcpy(str1, s);}String(const String &y){str1 = new char[strlen(y.str1) + 1];strcpy(str1, y.str1);}~String(){delete []str1;str1=NULL;}String& operator = (const String &y){if(strcmp(str1,y.str1)==0)return *this;delete[]str1;str1 = new char[strlen(y.str1) + 1];strcpy(str1, y.str1);return *this;}String operator +(String &y){String temp;temp.str1 = new char[strlen(str1) + strlen(y.str1) + 1];strcat(temp.str1,str1);strcat(temp.str1, y.str1);return temp;}friend ostream& operator<<(ostream &out,const String &y);friend istream& operator>>(istream &in,const String &y);char operator[](size_t i){return str1[i];}};ostream& operator<<(ostream &out,const String &y){out<<y.str1;return out;}istream& operator>>(istream &in,const String& y){in>>y.str1;return in;}int main(){String st1("world");String st2("hello");String st3;st3=st2+st1;//cin>>st1;cout<<st3<<endl;cout<<st2[2]<<endl;cout << st1 << endl;cout << st2 << endl;//return 0;system("pause");}
阅读全文
0 0
- String类的部分实现
- String类的部分实现
- string的部分实现
- 关于String类的部分实现
- String类的部分实现和测试
- JAVA中的String类的部分方法
- 模拟实现部分string函数
- string.h中库函数的实现代码(部分)
- string类的实现
- String类的实现
- String类的实现
- String类的实现
- String 类的实现
- String类的实现
- string类的实现
- String类的实现
- String类的实现
- string类的实现
- 第02课 04 JS中级课程-表单操作-4
- 34. Search for a Range
- 为什么小城市没有牛逼的研发团队
- 字符串
- XML解析(Dom4j)
- String类的部分实现
- Java中的copyOnWrite容器
- Webapi跨域
- IP与数字的转换
- 《大话设计模式》学习笔记——part1
- select、poll
- DATE_GetFirday
- iOS 重力感应之箭头指向重力方向
- 字符设备驱动--LED驱动程序