Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MyStringv2
//Title of this code #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class MyString { char* str; public: MyString(const char*); MyString(const MyString&); ~MyString(); MyString& operator+=(MyString&); MyString& operator+=(const char*); friend ostream& operator<<(ostream&, const MyString&); }; MyString::MyString(const char* s) { this->str = static_cast<char*>(malloc(sizeof(char) * (strlen(s) + 1))); strcpy(this->str, s); } MyString::MyString(const MyString& s) { this->str = static_cast<char*>(malloc(sizeof(char) * (strlen(s.str) + 1))); strcpy(this->str, s.str); } MyString::~MyString() { free(str); } MyString& MyString::operator+=(MyString& s) { *this += s.str; return *this; } MyString& MyString::operator+=(const char* s) { int newSize = strlen(this->str) + strlen(s) + 1; this->str = static_cast<char*>(realloc(this->str, newSize * sizeof(char))); strcpy(this->str + (strlen(str)), s); return *this; } MyString operator+(MyString s1, MyString s2) { MyString s = s1; return s += s2; } MyString operator+(const char* s1, MyString s2) { MyString s(s1); return s += s2; } MyString operator+(MyString s1, const char* s2) { MyString s(s1); return s += s2; } ostream& operator<<(ostream &output, const MyString &s) { output << s.str; return output; } int main() { MyString s1("abc"); MyString s2("def"); MyString s3 = s1 + s2 + "s"; cout << s1 << " + " << s2 << " = " << s3; }
run
|
edit
|
history
|
help
0
Pairs having sum equal to target
Finding Ocean
Problem D
Stock buy/sell, maximum subarray problem
Shorting in one line using class members std and boost bind
unicodeのテスト
Palindromo
ContainerVector
Dar
Lockable static queue