📄 string_con.h
字号:
#include<iostream.h>
#include<assert.h>
//----------------------------------------主串类
///////////////////////////
char nothing;
class string{
public:
string();
string(char);
string(unsigned );
string(const char*);
string(const string &);
~string();
friend class substring;
void operator=(const string & right);//赋值
void operator+=(const string & right);////赋值用于串连接
friend string operator+(const string & a,const string & b);//串连接
istream & getline (istream &);//输入
friend istream & operator>>(istream & in,string & str);
operator const char *()const;//强制转换类型为C字符
int compare (const string &)const; //比较字符
friend int operator>(const string & ,const string & );
friend int operator>=(const string & ,const string & );
friend int operator<(const string & ,const string & );
friend int operator<=(const string & ,const string & );
friend int operator==(const string &,const string & );
friend int operator!=(const string & ,const string & );
substring operator ()(unsigned start,unsigned ien);//取子串
char & operator[](unsigned index)const;//访问单个字符
unsigned length()const;//测量字符串的长度.
private:
unsigned buflen;
char *buffer;
};
//串长度
unsigned cstrlen(const char str[])
{
unsigned i=0;
while(str[i]!='\0')i++;
return i;
}
//- - - - - - - - - - - - - - -创建一个空字符串
string::string()
{ buflen=1;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]='\0';
}
// ---------------------------------------建立一个单字符的字符串
string::string(char c)
{ buflen=2;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]=c;
buffer[1]='\0';
}
//---------------------------------------构造一个构造函数分配一个缓冲区间
string::string(unsigned size)
{
assert(size>=0);
buflen=1+size;
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;i<buflen;i++)
buffer[i]='\0';
}
//-----------------------------------------------
string::string(const char *s)
{
buflen=1+cstrlen(s);
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;s[i]!='\0';i++)
buffer[i]=s[i];
buffer[i]='\0';
}
//------------------------------------//拷贝构造
string::string(const string & s)
{
buflen=1+cstrlen(s.buffer);
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;s.buffer[i]!='\0';i++)
buffer[i]=s.buffer[i];
buffer[i]='\0';
}
//----------------- 析构函数
string::~string()
{
delete[]buffer;
buffer=NULL;
buflen=0;
}
//------------------------------------
string::operator const char *()const//强制转换类型为C字符
{
return buffer;
}
//---------------------------------------
void string::operator =(const string & right)//赋值函数
{
const unsigned rightlength=right.length();
if(right.length()>=buflen)
{
delete[]buffer;
buflen=1+rightlength;
buffer=new char[buflen];
assert(buffer!=0);
}
for(unsigned i=0;right.buffer[i]!='\0';i++)
buffer[i]=right.buffer[i];
buffer[i]='\0';
}
//---------------------------------------
void string::operator+=(const string & s)//赋值用于串连接
{
unsigned i;
unsigned conlen=length()+s.length();
if(conlen>=buflen)
{char * newbuf=new char[1+conlen];
assert(newbuf!=0);
for(i=0;buffer[i]!='\0';i++)
newbuf[i]=buffer[i];
delete[]buffer;
buflen=1+conlen;
buffer=newbuf;
}
else
i=cstrlen(buffer);
for(unsigned j=0;s.buffer[j]!='\0';j++,i++)
buffer[i]=s.buffer[j];
buffer[i]='\0';
}
//---------------------------------------
string operator+(const string & left,const string & right)//串连接
{
string result(left);
result+=right;
return result;
}
//--------------------------------------
unsigned string::length()const//测量字符串的长度
{
return cstrlen(buffer);
}
//----------------------------------------
istream & string::getline(istream & in)//输入和输出
{char str[80];
in.getline(str,80,'\n');
string s1(str);
*this=s1;
return in;
}
//-----------------------------------------
istream & operator>>(istream & in,string & str)//输入
{
char inbuffer[1000];
if(in>>inbuffer)
str=inbuffer;
else
str="";
return in;
}
//-------------------------------------
int string::compare (const string & s)const//比较运算
{
char *p=buffer;
char *q=s.buffer;
for(;(*p!='\0')&&(*p==*q);p++,q++)
;
return *p-*q;
}
int operator<=(const string & a,const string & b)
{
int result=a.compare(b);
if(result<=0)
return 1;
else return 0;
}
int operator>(const string & a,const string & b)
{
int result=a.compare(b);
if(result>0)
return 1;
else return 0;
}
int operator>=(const string & a,const string & b)
{
int result=a.compare(b);
if(result>=0)
return 1;
else return 0;
}
int operator<(const string & a,const string & b)
{
int result=a.compare(b);
if(result<0)
return 1;
else return 0;
}
int operator==(const string & a,const string & b)
{
int result=a.compare(b);
if(result==0)
return 1;
else return 0;
}
//--------------------------------------------------
char & string::operator[](unsigned index)const//访问单个字符
{
if(index<0||index>cstrlen(buffer))
{
nothing='\0';
return nothing ;
}
return buffer[index];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -