String (Code)
June 5, 2018
实现了大部分基础的string操作,重载了”-“运算符,”!”反转运算符
算是一次小小的练手吧
头文件:
#include <iostream> using namespace std; class TString { private: char *content; int len; public: TString(); TString(const char *p); TString(const TString &p); ~TString(); friend ostream &operator<<(ostream &out, const TString &p); friend istream &operator>>(istream &in, TString &p); TString& operator=(const TString &p); TString& operator=(const char *p); friend bool operator!=(const TString&, const TString&); friend bool operator==(const TString&, const TString&); friend TString operator+(const TString&, const TString&); friend TString operator+(const TString&, const char&); friend TString operator+(const char&, const TString&); friend TString operator+(const TString&, const char*); friend TString operator+(const char*, const TString&); const char* c_str() const; TString& operator+=(const TString&); TString& operator+=(const char&); friend TString operator-(const TString&, const TString&); friend int find(const TString&, const TString&); char& operator[](const int); const char& operator[](const int)const; TString& erase(int, int); friend TString operator-(const TString&, const char&); friend TString operator-(const TString&, const char*); TString& operator-=(const TString&); TString& operator-=(const char&); friend TString operator*(TString&, const int&); TString& operator*=(const int&); friend TString operator!(TString&); friend bool operator<(const TString&, const TString&); friend bool operator<=(const TString&, const TString&); friend bool operator>(const TString&, const TString&); friend bool operator>=(const TString&, const TString&); int length(); friend int TStringLen(const TString &); };
具体实现:
#include <iostream> #include <stdlib.h> #include <string.h> #include "TString.h" using namespace std; TString::TString() { content = NULL; len = 0; } TString::TString(const char *p) { if (p == NULL) { content = NULL; len = 0; return; } len = strlen(p); if (len == 0) content = NULL; else { content = new(nothrow) char[len + 1]; if (content == NULL) len = 0; else memcpy(content, p, len + 1); } } TString::TString(const TString &p) { if (p.content == NULL) { content = NULL; len = 0; return; } len = p.len; if (len == 0) { content = NULL; return; } content = new(nothrow) char[len + 1]; memcpy(content, p.content, len + 1); } TString::~TString() { if (content != NULL) delete[]content; } ostream &operator<<(ostream &out, const TString &p) { if (p.content == NULL) { out << "<NULL>"; return out; } out << p.content; return out; } istream &operator>>(istream &in, TString &p) { char tmp[2049]; in >> tmp; p.len = strlen(tmp); p.content = new(nothrow) char[p.len + 1]; if (p.content == NULL) p.len = 0; else memcpy(p.content, tmp, p.len + 1); return in; } bool operator!=(const TString& a, const TString& b) { if (a.len == 0 && b.len == 0) return false; if (a.len != b.len) return true; if (strcmp(a.content, b.content) != 0) return true; return false; } bool operator==(const TString& a, const TString& b) { if (a.len == 0 && b.len == 0) return true; if (a.len != b.len) return false; if (strcmp(a.content, b.content) == 0) return true; return false; } TString &TString::operator=(const TString &a) { if (content == a.content) return *this; if (content) delete[]content; if (a.content) { len = a.len; content = new(nothrow) char[len + 1]; memcpy(content, a.content, len + 1); return *this; } else { content = NULL; len = 0; return *this; } } TString &TString::operator=(const char *p) { if (content == p) return *this; if (content) delete[]content; if (p&&*p) { len = strlen(p); content = new(nothrow) char[len + 1]; memcpy(content, p, len + 1); return *this; } else { content = NULL; len = 0; return *this; } } TString operator+(const TString&a, const TString&b) { TString c; c.len = a.len + b.len; if (c.len == 0) { c.content = NULL; return c; } c.content = new(nothrow) char[c.len + 1]; if (c.len == 0) { c.content = NULL; return c; } memcpy(c.content, a.content, a.len); memcpy(&c.content[a.len], b.content, b.len); c.content[c.len] = '\0'; return c; } TString operator+(const TString&a, const char&b) { TString c; c.len = a.len + 1; c.content = new(nothrow) char[c.len + 1]; if (c.len == 0) { c.content = NULL; return c; } memcpy(c.content, a.content, a.len); c.content[a.len] = b; c.content[a.len + 1] = '\0'; return c; } TString operator+(const char&b, const TString&a) { TString c; c.len = a.len + 1; c.content = new(nothrow) char[c.len + 1]; if (c.len == 0) { c.content = NULL; return c; } c.content[0] = b; memcpy(&c.content[1], a.content, a.len + 1); return c; } TString operator+(const TString&a, const char*b) { if (b == NULL) return a; TString c; int lenb = strlen(b); c.len = a.len + lenb; if (c.len == 0) { c.content = NULL; return c; } c.content = new(nothrow) char[c.len + 1]; if (c.len == 0) { c.content = NULL; return c; } memcpy(c.content, a.content, a.len); memcpy(&c.content[a.len], b, lenb); c.content[c.len] = '\0'; return c; } TString operator+(const char*b, const TString&a) { if (b == NULL) return a; TString c; int lenb = strlen(b); c.len = a.len + lenb; if (c.len == 0) { c.content = NULL; return c; } c.content = new(nothrow) char[c.len + 1]; if (c.len == 0) { c.content = NULL; return c; } memcpy(c.content, b, lenb); memcpy(&c.content[lenb], a.content, a.len + 1); return c; } TString& TString::operator+=(const TString& c) { if (c.content == NULL) return *this; if (this == &c) { TString c2(*this); return *this += c2; } len += c.len; char *p_old = content; content = new char[len + 1]; memcpy(content, p_old, len - c.len); memcpy(&content[len - c.len], c.content, c.len + 1); delete[] p_old; return *this; } TString& TString::operator+=(const char& c) { len += 1; char *p_old = content; content = new char[len + 1]; memcpy(content, p_old, len - 1); content[len - 1] = c; content[len] = '\0'; delete[] p_old; return *this; } const char* TString::c_str() const { return content; } char& TString::operator[](const int index) { return content[index]; } const char& TString::operator[](const int index)const { return content[index]; } int find(const TString& a, const TString& b) //kmp算法 { int *p = new int[a.len + 1]; int n = a.len, m = b.len; int i = 0, j = -1; p[0] = -1; while (i<m) { while (j >= 0 && b[i] != b[j]) j = p[j]; i++,j++; p[i] = j; } i = 0, j = 0; while (i<n) { while (j >= 0 && a[i] != b[j]) j = p[j]; i++, j++; if (j == m) { delete []p; return i - j; } } delete[]p; return -1; } TString& TString::erase(int pos, int n) { char *p_old = content; len -= n; content = new char[len + 1]; for (int i = 0; i < pos; i++) content[i] = p_old[i]; for (int i = pos; i<len; i++) content[i] = p_old[i + n]; *(content + len) = '\0'; return *this; } TString operator-(const TString&a, const TString&b) { TString c = a; int k = find(a, b); if (k == -1) return a; c.erase(k, b.len); return c; } TString operator-(const TString&a, const char*b) { TString c = a; int k = find(a, b); if (k == -1) return a; c.erase(k, strlen(b)); return c; } TString operator-(const TString&a, const char&b) { TString c = a; for(int i=0;i<c.len;i++) if (c.content[i] == b) { c.erase(i, 1); return c; } return c; } TString &TString::operator-=(const TString &b) { *this = *this - b; return *this; } TString &TString::operator-=(const char &b) { *this = *this - b; return *this; } TString operator*(TString&a, const int&n) { int i = 1; TString c; for (int i = 1; i <= n; i++) c += a; return c; } TString &TString::operator*=(const int &n) { *this = *this * n; return *this; } TString operator!(TString& a) { TString c(a); for (int i = 0; i <= c.len / 2 - 1; i++) { char d = c.content[i]; c.content[i] = c.content[c.len - i - 1]; c.content[c.len - i - 1] = d; } return c; } bool operator<(const TString& a, const TString& b) { if (a.len == 0 && b.len == 0) return false; if (a.len == 0 && b.len != 0) return true; if (a.len != 0 && b.len == 0) return false; if (strcmp(a.content, b.content) < 0) return true; return false; } bool operator<=(const TString& a, const TString& b) { if (a.len == 0 && b.len == 0) return true; if (a.len == 0 && b.len != 0) return true; if (a.len != 0 && b.len == 0) return false; if (strcmp(a.content, b.content) <= 0) return true; return false; } bool operator>(const TString& a, const TString& b) { if (a.len == 0 && b.len == 0) return false; if (a.len == 0 && b.len != 0) return false; if (a.len != 0 && b.len == 0) return true; if (strcmp(a.content, b.content) > 0) return true; return false; } bool operator>=(const TString& a, const TString& b) { if (a.len == 0 && b.len == 0) return true; if (a.len == 0 && b.len != 0) return false; if (a.len != 0 && b.len == 0) return true; if (strcmp(a.content, b.content) >= 0) return true; return false; } int TString::length() { return len; } int TStringLen(const TString &a) { return a.len; }