MyString(重写String)
http://wenku.baidu.com/view/d7ac113243323968011c925b.html
已知类String的原型为:
- class String{
- public:
- String(const char *str = NULL); // 普通构造函数
- String(const String &other); // 拷贝构造函数
- ~ String(void); // 析构函数
- String & operator =(const String &other);// 赋值函数
- private:
- char *m_data; // 用于保存字符串
- };
请编写String的上述4个函数。
- /*
- ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30
- new不忘delete
- Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString):
- MyString();//构造
- MyString(const char* cString);//地址不能变
- char at(int index) const;//输入数组下标,返回字符
- int length() const;//长度
- void clear();//清空 len=0
- bool empty() const;//是否清空?len不变
- int compare(const MyString& s) const;//
- int compare(int index, int n, const MyString& s) const;
- void copy(char s[], int index, int n);
- char* data() const;
- int find(char ch) const;
- int find(char ch, int index) const;
- int find(const MyString& s, int index) const;
- */
- #include<iostream>
- #include<string.h>
- using namespace std;
- class MyString
- {
- public:
- MyString(void):a(NULL){cout << "MyString()" << endl;}
- MyString(const char* cString=NULL);//ordinary func
- MyString(const MyString &other);//copy func
- MyString & operator =(const MyString &other);//assign func
- //~MyString(void);
- char at(int index) const;//
- int length() const;
- void clear();
- bool empty() const;
- int compare(const MyString& s) const;
- int compare(int index, int n, const MyString& s) const;
- void copy(char s[], int index, int n);
- char* data() const;
- int find(char ch) const;
- int find(char ch, int index) const;
- int find(const MyString& s, int index) const;
- // MyString &operator+=(const MyString &other);
- //MyString operator +(const MyString &mystr);
- //MyString &operator +(const MyString &mystr);
- //重载为友元函数,重载"+"运算符(不改变被加对象) 注意返回引用不行
- friend MyString operator +(const MyString& mystr1,const MyString &mystr2);
- MyString &operator+=(const MyString &other);
- bool operator > (const MyString & another);
- bool operator < (const MyString & another);
- bool operator == (const MyString & another);
- char& operator[](int idx);
- void dis();
- friend ostream& operator<<(ostream& os,const MyString& other);
- ~MyString();
- private:
- char *a;
- int len;
- };
- //MyString::MyString(){
- // cout << "MyString()" << endl;
- //}
- MyString::~MyString(){
- cout<<"~MyString():delete..."<<endl;
- delete []a;
- }
- MyString::MyString(const char* cString)
- {
- if (cString==NULL)
- {
- a=new char[];
- a[]=;
- //int len=0;
- }
- else
- {
- len = strlen(cString);//new
- a=new char [len+];
- strcpy(a,cString);
- //len=strlen(cString);
- }
- }
- MyString::MyString(const MyString &other){
- int len = strlen(other.a);
- a = new char[len+];
- strcpy(a, other.a);
- }
- MyString &MyString::operator =(const MyString &other){
- //self check assign
- if(this == &other){
- return *this;
- }
- //释放原有的内存资源
- delete []a;
- //分配新的内存资源,并复制内容
- int len = strlen(other.a);
- a = new char[len+];
- strcpy(a, other.a);
- //返回本对象的引用
- return *this;
- }
- char MyString::at(int index) const
- {
- if(len==)
- {cout<<"no char in string"<<endl;
- return ;
- }
- if(index>len)
- {
- cout<<"the maximun array exceed"<<endl;
- return ;
- }
- else
- {
- return a[index-];
- }
- }
- int MyString::length() const
- {
- return len;
- }
- void MyString::clear()
- {
- if(!a)
- {
- delete []a;
- a=NULL;
- }
- len=;
- //a=" ";
- }
- bool MyString::empty() const
- {
- if(len==)
- return true;
- else
- return false;
- }
- int MyString::compare(const MyString& s) const
- {
- int m=this->len;int n=s.len;
- for(int i=;i<m&&i<n;i++)
- {
- if(s.a[i]==a[i])
- continue;
- else if(s.a[i]<a[i])
- return ;
- else
- return -;
- }
- return ;
- }
- int MyString::compare(int index, int n, const MyString& s) const
- {
- int m=len,k=s.len;
- if(index+n>m||index+n>k)
- {
- cout<<"cannot compare!"<<endl;
- ///I dont know how to solve it
- }
- for(int i=index-,j=;i<n+index;i++,j++)
- {
- if(s.a[j]==a[i])
- continue;
- else if(s.a[j]<a[i])
- return ;
- else
- return -;
- }
- return ;
- }
- void MyString::copy(char s[], int index, int n)
- {
- if(n>=len)
- {cout<<"the maximun array exceed"<<endl;}
- else if(n+index->len)
- {
- cout<<"the maximun array exceed"<<endl;
- return;
- }
- else
- {
- for(int i=n-,j=;i<n+index-;i++,j++)
- s[j]=a[i];
- }
- }
- char* MyString::data() const
- {
- return a;
- }
- int MyString::find(char ch) const
- {
- for(int i=;i<len;i++)
- {
- if(ch==a[i])
- return i+;
- }
- return ;
- }
- int MyString::find(char ch, int index) const
- {
- if(index>len||index<)
- {
- cout<<"the index num is should be 1~ "<<len<<endl;
- return ;
- }
- for(int i=index-;i<len;i++)
- {
- if(ch==a[i])
- return i+;
- }
- return -;
- }
- int MyString::find(const MyString& s, int index) const
- {
- if(index>s.len||index<)
- {
- cout<<"the index num is should be 1~ "<<s.len<<endl;
- return ;
- }
- char ch=s.a[index-];
- for(int i=;i<len;i++)
- {
- if(ch==a[i])
- return i+;
- }
- return ;
- }
- // 加法运算符重载
- //MyString MyString::operator +(const MyString & another){
- // int len = strlen(this->a) + strlen(another.a);
- // MyString str("");
- // delete []str.a;
- // str.a = new char[len + 1];
- // memset(str.a,0,len + 1);
- // //int len1 = strlen(this->a) + 1;
- // //strcat_s(str.a, len1, this->a);
- // strcat(str.a, this->a);
- // // 源串长度 + 目标串长度 + 结束符
- // //int len2 = strlen(this->a) + strlen(another.a) + 1;
- // //strcat_s(str.a,len2, another.a);
- // strcat(str.a, another.a);
- // return str;
- //}
- //MyString MyString::operator +(const MyString &mystr){//mem leak
- // MyString newString("");
- // if (!mystr.a)
- // newString = *this;
- // else if (!a)
- // newString = mystr;
- // else {
- // newString.a = new char[strlen(a) + strlen(mystr.a) + 1];
- // strcpy(newString.a, a);
- // strcat(newString.a, mystr.a);
- // }
- // return newString;
- //}
- //重载"+"运算符(不改变被加对象)
- //MyString &MyString::operator +(const MyString &mystr){
- // MyString *temp=new MyString("");
- // temp->a=new char[strlen(a)+strlen(mystr.a)+1];
- // strcpy(temp->a,a);
- // strcat(temp->a,mystr.a);
- // return *temp;
- //}
- /*
- //重载"+"运算符(改变被加对象)
- Mystring &operator +(Mystring &mystr){
- char *temp=str;
- str=new char[strlen(temp)+strlen(mystr.str)+1];
- strcpy(str,temp);
- strcat(str,mystr.str);
- delete [] temp;
- return *this;
- }
- */
- //friend
- MyString operator +(const MyString& mystr1,const MyString &mystr2){
- MyString str("");
- delete[] str.a;
- str.len = strlen(mystr1.a)+strlen(mystr2.a);
- str.a = new char[str.len + ];
- strcpy(str.a, mystr1.a);
- strcat(str.a, mystr2.a);
- return str;
- // //MyString *temp=new MyString("");//mem leak
- // MyString temp("");
- // temp.a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
- // //temp->a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
- // strcpy(temp.a,mystr1.a);
- // strcat(temp.a,mystr2.a);
- // return temp;
- }
- // ==关系运算符重载
- bool MyString::operator==(const MyString &other)
- {
- if(strcmp(this->a,other.a) == )
- return true;
- else
- return false;
- }
- // >关系运算符重载
- bool MyString::operator>(const MyString &other)
- {
- if(strcmp(this->a,other.a) > )
- return true;
- else
- return false;
- }
- // <运算符重载
- bool MyString::operator<(const MyString &other)
- {
- if(strcmp(this->a,other.a) < )
- return true;
- else
- return false;
- }
- // []运算符重载
- char& MyString::operator[](int idx)
- {
- return a[idx];
- }
- ostream& operator<<(ostream& os,const MyString& other){
- return os << other.a;
- }
- MyString &MyString::operator+=(const MyString &other)
- {
- int len = strlen(a) + strlen(other.a) + ;
- char *newstr = new char[len];
- memset(newstr, , len);
- strcpy(newstr, a);
- strcat(newstr, other.a);
- delete[] a;
- a = newstr;
- return *this;
- }
- int main(){
- MyString ms="hello";
- MyString mt("world");
- mt += ms;
- cout << mt << endl;
- return ;
- }
MyString(重写String)的更多相关文章
- 重写String类,也有些区别,供参考
头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #inc ...
- C++ string 类重写
(我们知道学习C++时,在学习完C的基础内容后最先上手的就是C++的string类来学习字符串处理的内容,这里我们通过重写string类来重新认识字符串处理的内容) 1.树立string类主要函数,确 ...
- (1)Object类 (2)包装类和数学处理类 (3)String类
1.Object类1.1 基本概念 java.lang.Object类是Java类层次结构的根类,任何类都是Object类的直接/间接子类. 1.2 常用的方法(重点) Object() - 无参构造 ...
- JavaScript学习笔记之string
字符串定义: 1,var myString=“内容”:or var myString=‘内容’ 2,var myString= new String(“内容”) ---〉创建对象, ...
- 常用API——字符串String型函数
上图: 声明 var myString = new String(“Every good boy does fine.”); var myString = “Every good boy does f ...
- java内存分配和String类型的深度解析
[尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...
- Check if a string is NULL or EMPTY using PowerShell
http://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889 Check if a s ...
- java基础(六)-----String性质深入解析
本文将讲解String的几个性质. 一.String的不可变性 对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创建就不可以修改 ...
- java基础(五) String性质深入解析
引言 本文将讲解String的几个性质. 一.String的不可变性 对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创 ...
随机推荐
- phpcms标签云
{pc:get sql="SELECT keyword FROM v9_keyword WHERE siteid=$siteid AND searchnums > 5 ORDER BY ...
- 一种map容器遍历的方法
遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...
- html5拖拽实现
1.需求 做一个h5正方形的拖拽框 2.分析 使用touchstart,touchmove,touchend这3个事件实现. 需要记录的数据有三组数据,分别是下图的(x0,y0),(x1,y1),(x ...
- angularjs入门基础一
app.controller('firstController',function($scope,$rootScope){ $scope.name='张三'; $rootScope.age='30'; ...
- OpenCV成长之路(8):直线、轮廓的提取与描述
基于内容的图像分析的重点是提取出图像中具有代表性的特征,而线条.轮廓.块往往是最能体现特征的几个元素,这篇文章就针对于这几个重要的图像特征,研究它们在OpenCV中的用法,以及做一些简单的基础应用. ...
- vmware vcenter orchestrator configuration提示“用户名密码错误或登录失败超过次数被锁定”
首次登录,使用默认用户密码登录vmware/vmware vmware vcenter orchestrator configuration提示"用户名密码错误或登录失败超过次数被锁定&qu ...
- 表单验证神器——jquery.validate插件
jquery.validate.js插件应用举例,ajax方式提交数据. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- HEOI 2016 游记
闲来无事,把这玩意儿补上. OI生涯中第一次正经的考试.挂的很惨. Day -1 不小心把机油(雾)sm惹毛了. 好像没啥别的事儿. Day 0 说好了上午直接去机房,然而临时说让我们上完前两节课再去 ...
- django xadmin 插件(1)
1. 插件的作用可以是全局的,也可以是只针对某个模型的.通过其 init_request控制是否加载此插件, demo如下: class SCPCardOverviewPlugin(BaseAdmin ...
- 'ModelOptions' object has no attribute 'get_field_names
peewee安装时随意了点.装了2.8.0的. 倒回到2.6.0就好了. sudo pip uninstall peewee sudo pip install peewee==2.6.0