1003. The String class

Description

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;
}                               
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值