Skip to content

Instantly share code, notes, and snippets.

@KireinaHoro
Created March 24, 2018 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KireinaHoro/89b9d1cfcaa834702ec76dd3c76a52ac to your computer and use it in GitHub Desktop.
Save KireinaHoro/89b9d1cfcaa834702ec76dd3c76a52ac to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
using namespace std;
#if 0
int strlen(const char *s) {
int i = 0;
for (; s[i]; ++i);
return i;
}
void strcpy(char *d, const char *s) {
int i = 0;
for (i = 0; s[i]; ++i)
d[i] = s[i];
d[i] = 0;
}
int strcmp(const char *s1, const char *s2) {
for (int i = 0; s1[i] && s2[i]; ++i) {
if (s1[i] < s2[i])
return -1;
else if (s1[i] > s2[i])
return 1;
}
return 0;
}
void strcat(char *d, const char *s) {
int len = strlen(d);
strcpy(d + len, s);
}
#endif
class MyString {
// Your Code Here
public:
MyString() = default;
MyString(MyString const &x) {
ptr = nullptr;
copyWithCheck(x.ptr);
}
MyString(void const *p) {
ptr = nullptr;
copyWithCheck(static_cast<char const *>(p));
}
~MyString() {
delete[]ptr;
}
bool operator<(MyString const &a) const {
return ::strcmp(ptr, a.ptr) < 0;
}
bool operator==(MyString const &a) const {
return ::strcmp(ptr, a.ptr) == 0;
}
bool operator>(MyString const &a) const {
return a < *this;
}
MyString &operator=(MyString const &a) {
copyWithCheck(a.ptr);
return *this;
}
MyString operator+(MyString const &a) const {
MyString ret;
auto leftLen = ::strlen(ptr);
auto rightLen = ::strlen(a.ptr);
auto newSpc = new char[leftLen + rightLen + 1];
memset(newSpc, 0, sizeof(char) * (leftLen + rightLen + 1));
::strcat(newSpc, ptr);
::strcat(newSpc, a.ptr);
ret.ptr = newSpc;
return ret;
}
MyString &operator+=(MyString const &a) {
*this = *this + a;
return *this;
}
MyString operator()(size_t start, size_t length) const {
MyString ret(ptr + start);
ret.ptr[length + 1] = '\0';
return ret;
}
char &operator[](size_t n) const {
return ptr[n];
}
friend ostream &operator<<(ostream &os, MyString const &a) {
if (a.ptr == nullptr) { return os; };
return os << a.ptr;
}
friend MyString operator+(char const *x, MyString const &a) {
return MyString(x) + a;
}
private:
char *ptr;
void copyWithCheck(const char *src) {
auto newLen = ::strlen(src);
if (ptr != nullptr) {
auto currLen = ::strlen(ptr);
if (currLen < newLen) {
delete[]ptr;
ptr = new char[newLen + 1];
}
} else {
ptr = new char[newLen + 1];
}
memset(ptr, 0, sizeof(char) * (newLen + 1));
::strcpy(ptr, src);
}
static void memset(void *dst, char obj, size_t sz) {
auto charDst = static_cast<char *>(dst);
while (sz-- > 0) {
*charDst++ = obj;
}
}
// Your Code Ends Here
};
int CompareString(const void *e1, const void *e2) {
MyString *s1 = (MyString *) e1;
MyString *s2 = (MyString *) e2;
if (*s1 < *s2)
return -1;
else if (*s1 == *s2)
return 0;
else if (*s1 > *s2)
return 1;
}
int main() {
char *a = nullptr;
cout << *a << endl;
MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
MyString SArray[4] = {"big", "me", "about", "take"};
cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray, 4, sizeof(MyString), CompareString);
for (int i = 0; i < 4; i++)
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0, 4) << endl;
//s1的从下标5开始长度为10的子串
cout << s1(5, 10) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment