simulate std string

w(゚Д゚)w
C++就没有短的代码
改到哭

Description:
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.

more imformation just see the test.

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

String.cpp

#include "String.h"
#include<iostream>
#include<cstring>
using namespace std;
String::String() {
    _buff = NULL;
    _length = _size = 0;
}
String::String(const char *src) {
    _size = strlen(src) + 1;
    _buff = new char[_size];
    memset(_buff, 0, _size);
    snprintf(_buff, _size, "%s", src);
    _length = strlen(_buff);
}
String::String(const String &src) {
    _size = src._size;
    _length = src._length;
    _buff = new char[_size];
    memset(_buff, 0, _size);
    snprintf(_buff, _size, "%s", src._buff);
}
String::~String() {
    delete [] _buff;
}
void String::assign(const char *src) {
    this->clear();
    _size = strlen(src) + 1;
    _buff = new char[_size];
    memset(_buff, 0, _size);
    snprintf(_buff, _size, "%s", src);
    _length = strlen(_buff);
}
void String::append(const char &other) {
    if (this->empty()) {
        this->assign("o");
        _buff[0] = other;
    } else {
        if (_length + 1 < _size) {
            _buff[_length] = other;
            _length++;
        } else {
            _size = _size * 2;
            char* o_buff = new char[_size];
            memset(o_buff, 0, _size);
            snprintf(o_buff, _size, "%s", _buff);
            delete[] _buff;
            _buff = o_buff;
            _buff[_length] = other;
            _length++;
        }
    }
}
void String::clear() {
    if (_buff) {
        delete [] _buff;
        _buff = NULL;
    }
    _length = _size = 0;
}
int String::compare(const String &other) const {
    int min = _length < other._length ? _length : other._length;
    for (int i = 0; i < min; i++) {
        if (_buff[i] < other._buff[i]) {
            return -1;
        } else if (_buff[i] > other._buff[i]) {
            return 1;
        }
    }
    if (_length < other._length) {
        return -1;
    } else if (_length > other._length) {
        return 1;
    } else {
        return 0;
    }
}
const char* String::c_str() const {
    return _buff;
}
bool String::empty() const {
    if (_length == 0) {
        return true;
    } else {
        return false;
    }
}
int String::find(const String &other, int pos) const {
    if (other.empty()) return -1;
    int count = 0;
    for (int i = 0; i < _length; i++) {
        if (_buff[i] == other._buff[0]) {
            for (int j = 0; j < other._length; j++) {
                if (_buff[i + j] == other._buff[pos + j]) count++;
            }
            if (count == other._length) return i;
            count = 0;  //  因为漏了这个改了5、6次/(ㄒoㄒ)/~~
        }
    }
    return _length;
}
int String::length() const {
    return _length;
}
String String::substr(const int &pos, const int &count) const {
    String temp;
    if (pos >= _length || pos + count > _length) return temp;
    temp._size = count + 1;
    temp._buff = new char[temp._size];
    memset(temp._buff, 0, temp._size);
    snprintf(temp._buff, temp._size, "%s", _buff + pos);
    temp._length = strlen(temp._buff);
    return temp;
}
char& String::operator[](const int &index) {
    if (index >= 0 && index < _length) {
        return _buff[index];
    } else {
        return _error_sign;
    }
}
void String::operator=(const String &other) {
    this->assign(other._buff);
}
void String::operator=(const char *src) {
    this->assign(src);
}
String String::operator+(const String &other) const {
    String temp;
    temp._size = this->_length + other._length + 1;
    temp._buff = new char[temp._size];
    snprintf(temp._buff, _size, "%s", _buff);
    snprintf(temp._buff + _length, other._size, "%s", other._buff);
    temp._length = strlen(_buff);
    return temp;
}
bool String::operator<(const String &other) const {
    return this->compare(other) == -1 ? true : false;
}
bool String::operator<=(const String &other) const {
    return this->compare(other) == 1 ? false : true;
}
bool String::operator>(const String &other) const {
    return this->compare(other) == 1 ? true : false;
}
bool String::operator>=(const String &other) const {
    return this->compare(other) == -1 ? false : true;
}
bool String::operator==(const String &other) const {
    return this->compare(other) == 0 ? true : false;
}
bool String::operator!=(const String &other) const {
    return this->compare(other) == 0 ? false : true;
}
std::ostream& operator<<(std::ostream& out, const String &str) {
    for (int i = 0; i < str._length; i++) {
        out << str._buff[i];
    }
    return out;
}
char String::_error_sign = '\0';

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值