自定义的精简版String类,重载了各种字符串的运算符操作
来源:互联网 发布:python字典转换为列表 编辑:程序博客网 时间:2024/05/17 03:27
最近几天温习了字符串的相关知识,比较了C语言的字符串操作以及C++的string类字符串的操作,个人觉得C++的string类字符串操作方便很多。至少不用太在意字符串的内存分配,而且它还有比较方便的字符串搜索、插入等方法,便于对字符串的各种操作。出于学习巩固知识的目的,自己写了一个精简版的String类,这个类只是重载了几种简单的字符串运算操作符,还没有写搜索、插入等方法。以后再慢慢完善!
//自定义版string类#include <iostream>using namespace std;class String {public: String();//默认构造函数用于创建空字符串 ~String();//析构函数,如若创建临时变量时,会有很重要的意义 String(const String &rs);//拷贝构造函数 String(const char* const ch);//参数为数组字符串的构造函数 char &operator[](unsigned short int length);//下标操作符,由于不可能为负数,所以参数限定为ushort型 char operator[](unsigned short int length)const;//cons修饰函数体,一来作为下标操作函数的重载,二来为了针对const对象的操作 String &operator=(const String &s);//赋值运算符函数用于String对象之间的赋值String &operator=(const char* const ch);//常量字符串赋值运算函数 String operator+(const String &);//重载+运算符,实现String类对象的相加操作String operator+(const char* const ch); void operator+=(const String &rs);//重载+=运算符,实现String类对象的相加并重新赋值操作void operator+=(const char* const ch);//重载<<操作符,由于<<为二元运算符,若写成String类的成员函数的话,它的参数只能有一个,所以,这里写成友元函数,这样就可以传入两个参数//cout<<s2等价于operator<<(cout,s2); friend ostream &operator<<(ostream &o, const String &str) { o<<str.str;return o; } friend istream &operator>>(istream &i, String &str) { i>>str.str; return i; } friend bool operator<(const String &str1, const String &str2) {if(strcmp(str1.str, str2.str)<0)return 1;elsereturn 0; } friend bool operator>(const String &str1, const String &str2) {if(strcmp(str1.str, str2.str)>0)return 1;elsereturn 0; } friend bool operator==(const String &str1, const String &str2) {if(strcmp(str1.str, str2.str) == 0)return 1;elsereturn 0; } unsigned short int getlen()const{return len;} const char*getstr()const{return str;}private: String(unsigned short int);//不对外提供此构造函数的接口 unsigned short int len; char *str;};String::String(unsigned short int length){cout<<"整型变量为参数的构造函数"<<endl; str = new char[length + 1]; int i; for(i=0; i<=length; i++){ str[i] = '\0';} len = length;}String::String(){cout<<"默认构造函数"<<endl; len = 0; str = new char[1]; str[0] = '\0';}String::~String(){cout<<"String类析构函数"<<endl; delete []str; len = 0;}String::String(const String &rs){cout<<"拷贝构造函数"<<endl; len = rs.getlen(); str = new char[len + 1]; for(int i=0; i<len; i++){ str[i] = rs[i];} str[len] = '\0';}String::String(const char* const ch){cout<<"常量字符串为参数的构造函数"<<endl; len = strlen(ch); str = new char[len+1]; for(int i=0; i<len; i++) str[i] = ch[i]; str[len] = '\0';}char &String::operator[](unsigned short int length){cout<<"String对象【】运算符"<<endl; if(length>len)//限制数组越界操作 return str[len-1]; else return str[length];}char String::operator[](unsigned short int length)const{cout<<"const String对象【】运算符"<<endl; if (length>len)//限制数组越界操作 { return str[len-1]; } else return str[length];}String &String::operator=(const String&s){cout<<"String对象=运算符"<<endl; if (this == &s) return *this;if(str)delete[]str; len = s.getlen(); str = new char[len+1]; for(int i=0; i<len; i++) { str[i] = s[i];//这里是对String类对象进行下标操作,所以要在String类中重载下标操作符,注意这里还是const对象操作 } str[len] = '\0'; return *this;}String &String::operator=(const char* const ch){cout<<"常量字符串=运算符"<<endl;if(str)delete []str;len = strlen(ch);str = new char[len + 1];for(int i=0; i<len; i++){str[i] = ch[i];}str[len] = '\0';return *this;}String String::operator+(const String &rs){cout<<"String对象+运算符"<<endl; int total = len + rs.getlen(); String temp(total); int i,j; for( i=0; i<len; i++) temp[i] = str[i]; for( j=0; j<rs.getlen(); j++,i++) temp[i] = rs[j]; temp[total] = '\0'; return temp;//值传递,创建临时变量,temp对象析构掉}String String::operator+(const char* const ch){cout<<"常量字符串+运算符"<<endl;int total = len + strlen(ch);String temp(total);int i,j;for( i=0; i<len; i++)temp[i]=str[i];for( j=0; j<strlen(ch); j++,i++)temp[i] = ch[j];temp[total] = '\0';return temp;//值传递,创建临时变量,temp对象析构掉}void String::operator+=(const String &rs)//如果是String对象与常量字符串相加,这里会调用构造函数{cout<<"String对象+="<<endl; int total = len + rs.getlen(); String temp(total); int i,j; for(i=0;i<len;i++){ temp[i] = str[i];} for(j=0; j<rs.getlen(); j++,i++){temp[i] = rs[j];} temp[total] = '\0'; *this = temp;}void String::operator+=(const char* const ch){cout<<"常量字符串+="<<endl;int total = len + strlen(ch);String temp(total);//这里需要调用以整型变量为参数的构造函数int i,j;for(i=0;i<len;i++){temp[i] = str[i];}for(j=0; j<strlen(ch); j++,i++){temp[i] = ch[j];}temp[total] = '\0';*this = temp;}int main(){String s1; cout<<"s1的长度:"<<s1.getlen()<<endl; char*temp="help me"; s1 = temp;//调用常量字符串赋值函数 cout<<"s1:"<<s1.getstr()<<"\t s1的长度:"<<s1.getlen()<<endl; char ch[10]; strcpy(ch,"all right"); s1 += ch; cout<<"ch:\t"<<ch<<endl; cout<<"s1:\t"<<s1.getstr()<<endl; cout<<"s1:"<<s1<<endl; s1[2]='o'; cout<<"s1:"<<s1<<endl; cout<<"s1[999]:"<<s1[999]<<endl; String s2 = "mother"; String s3("Mother"); cout<<"s2:"<<s2<<"\t s3:"<<s3<<endl; String s4 = s2+s3;//拷贝构造函数 cout<<"s2+s3="<<s4<<endl;String s5 = "I love ";String s6 = s5 + "bupt";cout<<"s6:"<<s6<<endl; int check = s2>s3; cout<<"s2>s3:"<<check<<endl; check = s2<s3; cout<<"s2<s3:"<<check<<endl; check = s2 == s2; cout<<"s2==s2:"<<check<<endl; cin>>s2[0]>>s3[0]; s2 = s2 + s3; cout<<"s2:"<<"\t"<<s2<<endl;system("pause"); return 0;}
- 自定义的精简版String类,重载了各种字符串的运算符操作
- 重载运算符实现精简版string类
- 重载运算符实现精简版string类
- [c++]String字符串类的运算符重载
- 类string运算符的重载
- string类的运算符重载
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- String 类的编写:1.各种运算符的重载 2.用引用计数来实现String类
- 字符串的运算符重载
- 字符串的运算符重载
- 各种操作符的重载
- 字符串类改进版(加入了运算符重载)
- c++string类的操作符重载
- 有关String 字符串的各种操作大全
- 设计一个string类,通过重载“+=”运算符,实现对两个string类对象m1,m2的操作。
- 第八周--运算符重载-String类的构造
- 第十二周项目四 string类的运算符重载
- 【转】【复】string类的运算符重载
- 2013年大众点评网的一道程序笔试题
- 数据结构与算法之2-19 找出主要元素
- mac 不支持ntfs的写
- [精通WindowsSocket网络开发-基于VC++实现]第三章——WindowsSockets基础—概念
- 控制文件恢复与备份
- 自定义的精简版String类,重载了各种字符串的运算符操作
- 组件为什么需要注册?
- HDU4302(map的用法)
- 三种线性排序算法 计数排序、桶排序与基数排序
- 死锁的形成场景与死锁提示时机分析
- VC获取当前文件路径---比较
- Block编程值得注意的那些事儿
- SQL数据类型
- C语言第十二堂作业