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)的更多相关文章

  1. 重写String类,也有些区别,供参考

    头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #inc ...

  2. C++ string 类重写

    (我们知道学习C++时,在学习完C的基础内容后最先上手的就是C++的string类来学习字符串处理的内容,这里我们通过重写string类来重新认识字符串处理的内容) 1.树立string类主要函数,确 ...

  3. (1)Object类 (2)包装类和数学处理类 (3)String类

    1.Object类1.1 基本概念 java.lang.Object类是Java类层次结构的根类,任何类都是Object类的直接/间接子类. 1.2 常用的方法(重点) Object() - 无参构造 ...

  4. JavaScript学习笔记之string

    字符串定义: 1,var myString=“内容”:or var myString=‘内容’ 2,var myString= new String(“内容”)           ---〉创建对象, ...

  5. 常用API——字符串String型函数

    上图: 声明 var myString = new String(“Every good boy does fine.”); var myString = “Every good boy does f ...

  6. java内存分配和String类型的深度解析

    [尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...

  7. 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 ...

  8. java基础(六)-----String性质深入解析

    本文将讲解String的几个性质. 一.String的不可变性 对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创建就不可以修改 ...

  9. java基础(五) String性质深入解析

    引言   本文将讲解String的几个性质. 一.String的不可变性   对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创 ...

随机推荐

  1. C#网络爬虫 WebUtility使用 转义字符 urlCode

    背景: 在C#写网络爬虫时候,有时候需要将html中的转义字符进行处理,还有网址中的中文处理 一.html转义字符处理 1.ASP.NET中的html解析 HttpUtility.HtmlDecode ...

  2. MVC框架 与Smarty

    MVC一种软件设计模式 MVC全名是 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据. ...

  3. 使用uwsgi 部署python web服务

    uwsgi, wsgi协议的一个很好的实现,源码在这里:https://github.com/unbit/uwsgi c语言编写,有兴趣可以研究下. 上DEMO: wsgi_server.py def ...

  4. 应用HTK搭建语音拨号系统3:创建绑定状态的三音素HMM模型

    选自:http://maotong.blog.hexun.com/6261873_d.html 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声明:版权所有,转载请注明作者和来源 该系 ...

  5. 使用 MongoDB 的_id 查询

    MongoDB 默认在插入数据时,生成一个主键_id,那么怎么使用_id来查询数据? 查询全部 > db.foo.find(){ "_id" : ObjectId(" ...

  6. java前后台之间传值的几种方式

    自己写的代码太少,有时候前后台传值还写的不是很熟练,现在总结一下,加深下印象. 1.jquery的Ajax传值 ---->前台到后台 期望功能:把前台用户输入的信息保存在数据库里. 前台jsp代 ...

  7. 【GoLang】GoLang 错误处理 -- 使用 error is value 的思路处理,检查并处理error

    吐血推荐: https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully 参考资料: https:/ ...

  8. MVC Return View() 和 Return PartialView()的区别

    分部视图在action中返回一定要用PartialView(),而不要偷懒使用View(),因为,如果你使用View()渲染视图,系统会认为你是一个标准视图,会为你加个默认的母板页(Layout),除 ...

  9. HTML文档中头部文件介绍

    meta是用来在模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和http-eq ...

  10. oracle数据库常用plsql语句

    (一)oracle中常用的数据类型 (二)PL-sql基本语法 1.创建数据库表.删除数据库表 create table table1--创建表 ( field1 number(8), field2 ...