Skip to content
On this page

MyString.v2.cpp


标签:代码片段CPP/basic  

❓ 改进 MyString.cpp, 支持 CPP运算符重载

cpp
#ifndef __MY_STRING_H__
#define __MY_STRING_H__
#include <iostream>

class MyString {
private:
  char *_str;
  int _size;

public:
  MyString();
  MyString(const char *s);
  MyString(const MyString &other);
  ~MyString();
  MyString &operator=(const MyString &ohter);
  bool empty();
  long long int size();
  char *c_str();
  char &at(int pos);

  char &operator[](int pos) const;
  const MyString operator+(const char *other) const;
  const MyString operator+(const MyString &other) const;
  bool operator==(const char *other) const;
  bool operator==(const MyString &other) const;
  bool operator!=(const char *other) const;
  bool operator!=(const MyString &other) const;
  bool operator>=(const char *other) const;
  bool operator>=(const MyString &other) const;
  bool operator<=(const char *other) const;
  bool operator<=(const MyString &other) const;
  bool operator>(const char *other) const;
  bool operator>(const MyString &other) const;
  bool operator<(const char *other) const;
  bool operator<(const MyString &other) const;
  friend std::ostream &operator<<(std::ostream &l, const MyString &r);
  friend std::istream &operator>>(std::istream &l, const MyString &r);
};

bool operator==(const char *l, const MyString &r);
bool operator!=(const char *l, const MyString &r);
bool operator>(const char *l, const MyString &r);
bool operator>=(const char *l, const MyString &r);
bool operator<(const char *l, const MyString &r);
bool operator<=(const char *l, const MyString &r);
MyString operator+(const char *l, const MyString &r);

#endif
cpp
#include "MyString.h"
#include <cstring>

/**
 * 无参构造函数
 */
MyString::MyString() : _size(10) {
  this->_str = new char[_size];
  strcpy(_str, "");
}

/**
 * 有参构造函数
 */
MyString::MyString(const char *s) {
  this->_size = strlen(s);
  this->_str = new char[_size + 1];
  strcpy(_str, s);
}

/**
 * 拷贝构造函数
 */
MyString::MyString(const MyString &other) {
  this->_str = new char[other._size];
  strcpy(this->_str, other._str);
}

/**
 * 析构函数
 */
MyString::~MyString() { delete this->_str; }

/**
 * 判断空
 */
bool MyString::empty() { return this->size() == 0; }

/**
 * 获取长度
 */
long long int MyString::size() { return strlen(this->_str); }

/**
 * 获取 C 字符串
 */
char *MyString::c_str() { return this->_str; }

/**
 * 获取下标字符
 */
char &MyString::at(int pos) {
  if (pos < 0 || pos > this->_size) {
    return this->_str[0];
  }
  return this->_str[pos];
}

/**
 * 拷贝赋值函数
 */
MyString &MyString::operator=(const MyString &other) {
  if (this != &other) {
    this->_size = other._size;
    strcpy(this->_str, other._str);
  }
  return *this;
}

// 操作符重载 -------------------------------------------------------------

char &MyString::operator[](int pos) const {
  // if pos > size || pos < 0
  return this->_str[pos];
}

const MyString MyString::operator+(const char *other) const {
  MyString s = MyString(this->_str);
  s._size += strlen(other);
  strcat(s._str, other);
  return s;
}

const MyString MyString::operator+(const MyString &other) const {
  return *this + other._str;
}

bool MyString::operator==(const char *other) const {
  for (int i = 0; other[i] != '\0'; i++) {
    if (this->_str[i] != other[i])
      return false;
  }
  return true;
}

bool MyString::operator==(const MyString &other) const {
  return other == this->_str;
}

bool MyString::operator!=(const char *other) const { return !(*this == other); }

bool MyString::operator!=(const MyString &other) const {
  return other != this->_str;
}

bool MyString::operator>(const char *other) const {
  const int max_size = this->_size > strlen(other) ? this->_size : strlen(other);
  for (int i = 0; i < max_size; i++) {
    if (this->_str[i] > other[i])
      return true;
    else if (this->_str[i] < other[i])
      return false;
  }
  return false;
}

bool MyString::operator>=(const char *other) const {
  return this->operator==(other) || this->operator>(other);
}

bool MyString::operator<=(const char *other) const {
  return !(this->operator>(other));
}

bool MyString::operator<(const char *other) const {
  return !(this->operator>=(other));
}

bool MyString::operator>(const MyString &other) const {
  return other < this->_str;
}

bool MyString::operator>=(const MyString &other) const {
  return other <= this->_str;
}

bool MyString::operator<=(const MyString &other) const {
  return other >= this->_str;
}

bool MyString::operator<(const MyString &other) const {
  return other < this->_str;
}

// MyString 作为右操作数的运算符重载 ---------------------------------------

bool operator==(const char *l, const MyString &r) { return r == l; }
bool operator!=(const char *l, const MyString &r) { return r != l; }
bool operator>(const char *l, const MyString &r) { return r < l; }
bool operator>=(const char *l, const MyString &r) { return r <= l; }
bool operator<(const char *l, const MyString &r) { return r > l; }
bool operator<=(const char *l, const MyString &r) { return r >= l; }
MyString operator+(const char *l, const MyString &r) { return MyString(l) + r; }

std::ostream &operator<<(std::ostream &l, const MyString &r) {
  l << r._str;
  return l;
}

std::istream &operator>>(std::istream &l, const MyString &r) {
  l >> r._str;
  return l;
}
cpp
#include "MyString.h"
#include <iostream>

using namespace std;

// 用于测试
int main() {
  cout << boolalpha;
  MyString s1;                                   // MyString()
  cout << "1.  [true]\t" << s1.empty() << endl;  // empty()
  s1 = "hello";                                  // operator=()
  cout << "2.  [hello]\t" << s1.c_str() << endl; // c_str() 函数
  cout << "3.  [    5]\t" << s1.size() << endl;  // size() 函数
  s1.at(3) = 'm';                                // at() 函数
  cout << "4.  [helmo]\t" << s1 << endl;         //
  MyString s2(s1);                               // 拷贝构造函数
  cout << "5.  [helmo]\t" << s2 << endl;         //
  MyString s3 = "world";                         // 拷贝构造函数
  cout << "6.  [world]\t" << s3 << endl;         //
  s2 = s3;                                       // 拷贝赋值函数
  cout << "7.  [world]\t" << s2 << endl;         //
  MyString s4 = s1 + s3;                         // operator+(MyString)
  cout << "8.  [hemloworld]" << s4 << endl;      //
  cout << "9.  [   10]\t" << s4.size() << endl;  //
  MyString s5 = s1 + "!";                        // operator+(char *)
  cout << "10. [helmo!]\t" << s5 << endl;        //
  cout << "11. [    l]\t" << s1[2] << endl;      // operator[]()
  bool b1 = s1 == "helmo";                       // operator==(char *)
  cout << "12. [true]\t" << b1 << endl;          //
  bool b2 = s1 == MyString("hellomworlq");       // operator==(MyString)
  cout << "13. [false]\t" << b2 << endl;         //
  bool b3 = s1 != "hellomworld!";                // operator!=(char *)
  cout << "14. [true]\t" << b3 << endl;          //
  bool b4 = s1 != MyString("helmo");             // operator!=(MyString)
  cout << "15. [false]\t" << b4 << endl;         //
  bool b5 = "hello" == MyString("hello");        //
  cout << "16. [true]\t" << b5 << endl;          // operator==(char *, MyString)
  bool b6 = "hello" != MyString("hello");        //
  cout << "17. [false]\t" << b6 << endl;         // operator!=(char *, MyString)
  MyString s6 = "hello" + MyString("world");     // operator+(char *, MyString)
  cout << "18. [helloworld]" << s6 << endl;      //
  bool b7 = s1 > "aelmo";                        // operator>(char *)
  cout << "19. [true]\t" << b7 << endl;          //
  bool b8 = s1 > MyString("helmo");              // operator>(MyString)
  cout << "20. [false]\t" << b8 << endl;         //
  bool b9 = s1 >= "helmo";                       // operator>=(char *)
  cout << "21. [true]\t" << b9 << endl;          //
  bool b10 = s1 >= MyString("b");                // operator>=(MyString)
  cout << "22. [true]\t" << b10 << endl;         //
  bool b11 = s1 < "h";                           // operator<(char *)
  cout << "23. [false]\t" << b11 << endl;        //
  bool b12 = s1 < MyString("hela");              // operator<(MyString)
  cout << "24. [true]\t" << b12 << endl;         //
  bool b13 = s1 <= "i";                          // operator<=(char *)
  cout << "25. [true]\t" << b13 << endl;         //
  bool b14 = s1 <= MyString("helmoX");           // operator<=(MyString)
  cout << "26. [true]\t" << b14 << endl;         //
  bool b15 = "abc" > MyString("ab");             // operator>(char *, MyString)
  cout << "27. [true]\t" << b15 << endl;         //
  bool b16 = "xyz" < MyString("xyzz");           // operator<(char *, MyString)
  cout << "28. [true]\t" << b16 << endl;         //
  bool b17 = "abc" >= MyString("abc");           // operator>=(char *, MyString)
  cout << "29. [true]\t" << b17 << endl;         //
  bool b18 = "xyz" <= MyString("xyzz");          // operator<=(char *, MyString)
  cout << "30. [true]\t" << b18 << endl;         //
  return 0;
}

Last updated: