Created
April 24, 2019 14:38
-
-
Save kosbar/275f0d82e9ca5875f97c987d1f8f82f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct String { | |
| String(const char *str = "") { | |
| this->str = new char[strlen(str)+1]; | |
| strcpy(this->str, str); | |
| this->str[strlen(str) + 1] = 0; | |
| this->size = strlen(str)+1; | |
| }; | |
| String(const String& other) { | |
| this->str = new char[other.size]; | |
| strcpy(this->str, other.str); | |
| *(this->str + strlen(other.str) + 1) = 0; | |
| this->size = strlen(other.str) + 1; | |
| }; | |
| String& operator=(const String& other) { | |
| if (this != &other) { | |
| delete[] str; | |
| this->size = other.size; | |
| this->str = new char[other.size]; | |
| strcpy(this->str, other.str); | |
| return *this; | |
| } | |
| return *this; | |
| }; | |
| //void append(const String& other); | |
| size_t size; | |
| char *str; | |
| int first = 0; | |
| String& operator[](int i) { | |
| if (!first && *str) { | |
| first = i; | |
| // | |
| } | |
| if (first && *str) { | |
| char* sstr = new char[i]; | |
| char* sstr2 = new char[i-first]; | |
| strncpy(sstr, str, i); | |
| *(sstr + i) = 0; | |
| strcpy(sstr2, sstr + first); | |
| delete[] str; | |
| delete[] sstr; | |
| str = sstr2; | |
| *(sstr + strlen(sstr2)) = 0; | |
| size = strlen(sstr2)+1; | |
| return *this; | |
| } | |
| } | |
| }; | |
| int main() | |
| { | |
| String s("Hello"); | |
| s[0][4]; | |
| cout << s.str; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment