String 类的编写:1.各种运算符的重载 2.用引用计数来实现String类
2017-02-14 18:26
393 查看
1.各种运算符的重载
2.String 类构造函数,拷贝构造函数,析构函数的调用
3.引用计数模型的String 类
/*1.String 类的编写(主要是写运算符+ += - > < >= <=的重载): #include<iostream> using namespace std; class String; ostream& operator<<(ostream &out,const String &s); class String { friend ostream& operator<<(ostream &out,const String &s); public: String(const char *str="") { if(str==NULL) { data=new char[1]; data[0]='\0'; } else { data=new char[strlen(str)+1]; strcpy(data,str); } } String(const String &s) { data=new char(strlen(s.data)+1); strcpy(data,s.data); } String& operator=(const String &s)//深赋值 { if(this!=&s) { delete[]data;//1.释放原有空间 data=new char[strlen(s.data)+1];//2.开辟新空间 strcpy(data,s.data);//3.进行拷贝 } return *this;//4.返回*this } ~String() { delete[]data; data=NULL; } public: String operator+(const String &s) { int len; char *str=NULL; len=strlen(data)+strlen(s.data)+1; str=new char[len]; strcpy(str,data); strcat(str,s.data); return String(str); } /*String operator-(const String &s) { int i=0; int len; char *str1,*str2; char *str=NULL; str1=data; str2=s.data; len=strlen(data)-strlen(s.data)+1; str=new char[len]; while(*str1!='\0') { if(*str1==*str2) { str1++; str2++; } else { str[i++]=*str1; str1++; } } str[len-1]='\0';//必须写,如果不写后面就是乱码 return String(str); } String& operator+=(const String &s) { char *str; str=new char[strlen(data)+strlen(s.data)+1]; strcpy(str,data); delete []data; strcat(str,s.data); data=str; return *this; } bool operator>(const String &s) { int n1=strlen(data); int n2=strlen(s.data); if(n1>n2) return true; else if(n1<n2) return false; else { char *str1=data; char *str2=s.data; while(*str1!='\0'&&*str2!='\0') { if(*str1>*str2) { return true; } str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return false; } } bool operator<(const String &s) { int n1=strlen(data); int n2=strlen(s.data); if(n1<n2) return true; else if(n1>n2) return false; else { char *str1=data; char *str2=s.data; while(*str1!='\0'&&*str2!='\0') { if(*str1<*str2) { return true; } str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return false; } } bool operator>=(const String &s) { int n1=strlen(data); int n2=strlen(s.data); if(n1>n2) return true; else if(n1<n2) return false; else { char *str1=data; char *str2=s.data; while(*str1!='\0'&&*str2!='\0'&&*str1==*str2) { str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return true; else return false; } } bool operator<=(const String &s) { int n1=strlen(data); int n2=strlen(s.data); if(n1<n2) return true; else if(n1>n2) return false; else { char *str1=data; char *str2=s.data; while(*str1!='\0'&&*str2!='\0'&&*str1==*str2) { str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return true; else return false; } } bool operator==(const String &s) { char *str1=data; char *str2=s.data; while(*str1!=NULL&&*str2!=NULL&&*str1==*str2) { str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return true; else return false; } bool operator!=(const String &s) { char *str1=data; char *str2=s.data; while(*str1!='\0'&&*str2!='\0'&&*str1==*str2) { str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return false; else return true; } private: char *data; }; int main() { //String s1("abc"); //String s2("123"); //cout<<"s1+s2="<<s1+s2; String s1("eefe"); String s2("fefe"); /*if(s1!=s2) { cout<<"不相等"<<endl; } else { cout<<"相等"<<endl; }*/ /* if(s1==s2) { cout<<"相等"<<endl; } else { cout<<"不相等"<<endl; } if(s1>s2) { cout<<"大于"<<endl; } else { cout<<"不大于"<<endl; }*/ //cout<<"s1-s2="<<s1-s2; /* if(s1<s2) { cout<<"小于"<<endl; } else { cout<<"不小于"<<endl; }*/ /* if(s1>=s2) { cout<<"符合大于等于"<<endl; } else cout<<"不符合大于等于"<<endl; return 0;*/ /* if(s1<=s2) { cout<<"符合小于等于"<<endl; } else { cout<<"不符合小于等于"<<endl; } s1+=s2; cout<<s1<<endl; } ostream& operator<<(ostream &out,const String &s) { out<<s.data; return out; }
2.String 类构造函数,拷贝构造函数,析构函数的调用
#include<iostream> using namespace std; class String; ostream& operator<<(ostream& out,const String &s); class String { friend ostream& operator<<(ostream& out,const String &s); public: String(const char *str="") { if(str==NULL) { data=new char[1]; data[0]='\0'; } else { data=new char[strlen(str)+1]; strcpy(data,str); } cout<<"Create String"<<endl; } String(const String &s) { data=new char[strlen(s.data)+1]; strcpy(data,s.data); cout<<"Copy String"<<endl; } String& operator=(const String &s) { if(this!=&s) { delete[]data;//深赋值 data=new char[strlen(s.data)+1]; strcpy(data,s.data); } cout<<"assign String"<<endl; return *this; } ~String() { delete[]data; data=NULL; cout<<"delete String"<<endl; } private: char *data; }; ostream& operator<<(ostream &out,const String &s) { out<<s.data; return out; } void main() { String s1("abc");//Create String delete String //String s2=s1;//Create String Copy String(调拷贝构造函数) delete String delete String String s2; s2=s1;//Create String Create String assign String delete String delete String(深赋值) }
3.引用计数模型的String 类
#include<iostream> using namespace std; class String; ostream& operator<<(ostream& out,String &s); class String_rep { friend class String; friend ostream& operator<<(ostream& out,String &s); public: String_rep(const char *str=""):use_count(0) { if(str==NULL) { data=new char[1]; data[0]='\0'; } else { data=new char[strlen(str)+1]; strcpy(data,str); } } String_rep(const String_rep &s); String_rep& operator=(const String_rep &s); ~String_rep() { delete[]data; data=NULL; } public: void increment() { ++use_count; } void decrement() { if(--use_count==0) { delete this; } } private: char *data; int use_count; }; ostream& operator<<(ostream& out,String &s); class String { friend ostream& operator<<(ostream& out,String &s); public: String(const char *str=""):rep(new String_rep(str)) { rep->increment(); } String(const String &s) { rep=s.rep; rep->increment(); } String& operator=(const String &s) { if(this!=&s) { rep->decrement(); rep=s.rep; rep->increment(); } return *this; } ~String() { rep->decrement(); } public: void upper()//转换大小写函数 { String_rep *new_rep=new String_rep(rep->data); rep->decrement(); rep=new_rep; new_rep->increment(); char *p=rep->data; while(*p!='\0') { *p-=32; ++p; } } private: String_rep *rep; }; void main() { String s1("abc"); String s2=s1; String s3; s3=s2; s1.upper(); cout<<"s1="<<s1<<endl; cout<<"s2="<<s2<<endl; cout<<"s3="<<s3<<endl; } ostream& operator<<(ostream& out,String &s) { out<<s.rep->data; return out; }
相关文章推荐
- spring学习随笔2
- python 爬虫初识 ,不断更新中
- Android6.0 CTS测试环境搭建
- Android 名企面试题及涉及知识点整理
- jquery查找元素,绑定事件,DOM操作
- volley框架的解读
- iOS开发中的https(二):https服务器的简单用例
- Android开发——数据持久存储_SQLite数据库
- 两个整数变量的交换
- android activity的windowSoftInputMode属性
- S2_二本第三章使用集合组织相关数据(泛型)
- ViewPager写的三字经程序,外加三字经朗读,用了MediaPlayer
- ranong项目总结-HttpClient-HttpDelete支持HttpEntity
- Redis的订阅发布机制
- 进阶技能收集
- Android CountDownTimer学习使用
- SQL中的各种Join
- 昨日总结说明
- 简单理解回调
- 如何对网站的文件和资源进行优化