![C++语言string类的实现(完整源代码)_第1页](http://file3.renrendoc.com/fileroot_temp3/2021-12/26/e3392505-5109-43e3-a36b-216070fed2d8/e3392505-5109-43e3-a36b-216070fed2d81.gif)
![C++语言string类的实现(完整源代码)_第2页](http://file3.renrendoc.com/fileroot_temp3/2021-12/26/e3392505-5109-43e3-a36b-216070fed2d8/e3392505-5109-43e3-a36b-216070fed2d82.gif)
![C++语言string类的实现(完整源代码)_第3页](http://file3.renrendoc.com/fileroot_temp3/2021-12/26/e3392505-5109-43e3-a36b-216070fed2d8/e3392505-5109-43e3-a36b-216070fed2d83.gif)
![C++语言string类的实现(完整源代码)_第4页](http://file3.renrendoc.com/fileroot_temp3/2021-12/26/e3392505-5109-43e3-a36b-216070fed2d8/e3392505-5109-43e3-a36b-216070fed2d84.gif)
![C++语言string类的实现(完整源代码)_第5页](http://file3.renrendoc.com/fileroot_temp3/2021-12/26/e3392505-5109-43e3-a36b-216070fed2d8/e3392505-5109-43e3-a36b-216070fed2d85.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、MyString类主整源代码MyString. h 文件丄">'“ >kAx7"."7">7" >彳J哼.十.十.哼Jr* *T吟、吟、*r* 斤哼.T*/Copyright (c) 2013 道合 | Sameidea 1 com All rights reserved.K %丄. 丄. 丄.、丄"=".、.7丁IJ"1 .J*7 * T* bj'J*|g、*7i *g "4、,,.、 J、.、 :、,.、 j、*.*T *Tttifndef MYSTRING d
2、efine MYSTRING_H include <iostream> using namespace std;class MyString public:构造函数MyStringO;MyString(const MyString&);MyString (const char*):MyString(const size_t, const char);析构函数"MyString ();属性size_t length() :/字符串的长度bool empty () ;/字符串是否为空const char* c_str () :/返回C风格字符串的指针读写操作符frie
3、nd ostream& operator<< (ostream&, const MyString&);friend istream& operator>> (istream&, MyString&);/'+'操作符friend MyString operator+(const MyString&, const MyString&);比较操作符friend friend friend friend friend friendbool bool bool bool bool booloperat
4、or=(const MyString&, const MyString&); operator! = (const MyString&, const MyString&); operator<(const MyString&, const MyString&); operator<= (const MyString&, const MyString&); operator> (const MyString&, const MyString&); operator>=(const MyStri
5、ng&, const MyString&);下标操作符char& operator (const size_t);const char& operator (const size_t)const:赋值操作符MyString& operator二(const MyString&): '+J操作符MyString& operator+二(const MyString&);子串操作MyString substr(size_t, size_t);添加操作MyString& append (const MyString&am
6、p;);/插入操作MyString& insert(size_t, const MyString&);替换操作一MyString& assign (const MyString&,size_t, size_t);删除操作MyString& erase(size_t, size_t):private:size_t strLength; char* p_str;; _ ttendifMyString. cpp 文件I" I"V"I" xtx I" I" I" I" mL#刁.T*
7、 "7*鸣."T* 哼."T* 略."A"卜"7/Copyright (c) 2013 道合 | Sameidea 1. com All rights reserved.£“ " 匕* "" "】". .丄"丄* '“ J"丄*J.、' 丄* .丄* J" J.*1* Y" > :* " >吟J#include '"MyString h" ttinclude <ca
8、ssert>MyString:MyString():strLength(0), p_str(NULL)MyString::MyString(const MyString& str) strLength = str strLength; p_str = new charstrLength+1; strcpy(p_str, str p_str); 一 一MyString::MyString(const char* str) 辻(str二二NULL)return;strLength = strlen(str); p_str = new charstrLength+1: strcpy(p
9、_str, str);MyString::MyString (const size_t len,const char ch)strLength = len;p_str = new charstrLength+1;*(P_str+strLength)二'0 ; strset(p_str, ch); 一MyString: :'MyStringOdeletej p_str; "size_t MyString: length ()"return strLength;bool MyString:empty()return strLength0?true: false;
10、const char* MyString:c_str() "return p_str; /输出操作符ostream& operator<< (ostream& out,const MyString& str) 辻(str. p_str! =NULL) out«str p_str;return out:/输入操作符istream& operator>>(istream& in, MyString& str) /char tempLlOO ;/临时字符串数组 if(in>>temp)delete
11、 str p_str;str.strLength 二 strlen(temp):strp_str = new charstr.strLength+1; strcpy(str p_str, temp); return in;下标操作符char& MyString::operator (const size_t index) "assert (index>=0 && index<=strLength); return p_strindex; 一下标操作符(const情况) const char& MyString::operator (cons
12、t size_t index)const "assert(index>=0 && index<=strLength); return p_strindex: "/r +'操作符的巫载MyString operator+ (const MyString& lhs,const MyString& rhs) MyString ret;ret. strLength 二 lhs strLength + rhs. strLength;ret. p_str = new charret. strLength+1: strcpy(ret.
13、p_str, lhs p_str);strcat(ret. p_str, rhs p_str);return ret:赋值操作符MyString& MyString::operator=(const MyString& str)if (this!=&str)if(strLength<str strLength) delete p_str;p_str = new charstrstrLength+1; "strLength = str strLength; strcpy(p_str, str p_str); 一 一return *this:/r v操作符M
14、yString& MyString:operator+= (const MyString& str)if(this = &str)MyString copy(str); return *this+二copy;strLength += str.strLength;char* p_old = p_str; p_str = new charstrLength+1: strcpy(p_str, p_old); delete p_old; strcat(p_str, str p_str); return *this;比较操作符bool operator=(const MyStri
15、ng& lhs, const MyString& rhs) return strcmp (lhs. p_str, rhs. p_str)二二0; " "bool operator! = (const MyString& lhs, const MyString& rhs) return strcmp(lhs. p_str, rhs. p_str)!=0; " "bool operator<(const MyString& lhs, const MyString& rhs)return strcmp(lh
16、s. p_str, rhs. p_str)<0; " "bool operator<=(const MyString& lhs, const MyString& rhs) return strcmp(lhs. p_str, rhs p_str)<=0; 一 "bool operator>(const MyString& lhs, const MyString& rhs)return strcmp(lhs. p_str, rhs. p_str)>0; 一 "bool operator>=(
17、const MyString& lhs, const MyString& rhs) return strcmp(lhs. p_str, rhs. p_str) >=0; " 子串操作返回一个MyString类型的字符串,它包含源字符串中从下标pos开始的n个 字符MyString MyString::substr(size_t pos, size_t n) "assert(pos+n二strLength);MyString ret;ret.strLength = n;ret. p_str = new charret. strLength+1;for (
18、size_t i=0;i!=n;+i)ret i = (*this) pos+i;ret n=,0'return ret:添加操作将一个字符串的副本添加到源字符串的木尾,同“+二”操作符类似MyString& MyString:append(const MyString& str)*this+=str;return *this;插入操作/在下标为pos的元素之前插入MyString对象str的副本MyString& MyString:insert(size_t pos,const MyString& str) "assert (pos<strLength);char* p_old = p_str;strLength +=str strLength;p_str = new charstrLength+1;for (size_t i=0;i!=pos;+i)*(p_str+i) = *(p_old+i);for(size_t i=pos;i!=str strLength+pos:+i)*(p_str+i) = *(strp_str+i-pos);for(size_t i二str strLength+pos;i!二strLength;+i)*(p_str+i) =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025购销合同管材模板
- 公司保洁服务合同
- 合作协议合同范本
- 2025商户网上银行服务合同
- 2025年华东师大版选择性必修一历史上册月考试卷
- 2025农村土地承包经营权流转合同(安徽省标准文本)
- 2025年沪科版选修3历史下册阶段测试试卷含答案
- 2025年外研版三年级起点选修历史上册阶段测试试卷含答案
- 2025年起重磁力设备项目申请报告模板
- 2025年自营批发服务项目规划申请报告
- 点亮生命-大学生职业生涯发展与就业指导全套教学课件
- 旅居管家策划方案
- 车间消防安全知识培训课件
- 华为经营管理-华为的研发管理(6版)
- 锂离子电池生产工艺流程图
- 平衡计分卡-化战略为行动
- 幼儿园小班下学期期末家长会PPT模板
- 矿山安全培训课件-地下矿山开采安全技术
- GB/T 6417.1-2005金属熔化焊接头缺欠分类及说明
- 《社会主义市场经济理论(第三版)》第七章社会主义市场经济规则论
- 《腰椎间盘突出》课件
评论
0/150
提交评论