?? 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;//強制轉(zhuǎn)換類型為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;
}
//- - - - - - - - - - - - - - -創(chuàng)建一個空字符串
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';
}
//---------------------------------------構(gòu)造一個構(gòu)造函數(shù)分配一個緩沖區(qū)間
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';
}
//------------------------------------//拷貝構(gòu)造
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';
}
//----------------- 析構(gòu)函數(shù)
string::~string()
{
delete[]buffer;
buffer=NULL;
buflen=0;
}
//------------------------------------
string::operator const char *()const//強制轉(zhuǎn)換類型為C字符
{
return buffer;
}
//---------------------------------------
void string::operator =(const string & right)//賦值函數(shù)
{
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 + -