[Operator Overloading]Simulate std::string

Implement the class String with given header, you can also make it like std::string!

member function ‘compare()’ returns -1 for <, 0 for ==, 1 for >.

Any question see STL references.

From: 范子垚

String.h
#ifndef SSCPP2014_STRING_H
#define SSCPP2014_STRING_H
 
#include <iostream>
 
class String {
  private:
    char *_buff;
    int _length, _size;  // _size is of the allocated memory
 
  public:
    // constructors
    String();
    explicit String(const char *src);
    String(const String &src);
    // destructor
    ~String();
    // member methods
    void assign(const char *src);
    void append(const char &other);
    void clear();
    int compare(const String &other) const;
    const char* c_str() const;
    bool empty() const;
    int find(const String &other, int pos = 0) const;
    int length() const;
    String substr(const int &pos, const int &count) const;
    // overload operators
    char& operator[](const int &index);
    void operator=(const String &other);
    void operator=(const char *src);
    String operator+(const String &other) const;
    bool operator<(const String &other) const;
    bool operator<=(const String &other) const;
    bool operator>(const String &other) const;
    bool operator>=(const String &other) const;
    bool operator==(const String &other) const;
    bool operator!=(const String &other) const;
    // friend methods
    friend std::ostream& operator<<(std::ostream& out, const String &str);
    // non-meaning static property
    static char _error_sign;  // initial as any char is okay
};
 
#endif

StringTest.cpp
#include <iostream>
#include <cstring>
#include "String.h"
#include <string>
using namespace std;
 
String a, b("MFGZ!");
String c = b;
 
void display() {
  cout << a.empty() << " " << a.length() << " " << a << endl;
  cout << b.empty() << " " << b.length() << " " << b << endl;
  cout << c.empty() << " " << c.length() << " " << c << endl;
}
 
int main() {
  string aa, bb, cc;
  display();
  c[0] = 'K';
  display();
  cin >> aa >> cc;
  a.assign(aa.c_str());
  c.assign(cc.c_str());
  display();
  b.clear();
  display();
  for (int i = 0 ; i < 10; ++i) {
      char t;
      cin >> t;
      a.append(t);
      b.append(t);
      c.append(t);
  }
  display();
  b = c;
  display();
  b = a + c;
  display();
  cout << a.find(String("1993")) << endl;
  cout << b.find(String("HYOUKA")) << endl;
  cout << c.find(String("RIKKA")) << endl;
  cout << a.substr(0, 3) << endl;
  cout << b.substr(3, 8) << endl;
  cout << c.substr(6, 1) << endl;
  cout << (a > b) << (a >= b) << (a < b) << (a <= b) << (a == b) << endl;
  cout << a.compare(b) << b.compare(a) << endl;
  cout << (a > c) << (a >= c) << (a < c) << (a <= c) << (a == c) << endl;
  cout << a.compare(c) << c.compare(a) << endl;
  b = a;
  cout << (a > b) << (a >= b) << (a < b) << (a <= b) << (a == b) << endl;
  cout << a.compare(b) << b.compare(a) << endl;
  cout << a.compare(a) << endl;
  return 0;
}
 
String.cpp
#include "String.h"
#include <iostream>
#include <cstring>
#include<string>

using namespace std;

char String:: _error_sign = '\0';     //  
String::String() {     
    _buff = NULL;
    _length = 0;
    _size = 0;
}

String::String(const char *src) {
    _length = strlen(src);
    _size = _length + 1;
    _buff = new char[_size];
    memset(_buff, '\0', _size);      //  每一次都不能忘记赋初值不然会有内存访问错误。。。。。这个以前也没用过刚开始我还写的是 sizeof(_buff)
    for ( int i = 0; i < _length; i++ ) {     //  还好xcode提示我了。。。
        _buff[i] = src[i];
    }
    _buff[_length] = '\0';
}
String::String(const String &src) {
    _length = src._length;
    _size = src._size;
    _buff = new char[_size];
    memset(_buff, '\0', _size);
    for (int i = 0; i < _length; i++) {
        _buff[i] = src._buff[i];
    }
}

String::~String() {
    if (_buff != NULL) {
        delete []_buff;    //   刚开始抽风了,想着如何一个一个删。。。。。
    }
}

void String::assign(const char *src) {
    clear();
    _length = strlen(src);
    _size = _length + 1;
    _buff = new char[_size];
    memset(_buff, '\0', _size);
    for (int i = 0; i < _length; i++) {
        _buff[i] = src[i];
    }
}

void String::append(const char &other) {
    if (_buff == NULL) {
        _length = 1;
        _size = 2;
        _buff = new char[2]; 
        memset(_buff, '\0', _size);
        _buff[0] = other;
        _buff[1] = '\0';
    } else {
        char *temp = new char[_size+1];
        memset(temp, '\0', _size+1);
        _size += 1;
        _length += 1;
        for (int i = 0; i < _length - 1; i++) {
            temp[i] = _buff[i];
        }
        temp[_length-1] = other;
        delete []_buff;
        _buff = temp;
    }
}

void String::clear() {
    if (_buff != NULL) {
        delete []_buff;
    }
    _size = 0;
    _length = 0;
    _buff = NULL;
}
int String::compare(const String &other) const {
    int min = 0;
    int flag = 1;  //  这个写的太长太恶心了然后也没去优化。。就是分长度一样和不一样两种情况。
    if (_length == other._length) {   //  长度一样的时候很自然的就return 1,0和-1了
        for (int i = 0; i < _length; i++) {
            if (_buff[i] < other._buff[i]) {
                flag = 0;
                return -1;
            } else if (_buff[i] > other._buff[i]) {
                flag = 0;
                return 1;
            }
        }
        if (flag == 1)
            return 0;
    } else {    //   写的太丑了实在。。。
        if (_length < other._length)  {
            min = _length;
        } else {
            min = other._length;
        }
        
        flag = 1;
        for (int i = 0; i < min; i++) {
            if (_buff[i] < other._buff[i]) {
                flag = 0;
                return -1;
            } else if ( _buff[i] > other._buff[i] ) {  //    就这刚开始只写了else 导致我找了好久的错T。T。。。。
                flag = 0;
                return 1;
            }
        }
        if ( flag == 1 ) {    
            if (_length > other._length) {
                return 1;
            } else {
                return -1;
            }
        }
    }
}

const char* String::c_str() const {   //  返回_buff
    if (_buff !=  NULL) {
        return _buff;
    } else {
        return "";
    }
}

bool String::empty() const {
    if (_buff == NULL) {
        return true;
    } else {
        return false;
    }
}
int String::find(const String &other, int pos) const {
    int i;
    bool flag = true;
    for (i = 0; i < _length; ++i) {
        flag = true;
        if ( _length - i < other._length ) {   //  我的想法好像还比较自然,如果other的长度大于_length-i那肯定找不到。。。
            return _length;
        } else {
            for (int j = 0; j < other._length; ++j) {
                if ( _buff[j+i] != other._buff[j] ) {
                    break;
                    flag = true;
                }
                if ( j == other._length-1 && flag ) {
                    return i;
                }
                if ( j == other._length -1 && flag == false)
                    return _length;
            }
        }
    }
}
int String::length() const {
    return _length;
}

String String::substr(const int &pos, const int &count) const {
    String a;     //   从pos起,找出count个字符
    a._length = count;
    int j = pos;
    a._size = count + 1;
    a._buff = new char[a._size];
    memset(a._buff, '\0', a._size);
    for (int i = 0; i < a._length; i++) {
        a._buff[i] = _buff[j + i];
    }
    return a;
}
char& String::operator[](const int &index) {
    if (index < _size &&index >= 0) {
        return _buff[index];
    } else {
        return _error_sign;
    }
}
void String::operator=(const String &other) {
    clear();
    _size = other._size;
    _length = other._length;
    _buff = new char[_size];
    memset(_buff, '\0', _size);
    for (int i = 0; i < _length; i++) {
        _buff[i] = other._buff[i];
    }
}
void String::operator=(const char *src) {
    clear();
    _length = strlen(src);
    _size = _length + 1;
    _buff = new char[_size];
    memset(_buff, '\0', _size);
    for (int i = 0; i < _length; i++) {
        _buff[i] = src[i];
    }
}
String String::operator+(const String &other) const {  //好多空格在复制粘贴过程中就不见了怎么破。。。
    String a;
    a._length = _length + other._length;
    a._size = _size + other._length;
    a._buff = new char[a._size];
    memset(a._buff, '\0', a._size);
    for (int i = 0; i < _length; i++) {
        a._buff[i] = _buff[i];
    }
    for ( int i = _length, j = 0; i < a._length; i++, j++ ) {
        a._buff[i] = other._buff[j];
    }
    a._buff[a._length] = '\0';
    return a;
}
bool String::operator<(const String &other) const {
    if (this->compare(other) == -1) {    //   利用写好的compare函数省了好多力气。。。
        return true;
    } else {
        return false;
    }
}
bool String::operator<=(const String &other) const {
    if (this->compare(other) == 1) {
        return false;
    } else {
        return true;
    }
    
}
bool String::operator>(const String &other) const {
    if (this->compare(other) == 1) {
        return true;
    } else {
        return false;
    }
    
}
bool String::operator>=(const String &other) const {
    if (this->compare(other) == -1) {
        return false;
    } else {
        return true;
    }
}
bool String::operator==(const String &other) const {
    if (!this->compare(other)) {
        return true;
    } else {
        return false;
    }
}
bool String::operator!=(const String &other) const {
    if (this->compare(other) != 0) {
        return true;
    } else {
        return false;
    }
}
ostream& operator<<(std::ostream& out, const String &str) {
    for (int i = 0; i < str._length; i++) {
        out << str._buff[i];
    }
    return out;
}

猜你喜欢

转载自blog.csdn.net/weixin_43912267/article/details/89292646