Implement the String class:
class String {
public:
String(); // x = ""
String(const char*); // x = "abc"
String(const String&); // x = other_string
String& operator=(const char *);
String& operator=(const String&);
String operator+(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);
};
The String class implemented may be used like this:
f()
{
String x, y, s;
cout << "Please enter two strings\n";
cin >> x >> y;
cout << "x= " << x << " , y = " << y << '\n';
cout << "s = \"" << s << "\"" << endl;
s = "abc";
cout << "s = \"" << s << "\"" << endl;
cout << "\"" << x << "\" + \"" << y << "\" = " << "\"" << x+y << "\"\n";
String z = x;
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';
}
The f() will has the same output when String replace by string in STL.
conclusion:有关运算符重载 或者是引用传递 *this指针之类的 在印象中还很模糊 v.assign() v.append() v.size()调用的都是string 的函数,下面的代码就等于复制了一个string类,总的来说,目前已知的代码只是还很欠缺,也没有下定决心去认真钻研,一想到会很累就已然放弃
来找错:
// Problem#: 14443
// Submission#: 3726529
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include<cstdlib>
using namespace std;
class String {
public:
String(); // x = ""
String(const char*); // x = "abc"
String(const String&); // x = other_string
String& operator=(const char *);
String& operator=(const String&);
String operator+(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 *x;
};
String :: String(){
x="";
}
String :: String(const char* a){
strcpy(x,a);
}
String :: String(const String&a){
strcpy(x,a.x);
}
String& String :: operator=(const char *c){
x.assign(c);
return *this;
}
String& String :: operator=(const String& v){
x.assign(v,x)
return *this;
}
String String :: operator+(String s){
String temp();
temp.x.append(s,x);
return temp;
}
char& String :: operator[](int i){
return x[i];
}
char String :: operator[](int i) const{
return x[i];
}
int String :: size() const{
return x.size();
}
String& String :: operator+=(const String& a){
x.append(a,x)
return *this;
}
String& String :: operator+=(const char*a){
x.append(a);
return *this;
}
ostream& operator<<(ostream&b,const String&a{
b<<a.x;
return b;
}
istream& operator>>(istream&b,String&a){
b>>a.x;
return b;
}
bool operator==(const String& a, const char* s){
return s==a.x;
}
bool operator==(const String& a, const String& y){
return y.x==a.x;
}
bool operator!=(const String& a, const char* s){
return s!=a.x;
}
bool operator!=(const String& a, const String& y){
return y.x!=a.x;
}
正解:
// Problem#: 14443
// Submission#: 3726563
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class String {
public:
String();
String(const char*);
String(const String&);
String& operator=(const char *);
String& operator=(const String&);
String operator+(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:
string x;
};
String::String()
{
x = "";
}
String::String(const char* a)
{
x.assign(a);
}
String::String(const String&a)
{
x.assign(a.x);
}
String& String::operator=(const char *a)
{
x.assign(a);
return *this;
}
String& String::operator=(const String& a)
{
x.assign(a.x);
return *this;
}
String String::operator+(String a)
{
String temp(*this); temp.x.append(a.x);
return temp;
}
char& String::operator[](int i)
{
return x[i];
}
char String::operator[](int i) const
{
return x[i];
}
int String::size() const
{
return x.size();
}
String& String::operator+=(const String& a)
{
x.append(a.x);
return *this;
}
String& String::operator+=(const char* a)
{
x.append(a);
return *this;
}
ostream& operator<<(ostream& b, const String& a)
{
b << a.x;
return b;
}
istream& operator>>(istream& a, String& b)
{
a >> b.x;
return a;
}
bool operator==(const String& a, const char* s)
{
string n; n.assign(s); return a.x == n;
}
bool operator==(const String& a, const String& y)
{
return a.x == y.x;
}
bool operator!=(const String& a, const char* s)
{
string n; n.assign(s); return a.x != n;
}
bool operator!=(const String& a, const String& y)
{
return a.x != y.x;
}