Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- class String{
- private:
- char* str = nullptr;
- size_t sz = 0;
- size_t buffer = 0;
- void increaseBuffer() {
- buffer *= 2;
- char* newstr = new char[buffer];
- memcpy(newstr, str, sz);
- delete[] str;
- str = newstr;
- }
- void decreaseBuffer() {
- buffer /= 2;
- char* newstr = new char[buffer];
- memcpy(newstr, str, sz);
- delete[] str;
- str = newstr;
- }
- public:
- String() {}
- String(const size_t n, const char c): str(new char[n]), sz(n), buffer(n) {
- memset(str, c, n);
- }
- String(const char* s): str(new char[strlen(s)]), sz(strlen(s)), buffer(strlen(s)) {
- memcpy(str, s, buffer);
- }
- String(const String& other): String(other.sz, '\0') {
- memcpy(str, other.str, sz);
- }
- ~String() {
- delete[] str;
- }
- size_t length() const {
- return sz;
- };
- void push_back(const char c) {
- if (sz == buffer) {
- increaseBuffer();
- }
- str[sz++] = c;
- }
- void pop_back() {
- if (sz <= (buffer / 4)) {
- decreaseBuffer();
- }
- sz--;
- }
- char& front() {
- return str[0];
- }
- const char& front() const {
- return str[0];
- }
- char& back() {
- return str[sz - 1];
- }
- const char& back() const {
- return str[sz - 1];
- }
- size_t find(const String& other) const {
- int flag = 1;
- for (size_t i = 0; i < sz; ++i) {
- for (size_t j = 0; j < other.length(); ++j) {
- if (i + j >= sz || str[i + j] != other[j]) {
- flag = 0;
- break;
- }
- }
- if (flag == 1) {
- return i;
- }
- flag = 1;
- }
- return sz;
- }
- size_t rfind(const String& other) const {
- int flag = 1;
- for (size_t i = 0; i < sz; ++i) {
- for (size_t j = 0; j < other.length(); ++j) {
- if (sz - 1 - i + j >= sz || str[sz - 1 - i + j] != other[j]) {
- flag = 0;
- break;
- }
- }
- if (flag == 1) {
- return sz - 1 - i;
- }
- flag = 1;
- }
- return sz;
- }
- String substr(size_t start, size_t count) const {
- String ans(count, '\0');
- for (size_t i = 0; i < count; ++i) {
- ans[i] = str[start + i];
- }
- return ans;
- }
- bool empty() const {
- return !sz;
- }
- void clear() {
- delete[] str;
- str = new char[1];
- sz = 0;
- buffer = 1;
- }
- String& operator=(const String& other) {
- String copy = other;
- sz = copy.sz;
- buffer = copy.buffer;
- delete[] str;
- str = new char[buffer];
- memcpy(str, copy.str, sz);
- return *this;
- }
- char& operator[](const size_t pos) {
- return str[pos];
- }
- const char& operator[](const size_t pos) const {
- return str[pos];
- }
- String& operator+=(const String& rhs) {
- for (size_t i = 0; i < rhs.sz; ++i) {
- push_back(str[i]);
- }
- return *this;
- }
- String& operator+=(const char rhs) {
- push_back(rhs);
- return *this;
- }
- };
- bool operator==(const String& lhs, const String& rhs) {
- if (lhs.length() != rhs.length()) {
- return false;
- }
- for (size_t i = 0; i < lhs.length(); ++i) {
- if (lhs[i] != rhs[i]){
- return false;
- }
- }
- return true;
- }
- String operator+(const char first, const String& second) {
- String copy = second;
- copy += first;
- return copy;
- }
- String operator+(const String& first, const char second) {
- return second + first;
- }
- String operator+(const String& first, const String& second) {
- String copy = first;
- copy += second;
- return copy;
- }
- std::ostream& operator<<(std::ostream& out, const String& s) {
- for (size_t i = 0; i < s.length(); ++i){
- out << s[i];
- }
- return out;
- }
- std::istream& operator>>(std::istream& in, String& s) {
- char c;
- while (in >> c) {
- s.push_back(c);
- }
- return in;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement