C++自定义字符串类
//header.h
#ifndef _HEADER_H
#define _HEADER_H
#define defaultSize 128
#include<iostream>
#include<assert.h>
#include<string.h>
using namespace std;
class myString
{
private:
char *ch;
int curLength;
int maxSize;
protected:
friend
ostream& operator<<(ostream& out, myString& str)
{
if(str.curLength>0 && str!=NULL)
cout<<str.ch<<endl;
else
cerr<<"error string object!\n";
return out;
}
public:
myString(int sz=defaultSize);//三种构造方式
myString(const char *init);
myString(const myString& ob);
~myString(){delete []ch;}
void print();//打印字符串
int Length()const;
myString operator()(int pos, int len);//字符串切片操作
bool operator == (myString& ob)const;//符号重载
bool operator != (myString& ob)const;
bool operator !()const;//判断字符串是否为空
myString& operator = (const myString& ob);//临时变量不能作为非const的引用参数
myString& operator +=(myString& ob);
char& operator[](int i);//返回字符串第i个位置的字符
int Find(myString &pat)const;//查子串,返回第一个子串起始位置,不存在返回-1
};
int myString::Find(myString &pat)const
{
if(pat.curLength > this->curLength)
return -1;
int i=0, j=0;
while(this->ch[i]!=pat.ch[0] && i<this->curLength)//在母串匹配子串第一个字符
{
++i;
} CIRCLE: while(this->ch[i]!=pat.ch[0] && i<this->curLength)//在母串匹配子串第一个字符
{
++i;
} if(i==this->curLength)//抵达母串最大长度还没找到。返回-1
return -1;
else if(this->ch[i] == pat.ch[0])//找到了继续匹配
{
++i, j=1;
while(i<this->curLength && j<pat.curLength && this->ch[i]==pat.ch[j])
{
++i;
++j;
}
if(j == pat.curLength)//抵达子串最大长度,则说明子串在母串内,返回位置
return i-j+1;
else if(i < this->curLength)//没找到子串且母串未抵达最大位置,继续在母串匹配子串第一个字符
goto CIRCLE;
else //母串抵达最大位置且没找到子串,返回-1
return -1;
} }
char& myString::operator[](int i)
{
assert(i<=this->curLength&&i>0);
return this->ch[i-1];
}
myString& myString::operator+=(myString& ob)//这个函数直接strcat(this->ch, ob.ch)就可以成功拼接,但是若这样操作则无法知道字符串的最大长度
{
myString tmp(*this);
delete [] this->ch;
this->ch = new char[this->maxSize+ob.maxSize];
strcpy(this->ch, tmp.ch);
strcat(this->ch, ob.ch);
this->curLength += ob.curLength;
this->maxSize += ob.maxSize;
delete[] tmp.ch;
return *this;
}
bool myString::operator !()const
{
return this->curLength==0;
}
bool myString::operator != (myString& ob)const
{
return strcmp(this->ch, ob.ch) == 0 ? false : true;
}
bool myString::operator == (myString& ob)const
{
return strcmp(this->ch, ob.ch) == 0 ? true : false;
}
myString& myString::operator = (const myString& ob)
{
if(&ob!=this)
{
delete[]ch; this->ch = new char[ob.maxSize];
this->maxSize = ob.curLength;
strcpy(this->ch, ob.ch);
this->curLength = ob.curLength;
}
else
{
cerr<<"String copy error\n";
}
return *this;
} myString myString::operator()(int pos, int len)
{
myString temp;
if(pos<0 || len<=0 || pos+len-1>=this->maxSize)
{
temp.curLength = 0;
temp.ch[0] = '\0';
}
else
{
if(pos+len-1 >= this->curLength)
len = this->curLength-pos;
temp.curLength = len;
for(int i=0,j=pos; i<len; ++i,++j)
temp.ch[i] = this->ch[j];
temp.ch[len] = '\0';
}
return temp;
} int myString::Length()const
{
return this->curLength;
} void myString::print()
{
cout<<this->ch<<endl;
} myString::myString(int sz)
{
this->maxSize = sz;
this->ch = new char[this->maxSize+1];
if(this->ch == NULL)
{
cerr<<"Allocation ERROR\n";
exit(1);
}
this->curLength = 0;
ch[0] = '\0';
} myString::myString(const char *init)
{
int len = strlen(init);
this->maxSize = (len > defaultSize) ? len : defaultSize;
this->ch = new char[this->maxSize+1];
if(this->ch == NULL)
{
cerr<<"Application Memory ERROR\n";
exit(1);
} this->curLength = len;
strcpy(this->ch, init); } myString::myString(const myString& ob)
{
this->maxSize = ob.maxSize;
this->ch = new char[this->maxSize+1];
if(this->ch == NULL)
{
cerr<<"Application Memory ERROR\n";
exit(1);
}
this->curLength = ob.curLength;
strcpy(this->ch, ob.ch); } #endif
#include"header.h" int main()
{
myString st(10), st1("ABCDEFG");
myString st2(st1);
//st.print(), st1.print(), st2.print();
st = st1(0, 4);
cout<<"st = ";
st.print();
cout<<"st1 = ";
st1.print();
st += st1;
cout<<"st + st1 = ";
st.print();
char a = st[5];
cout<<a<<endl;
myString st3("EFGH");
cout<<st1.Find(st3)<<endl;
st3 = st3(0, 3);
cout<<st1.Find(st3)<<endl;
return 0;
}
在实现自定义字符串类的时候遇到两个问题,一个是=号重载时,参数必须为const才能正常赋值。
https://www.cnblogs.com/area-h-p/p/11498481.html
另一个就是模式匹配寻找子串。
我实现的寻找子串的方法只需将母串遍历一遍,即可得到子串位置。
具体操作就是先在母串找到子串的第一个字符,然后检查后面字符是否也同样相同,不同继续在母串后边找与子串第一个字符相同的字符,相同则继续循环匹配,直到抵达子串或母串最大长度,若抵达子串最大长度,即找到子串位置,若没有到母串最大长度,则继续在母串后边找与子串第一个字符相同的字符。以此循环。
C++自定义字符串类的更多相关文章
- c++-重载等号,数组,指针,字符串类
重载 重载=操作符 1先释放旧对象资源 2用一个对象=给另外一个对象 3函数返回值当左值 返回一个引用 4 数组类 Array& operator=(Array& a1); 5 字符串 ...
- C++自定义String字符串类,支持子串搜索
C++自定义String字符串类 实现了各种基本操作,包括重载+号实现String的拼接 findSubStr函数,也就是寻找目标串在String中的位置,用到了KMP字符串搜索算法. #includ ...
- kettle系列-[KettleUtil]kettle插件,类似kettle的自定义java类控件
该kettle插件功能类似kettle现有的定义java类插件,自定java类插件主要是支持在kettle中直接编写java代码实现自定特殊功能,而本控件主要是将自定义代码转移到jar包,就是说自定义 ...
- struts2:遍历自定义字符串数组,遍历Action实例所引用对象中的数组
在struts2:OGNL表达式,遍历List.Map集合:投影的使用一文中已经讲述了OGNL遍历List.Map集合等功能. 本文简单写一个遍历数组的示范程序. 1. 遍历自定义字符串数组 < ...
- [转]掌握 ASP.NET 之路:自定义实体类简介 --自定义实体类和DataSet的比较
转自: http://www.microsoft.com/china/msdn/library/webservices/asp.net/CustEntCls.mspx?mfr=true 发布日期 : ...
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...
- 第33课 C++中的字符串类
在C语言中学习字符串时,我们使用的是字符数组的概念. C语言中没有真正意义的字符串.为了表达字符串的概念,我们使用了字符数组来模拟字符串. 在应用程序开发中,我们需要大量的处理字符串,如果还用C语言中 ...
- PHP自定义XML类实现数组到XML文件的转换
这两天在公司写和各应用商店应用内搜索的接口,大致就像百度应用内搜索这样的东西,具体可以点下面的链接查看. 百度应用内搜索 有的应用商店需要JSON格式的数据,所以我只需要用下面的语句就可以返回对方服务 ...
- 第11课 Qt中的字符串类
1. 历史遗留问题和解决方案 (1)历史遗留问题 ①C语言不支持真正意义上的字符串 ②C语言用字符数组和一组函数实现字符串操作 ③C语言不支持自定义类型,因此无法获得字符串类型 (2)解决方案 ①从C ...
随机推荐
- mac bigsur 安装mysql步骤
我首先下载的是mysql8.x,安装完后,在偏好设置里面,双击mysql图标,弹窗:未能载入偏好设置面板MySQL,重启无果,查攻略说是要安装5.7.x,在mysql官网上,下载5.7.29 强烈建议 ...
- Tomcat 内存马(二)Filter型
一.Tomcat处理请求 在前一个章节讲到,tomcat在处理请求时候,首先会经过连接器Coyote把request对象转换成ServletRequest后,传递给Catalina进行处理. 在Cat ...
- Linux基本命令学习-文件基本操作1
关机重启 shutdown -h now #立即关机 shutdown -h 5 # 5秒后关机 #重启 shutdown -r now #立即重启 reboot halt #重启 文件相关 系统目录 ...
- java中将double保留两位小数,将double保留两位小数并转换成String
将Double类型的数据保留2位小数: Double a = 3.566; BigDecimal bd = new BigDecimal(a); Double d = bd.setScale(2, B ...
- C++中简单使用HP-Socket
目录 简介 使用方式 实现简单线程池 实现TCP客户端 实现TCP服务端 实现Http客户端 附件 简介 HP-Socket 是一套通用的高性能 TCP/UDP /HTTP 通信 框架 ,包含服务端组 ...
- ipython和pip,模块安装方法
先下载 pip-.tar.gz 解压文件 cmd进入这个加压后的文件 执行 python setup.py install 然后配置环境变量 把 python 下的 Scripts 文件目录添加到 P ...
- python 函数的定义及调用语法,map 方法,函数嵌套递归
1.什么是函数 开发程序时候,需要代码执行多次,为了提高编写效率及代码重用性,所以把具有独立功能的代码块组织为一个小模块,给这个功能一个名称,这就是函数. 函数可以使用系统自带的函数也可以 ...
- 【Tool】MySQL卸载
MySQL卸载 2019-11-07 13:23:00 by冲冲 1.停止MySQL服务 右击"计算机" -- "管理" -- 左击"服务和应用程 ...
- Yarp 让系统内调度更灵活
简介 Yarp 是微软团队开发的一个反向代理组件, 除了常规的 http 和 https 转换通讯,它最大的特点是可定制化,很容易根据特定场景开发出需要的定制代理通道. 详细介绍:https://de ...
- k8s-数据持久化存储卷,nfs,pv/pvc
目录 数据持久化-储存卷 官方文档 存储卷类型 1.emptyDir 2.hostpath 3.pv/pvc(推荐使用) nfs官方文档 安装测试nfs pv/pvc管理nfs 官方文档 pv/pvc ...