首页 C++语言string类的实现(完整源代码)

C++语言string类的实现(完整源代码)

举报
开通vip

C++语言string类的实现(完整源代码) MyString 类完整源代码 MyString.h 文件 /*********************************************************** //Copyright (c) 2013 道合|SameIdeal.com All rights reserved. ************************************************************/ #ifndef MYSTRING_H #define MYSTRING...

C++语言string类的实现(完整源代码)
MyString 类完整源代码 MyString.h 文件 /*********************************************************** //Copyright (c) 2013 道合|SameIdeal.com All rights reserved. ************************************************************/ #ifndef MYSTRING_H #define MYSTRING_H #include using namespace std; class MyString { public: //构造函数 MyString(); MyString(const MyString&); MyString(const char*); MyString(const size_t,const char); //析构函数 ~MyString(); //属性 size_t length();//字符串的长度 bool empty();//字符串是否为空 const char* c_str();//返回 C风格字符串的指针 //读写操作符 friend ostream& operator<< (ostream&,const MyString&); friend istream& operator>> (istream&,MyString&); //‘+’操作符 friend MyString operator+(const MyString&,const MyString&); //比较操作符 friend bool operator==(const MyString&,const MyString&); friend bool operator!=(const MyString&,const MyString&); friend bool operator<(const MyString&,const MyString&); friend bool operator<=(const MyString&,const MyString&); friend bool operator>(const MyString&,const MyString&); friend bool operator>=(const MyString&,const MyString&); //下标操作符 char& operator[] (const size_t); const char& operator[] (const size_t)const; //赋值操作符 MyString& operator=(const MyString&); //'+='操作符 MyString& operator+=(const MyString&); //子串操作 MyString substr(size_t,size_t); //添加操作 MyString& append(const MyString&); //插入操作 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; }; #endif MyString.cpp 文件 /*********************************************************** //Copyright (c) 2013 道合|SameIdeal.com All rights reserved. ************************************************************/ #include "MyString.h" #include MyString::MyString():strLength(0),p_str(NULL){} MyString::MyString(const MyString& str) { strLength = str.strLength; p_str = new char[strLength+1]; strcpy(p_str,str.p_str); } MyString::MyString(const char* str) { if(str==NULL) return; strLength = strlen(str); p_str = new char[strLength+1]; strcpy(p_str,str); } MyString::MyString(const size_t len,const char ch) { strLength = len; p_str = new char[strLength+1]; *(p_str+strLength)='\0'; strset(p_str,ch); } MyString::~MyString() { delete[] p_str; } size_t MyString::length() { return strLength; } bool MyString::empty() { return strLength==0?true:false; } const char* MyString::c_str() { return p_str; } //输出操作符 ostream& operator<< (ostream& out,const MyString& str) { if(str.p_str!=NULL) out<>(istream& in,MyString& str)// { char temp[100];//临时字符串数组 if(in>>temp) { delete[] str.p_str; str.strLength = strlen(temp); str.p_str = new char[str.strLength+1]; strcpy(str.p_str,temp); } return in; } //下标操作符 char& MyString::operator [](const size_t index) { assert(index>=0 && index<=strLength); return p_str[index]; } //下标操作符(const 情况) const char& MyString::operator [](const size_t index)const { assert(index>=0 && index<=strLength); return p_str[index]; } //'+'操作符的重载 MyString operator+(const MyString& lhs,const MyString& rhs) { MyString ret; ret.strLength = lhs.strLength + rhs.strLength; ret.p_str = new char[ret.strLength+1]; strcpy(ret.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 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 在 道合|sameideal.com 欢迎大家访问我的独立技术博客~
本文档为【C++语言string类的实现(完整源代码)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_142418
暂无简介~
格式:pdf
大小:138KB
软件:PDF阅读器
页数:7
分类:互联网
上传时间:2013-06-23
浏览量:20