?? string.cpp
字號:
#include <iostream>
using namespace std;
/*-------------------僅低版本的VC++需要-------------------*/
class String; //前向引用聲明
bool operator<(const String &s1,const String &s2);
bool operator<=(const String &s1,const String &s2);
bool operator>(const String &s1,const String &s2);
bool operator>=(const String &s1,const String &s2);
bool operator==(const String &s1,const String &s2);
bool operator!=(const String &s1,const String &s2);
ostream &operator<<(ostream &out,const String &s);
istream &operator>>(istream &is,String &s);
/*-------------------僅低版本的VC++需要-------------------*/
class String {
public:
//用一個字符串構造String對象(類型轉換構造函數)
String(char *str="");
//拷貝構造函數
String(const String &src);
//析構函數
~String() {delete[] _str;}
//獲取字符串的長度
int getLength() const {return strlen(_str);}
//用友元函數重載6個關系運算符
friend bool operator<(const String &s1,const String &s2);
friend bool operator<=(const String &s1,const String &s2);
friend bool operator>(const String &s1,const String &s2);
friend bool operator>=(const String &s1,const String &s2);
friend bool operator==(const String &s1,const String &s2);
friend bool operator!=(const String &s1,const String &s2);
//重載插入運算符<<,友員函數
friend ostream &operator<<(ostream &out,const String &s);
//重載抽取運算符>>,友員函數
friend istream &operator>>(istream &is,String &s);
//重載賦值運算符=(字符串拼接)
String &operator=(const String &s);
//重載運算符+(字符串拼接)
String operator+(const String &s);
//重載運算符+=(字符串拼接)
String &operator+=(const String &s);
//重載下標運算符[](取字符串中的某一個字符)
char &operator[](int index) {return _str[index];}
private:
char *_str; //字符串指針
};
String::String(char *str) {
_str=new char[strlen(str)+1]; //申請空間
strcpy(_str,str);
}
String::String(const String &src) {
_str=new char[strlen(src._str)+1]; //申請空間
strcpy(_str,src._str);
}
bool operator<(const String &s1,const String &s2) {
return (strcmp(s1._str,s2._str)<0);
}
bool operator<=(const String &s1,const String &s2) {
return (strcmp(s1._str,s2._str)<=0);
}
bool operator>(const String &s1,const String &s2) {
return (strcmp(s1._str,s2._str)>0);
}
bool operator>=(const String &s1,const String &s2) {
return (strcmp(s1._str,s2._str)>=0);
}
bool operator==(const String &s1,const String &s2) {
return (strcmp(s1._str,s2._str)==0);
}
bool operator!=(const String &s1,const String &s2) {
return (strcmp(s1._str,s2._str)!=0);
}
ostream &operator<<(ostream &out,const String &s) {
out<<s._str; //使用標準的<<
return out;
}
istream &operator>>(istream &is,String &s) {
char tmpStr[256]; //假定最多可以讀入長度不超過255的字符串
is.getline(tmpStr,255); //從輸入流讀入最多255個字符
delete[] s._str; //釋放原來的字符串空間
//根據所讀入的字符串的實際長度重新申請空間
s._str=new char[strlen(tmpStr)+1];
//將字符串從臨時緩沖區中復制到_str中
strcpy(s._str,tmpStr);
return is;
}
String &String::operator=(const String &s) {
if (this!=&s) {//重載時,必須當心自賦值!
if (strlen(_str)!=strlen(s._str)) {
//釋放當前字符串的空間
delete[] _str;
//申請新的空間
_str=new char[strlen(s._str)+1];
}
//復制字符串
strcpy(_str,s._str);
}
return *this;
}
String String::operator+(const String &s) {
char *tmpStr; //臨時字符串
//為臨時字符串申請空間
tmpStr=new char[strlen(_str)+strlen(s._str)+1];
//將兩個字符串拼接在一起
strcpy(tmpStr,_str);
strcat(tmpStr+strlen(_str),s._str);
//用臨時字符串構造一個臨時的String對象并返回
return String(tmpStr);
}
String &String::operator+=(const String &s) {
//先保存當前的字符串
char *save=_str;
//申請新的空間
_str=new char[strlen(_str)+strlen(s._str)+1];
//將兩個字符串拼接在一起
strcpy(_str,save);
strcat(_str+strlen(save),s._str);
//釋放原字符串的空間
delete[] save;
return *this;
}
void main() {
String s1;
String s2("This is a test string.");
String s3("This is second test string.");
String s4(s2);
cout<<"字符串1["<<s1<<"]的長度為:"<<s1.getLength()<<endl;
cout<<"字符串2["<<s2<<"]的長度為:"<<s2.getLength()<<endl;
cout<<"字符串3["<<s3<<"]的長度為:"<<s3.getLength()<<endl;
cout<<"字符串4["<<s4<<"]的長度為:"<<s4.getLength()<<endl;
cout<<"s2[2]為:"<<s2[2];
cout<<" s2[10]為:"<<s2[10]<<endl;
if (s2<s3)
cout<<"字符串s2<字符串s3!"<<endl;
else
cout<<"字符串s2>=字符串s3!"<<endl;
if (s3>=s4)
cout<<"字符串s3>=字符串s4!"<<endl;
else
cout<<"字符串s3<字符串s4!"<<endl;
if (s2==s4)
cout<<"字符串s2==字符串s4!"<<endl;
else
cout<<"字符串s2!=字符串s4!"<<endl;
if (s3!=s4)
cout<<"字符串s3!=字符串s4!"<<endl;
else
cout<<"字符串s3==字符串s4!"<<endl;
cout<<"請輸入一個長度不超過255的串:";
cin>>s1;
cout<<"你剛輸入的字符串["<<s1<<"]的長度為:"<<s1.getLength()<<endl;
s1=s2+s1;
cout<<"執行s1=s2+s1;后的字符串為:["<<s1<<"]\n";
s1+="+=string."; //自動執行類型轉換構造函數進行類型轉換!
cout<<"執行s1+=\"+=string.\";后的字符串為:["<<s1<<"]\n";
}
/*
該程序的輸出為:
字符串1[]的長度為:0
字符串2[This is a test string.]的長度為:22
字符串3[This is second test string.]的長度為:27
字符串4[This is a test string.]的長度為:22
s2[2]為:i s2[10]為:t
字符串s2<字符串s3!
字符串s3>=字符串s4!
字符串s2==字符串s4!
字符串s3!=字符串s4!
請輸入一個長度不超過255的串:s2
你剛輸入的字符串[s2]的長度為:2
執行s1=s2+s1;后的字符串為:[This is a test string.s2]
執行s1+="+=string.";后的字符串為:[This is a test string.s2+=string.]
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -