简单的string 类库的封装
来源:互联网 发布:篮球教学软件大全 编辑:程序博客网 时间:2024/04/27 20:10
#include <iostream>
#include <cstring>
using namespace std;
class String
{
int len;
char *space;
public:
//无参构造
String():len()
{
space=new char(0);
}
//有参构造
String(const char *str)
{
len=strlen(str);
space=new char[len+1];
strcpy(space,str);
}
//拷贝构造
String(const String&s)
{
len=s.len;
space=new char[len+1];
strcpy(space,s.space);
}
//析构
~String()
{
if(space!=NULL)
{
delete[] space;
space=NULL;
len=0;
}
}
//赋值重载
String& operator=(const String &s)
{
if(this==&s)
{
return *this;
}
if(space!=NULL)
{
delete[] space;
space=NULL;
len=0;
}
len=s.len;
space=new char[len+1];
strcpy(space,s.space);
}
//c字符串赋值重载
String& operator=(const char *str)
{
if(space!=NULL)
{
delete []space;
space=NULL;
len=0;
}
len=strlen(str);
space=new char[len+1];
strcpy(space,str);
return *this;
}
//返回c语言字符串
char* c_str()
{
return space;
}
//返回字符串长度
int length()
{
return len;
}
//<号重载
bool operator<(const String&another)
{
if(*this>another||*this==another)
{
return false;
}
else
{
return true;
}
}
//字符查找
char * findchr(const char c)
{
if(NULL==space)
{
return NULL;
}
return strchr(space,c);
}
//字符串查找
char* findstr(const char *str)
{
if(NULL==space||NULL==str)
{
return NULL;
}
return strstr(space,str);
}
//对象查找
char* findobj(const String&s)
{
if(s.space==NULL||space==NULL)
{
return false;
}
return strstr(space,s.space);
}
//插入的位置
String& insert(String&s,int index=0)
{
if(s.len==0)//直接返回
{
return *this;
}
//坐标位置大于本类的位置,追加
if(index>len-1)
index=len;
if(index==len)//追加
{
*this+s;
}
else//插入
{
len+=s.len;//新的长度
char *tmp=new char[len+1];
int i;
//先拷贝到目的地址
for(i=0;i<index;i++)
tmp[i]=space[i];
//追加内容
strcat(tmp,s.space);
//追加
strcat(tmp,space+i);
if(space!=NULL)
{
delete[] space;
space=NULL;
}
space=tmp;
}
return *this;
}
//c语言插入插入的位置
String& insert(const char*str,int index=0)
{
String tmp=str;
insert(tmp,index);
return *this;
}
//删除一个c字符
String& delchr(const char c)
{
char *add;
if(add=findchr(c))
{
char *tmp=new char[len];
strncat(tmp,space,add-space);
}
return *this;
}
//删除一个字符串
String& delstr(const char *str)
{
}
inline void strswap(char&a,char &b)
{
char tmp=a;
a=b;
b=tmp;
}
//排序,0正序,逆序
String& sort(const int flag=0)
{
char *pCur;
char *pNew;
for(pCur=space;*pCur!=0;pCur++)
{
for(pNew=pCur+1;*pNew!=0;pNew++)
{
if(0==flag)
{
if(*pCur>*pNew)
strswap(*pCur,*pNew);
}
else
{
if(*pCur<*pNew)
strswap(*pCur,*pNew);
}
}
}
return *this;
}
//翻转
String& reverse()
{
if(len<=1)
{
return *this;
}
int start=0;
int end=len-1;
while(start<end)
{
strswap(space[start],space[end]);
start++;
end--;
}
return *this;
}
friend ostream& operator<<(ostream&o,const String&s);
friend bool operator>(const String&a,const String&b);
friend bool operator==(const String&a,const String&b);
friend String&operator+(String&a,const String&b);
};
//输出重载
ostream& operator<<(ostream&o,const String&s)
{
return o<<s.space;
}
//>重载
bool operator>(const String&a,const String&b)
{
if(strcmp(a.space,b.space)>0)
{
return true;
}
else
{
return false;
}
}
//==重载
bool operator==(const String&a,const String&b)
{
if(0==strcmp(a.space,b.space))
{
return true;
}
else
{
return false;
}
}
//+号重载
String& operator+(String&a,const String&b)
{
a.len+=b.len;
char *tmp=new char[a.len+1];
strcat(tmp,a.space);
strcat(tmp,b.space);
if(a.space!=NULL)
{
delete []a.space;
a.space=NULL;
}
a.space=tmp;
return a;
}
int main()
{
String m="5325afagw23twe+";
m.insert("xxxxxxx",3).sort(1);
cout<<m<<endl;
cout<<m.reverse()<<endl;
return 0;
}
#include <cstring>
using namespace std;
class String
{
int len;
char *space;
public:
//无参构造
String():len()
{
space=new char(0);
}
//有参构造
String(const char *str)
{
len=strlen(str);
space=new char[len+1];
strcpy(space,str);
}
//拷贝构造
String(const String&s)
{
len=s.len;
space=new char[len+1];
strcpy(space,s.space);
}
//析构
~String()
{
if(space!=NULL)
{
delete[] space;
space=NULL;
len=0;
}
}
//赋值重载
String& operator=(const String &s)
{
if(this==&s)
{
return *this;
}
if(space!=NULL)
{
delete[] space;
space=NULL;
len=0;
}
len=s.len;
space=new char[len+1];
strcpy(space,s.space);
}
//c字符串赋值重载
String& operator=(const char *str)
{
if(space!=NULL)
{
delete []space;
space=NULL;
len=0;
}
len=strlen(str);
space=new char[len+1];
strcpy(space,str);
return *this;
}
//返回c语言字符串
char* c_str()
{
return space;
}
//返回字符串长度
int length()
{
return len;
}
//<号重载
bool operator<(const String&another)
{
if(*this>another||*this==another)
{
return false;
}
else
{
return true;
}
}
//字符查找
char * findchr(const char c)
{
if(NULL==space)
{
return NULL;
}
return strchr(space,c);
}
//字符串查找
char* findstr(const char *str)
{
if(NULL==space||NULL==str)
{
return NULL;
}
return strstr(space,str);
}
//对象查找
char* findobj(const String&s)
{
if(s.space==NULL||space==NULL)
{
return false;
}
return strstr(space,s.space);
}
//插入的位置
String& insert(String&s,int index=0)
{
if(s.len==0)//直接返回
{
return *this;
}
//坐标位置大于本类的位置,追加
if(index>len-1)
index=len;
if(index==len)//追加
{
*this+s;
}
else//插入
{
len+=s.len;//新的长度
char *tmp=new char[len+1];
int i;
//先拷贝到目的地址
for(i=0;i<index;i++)
tmp[i]=space[i];
//追加内容
strcat(tmp,s.space);
//追加
strcat(tmp,space+i);
if(space!=NULL)
{
delete[] space;
space=NULL;
}
space=tmp;
}
return *this;
}
//c语言插入插入的位置
String& insert(const char*str,int index=0)
{
String tmp=str;
insert(tmp,index);
return *this;
}
//删除一个c字符
String& delchr(const char c)
{
char *add;
if(add=findchr(c))
{
char *tmp=new char[len];
strncat(tmp,space,add-space);
}
return *this;
}
//删除一个字符串
String& delstr(const char *str)
{
}
inline void strswap(char&a,char &b)
{
char tmp=a;
a=b;
b=tmp;
}
//排序,0正序,逆序
String& sort(const int flag=0)
{
char *pCur;
char *pNew;
for(pCur=space;*pCur!=0;pCur++)
{
for(pNew=pCur+1;*pNew!=0;pNew++)
{
if(0==flag)
{
if(*pCur>*pNew)
strswap(*pCur,*pNew);
}
else
{
if(*pCur<*pNew)
strswap(*pCur,*pNew);
}
}
}
return *this;
}
//翻转
String& reverse()
{
if(len<=1)
{
return *this;
}
int start=0;
int end=len-1;
while(start<end)
{
strswap(space[start],space[end]);
start++;
end--;
}
return *this;
}
friend ostream& operator<<(ostream&o,const String&s);
friend bool operator>(const String&a,const String&b);
friend bool operator==(const String&a,const String&b);
friend String&operator+(String&a,const String&b);
};
//输出重载
ostream& operator<<(ostream&o,const String&s)
{
return o<<s.space;
}
//>重载
bool operator>(const String&a,const String&b)
{
if(strcmp(a.space,b.space)>0)
{
return true;
}
else
{
return false;
}
}
//==重载
bool operator==(const String&a,const String&b)
{
if(0==strcmp(a.space,b.space))
{
return true;
}
else
{
return false;
}
}
//+号重载
String& operator+(String&a,const String&b)
{
a.len+=b.len;
char *tmp=new char[a.len+1];
strcat(tmp,a.space);
strcat(tmp,b.space);
if(a.space!=NULL)
{
delete []a.space;
a.space=NULL;
}
a.space=tmp;
return a;
}
int main()
{
String m="5325afagw23twe+";
m.insert("xxxxxxx",3).sort(1);
cout<<m<<endl;
cout<<m.reverse()<<endl;
return 0;
}
0 0
- 简单的string 类库的封装
- 简单封装Jackson,实现JSON String<->Java Object的Mapper.
- 基于Okhttp3的简单封装,Bean,String数据返回
- 简单的String类
- 简单的string 类
- 简单的String类
- 简单封装的doc4j工具类方法的简单封装
- 封装简单的axios库
- HttpWebRequest类的简单封装
- OKHttp的简单封装类
- SQLSERVER简单的封装
- 简单封装的窗口
- Mutex的简单封装
- fusionchart的简单封装
- rapidxml的简单封装
- 简单的Ajax封装
- Curl 简单的封装
- 队列的简单封装
- hdu2553:N皇后问题(dfs)
- XHTML总结
- 网页中时光轴的简单实现
- 树上倍增实现lca
- 面试那点事儿
- 简单的string 类库的封装
- KVM&Libvirt基本概念及开发杂谈
- sql2008登录成功后又更改了计算机名怎么办
- 深入分析Activity 绘制机制
- 蓝桥杯—网络寻路题解(邻接表构图)
- 背包系列第六篇----完全背包(求解最大价值的个数)
- Linux文件读写权限
- Git初学
- Java IO系统——NIO之Buffer、Channel和Charset类