首页 > 代码库 > 1001. The String Class
1001. The String Class
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class String {
public:
String(); // str = ""
String(const char*); // str = "abc"
String(const String&); // str = other_string
String& operator=(const char *);
String& operator=(const String&);
String operator+(const String&);
~String();
char& operator[](int i);
char operator[](int i) const;
int size() const;
String& operator+=(const String&);
String& operator+=(const char*);
friend ostream& operator<<(ostream&, const String&);
friend istream& operator>>(istream&, String&);
friend bool operator==(const String& x, const char* s);
friend bool operator==(const String& x, const String& y);
friend bool operator!=(const String& x, const char* s);
friend bool operator!=(const String& x, const String& y);
private:
char *str;
};
String::String(){
str = new char[1];
str[0]=‘\0‘;
}
String::String(const char* a){
int t = strlen(a)+1;
str = new char[t];
str = strcpy(str,a);
str[t-1]=‘\0‘;
}
String::String(const String& b){
int t=strlen(b.str)+1;
str = new char[t];
str = strcpy(str,b.str);
str[t-1]=‘\0‘;
}
String& String::operator=(const char *b){
int t=strlen(b)+1;
str = new char[t];
str = strcpy(str,b);
str[t-1]=‘\0‘;
return *this;
}
String& String::operator=(const String& b){
int t=strlen(b.str)+1;
str = new char[t];
str =strcpy(str,b.str);
str[t-1]=‘\0‘;
return *this;
}
String String::operator+(const String& b){
String str1;
str1 = new char[strlen(b.str)+1+strlen(this->str)];
for(int i=0;i<strlen(this->str);i++){
str1.str[i]=this->str[i];
}
for(int j=strlen(this->str);j<strlen(b.str)+strlen(this->str);j++){
str1.str[j]=b.str[j-strlen(this->str)];
}
str1[strlen(b.str)+strlen(this->str)]=‘\0‘;
return str1;
}
String::~String(){
delete [] str;
}
char& String::operator[](int i){
return str[i];
}
char String::operator[](int i) const{
return str[i];
}
int String::size() const{
return strlen(str);
}
String& String::operator+=(const String& b){
int l=strlen(str)+strlen(b.str);
char *p=new char[l+10];
strcpy(p,str);
strcat(p,b.str);
strcpy(str,p);
return *this;
}
String& String::operator+=(const char* b){
int l=strlen(str)+strlen(b);
char *p=new char[l+1];
strcat(p,str);
strcat(p,b);
strcpy(str,p);
return *this;
}
ostream& operator<<(ostream& a, const String& b){
a<<b.str;
return a;
}
istream& operator>>(istream& a, String& b){
a>>b.str;
return a;
}
bool operator==(const String& x, const char* s){
if(strlen(x.str)!=strlen(s)) return false;
for(int i=0;i<strlen(x.str);i++){
if(x.str[i]!=s[i]) return false;
}
return true;
}
bool operator==(const String& x, const String& s){
if(strlen(x.str)!=strlen(s.str)) return false;
for(int i=0;i<strlen(x.str);i++){
if(x.str[i]!=s.str[i]) return false;
}
return true;
}
bool operator!=(const String& x, const char* s){
if(strlen(x.str)!=strlen(s)) return true;
int count=0;
for(int i=0;i<strlen(s);i++){
if(x.str[i]!=s[i]);
count++;
}
if(count==strlen(s)) return false;
else return true;
}
bool operator!=(const String& x, const String& s){
if(strlen(x.str)!=strlen(s.str)) return true;
int count=0;
for(int i=0;i<strlen(s.str);i++){
if(x.str[i]!=s[i]);
count++;
}
if(count==strlen(s.str)) return false;
else return true;
}
String f1(String a, String b)
{
a[2] = ‘x‘;
char c = b[2];
cout << "in f: " << a << ‘ ‘ << b << ‘ ‘ << c << ‘\n‘;
return b;
}
void f2(String s, const String& r)
{
char c1 = s[1]; // c1 = s.operator[](1).operator char()
s[1] = ‘c‘; // s.operator[](1).operator=(‘c‘)
char c2 = r[1]; // c2 = r.operator[](1)
// r[1] = ‘d‘; // error: assignment to non-lvalue char, r.operator[](1) = ‘d‘
}
void f()
{
String x, y, s;
cout << "Please enter two strings\n";
cin >> x >> y;
cout << "x= " << x << " , y = " << y << ‘\n‘;
y = f1(x,y);
cout << y << endl;
f2(x,y);
cout << "s = \"" << s << "\"" << endl;
s = "abc";
cout << "s = \"" << s << "\"" << endl;
cout << "\"" << x << "\" + \"" << y << "\" = " << "\"" << x+y << "\"\n";
String z = x;
cout<<z<<endl;
if (x != z) cout << "x corrupted!\n";
x[0] = ‘!‘;
if (x == z) cout << "write failed!\n";
cout << "exit: " << x << ‘ ‘ << z << ‘\n‘;
z = s;
if (s != z) cout << "s corrupted!\n";
s[0] = ‘!‘;
if (s == z) cout << "write failed!\n";
cout << "exit: " << s << ‘ ‘ << z << ‘\n‘;
}
int main()
{
int T;
cin >> T;
while (T--)
{
f();
}
}
1001. The String Class