📄 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 + -