#include <iostream> #include <cassert> using namespace std; class String {
public: typedef const char* const_Iterator; typedef char* Iterator; Iterator
begin() { return _str; } Iterator end() { return _str + _size; } const_Iterator
Begin() const { return _str; } const_Iterator End() const { return _str + _size;
} String(const char* str = "") // String has '\0’ It can also be written as "\0" :_str(new char[strlen(str) +
1]) { strcpy(_str, str); // while(*dst++ = *src++) _size = strlen(str); //
no need +1.'\0' Not counting its valid characters _capacity = _size; // no need +1, How many valid characters can be saved at most , The real space is bigger than _capacity Big one
} ~String() { delete[] _str; _str = nullptr; _size = _capacity = 0; } String(
const String&s) :_str(new char[s._size + 1]) , _size(s._size) // open Space of the same value ,
_capacity(s._size) // open Space of the same value { strcpy(_str, s._str); } const char* c_str() {
return _str; } String& operator=(String& s) { if (this != &s) { delete[] _str;
_str= new char[s._size + 1]; strcpy(_str, s._str); _size = s._size; _capacity =
s._capacity; } return *this; } const char& operator[](size_t pos) const { return
_str[pos]; } void push_back(char c) { if (_size == _capacity) { size_t
newcapcity= _capacity == 0 ? 2 : _capacity*2 ; char* str = new char[newcapcity +
1]; strcpy(str, _str); delete[] _str; _str = str; _capacity = newcapcity; } _str
[_size] = c; _size++; _str[_size] = '\0'; } void reserve(size_t n) // out of data , Open space {
if (n > _capacity) { char* tmp = new char[n + 1]; strcpy(tmp, _str); delete[]
_str; _str = tmp; _capacity = n; } } void append(const char* str) { int len =
strlen(str); if (strlen(str) + _size > _capacity) { char* tmp = new char[strlen(
str) + _size + 1]; strcpy(tmp, _str); delete[] _str; _str = tmp; _capacity =
strlen(str) + _size; } // Method 1 strcpy(_str + _size, str); // Method 2 //for (int i =
_size; i < _size + len; i++) //{ // _str[i] = str[i - _size]; //} _size += len;
_str[_size] = '\0'; } const String &operator += (char ch) { push_back(ch);
return *this; } const String& operator += (const char* str) { append(str);
return *this; } const String& operator += (const String& s) { append(s._str);
return *this; } void insert(size_t pos, char ch) { assert(pos < _size); if (
_size== _capacity) { reserve(_capacity*2); } int end = _size; while (end >= (int
)pos) { _str[end + 1] = _str[end]; --end; } _str[pos] = ch; ++_size; } void
insert(size_t pos, const char* str) { assert(pos <= size); size_t len = strlen(
str); if (_size + 1 > _capacity) { reserve(_size + len); } size_t end = _size +
len; while (end >= pos + len) { _str[end] = _str[end - len]; --end; } while (*
str) { _str[pos++] = *str++; } _size += len; } bool operator>(const String& s) {
const char* str1 = _str; const char* str2 = s._str; while (*str1 && *str2) { if
(*str1 > *str2) { return true; } else if (*str1 < *str2) { return false; } else
{ ++str1; ++str2; } } if (*str1) { return true; } else if (*str2) { return false
; } else { return false; } } bool operator<(const String& s) { return !(*this >
s); } bool operator ==(const String& s) { const char* str1 = _str; const char*
str2= s._str; while (*str1 && *str2) { if (*str1 != *str2) { return false; }
else { ++str1; ++str2; } } if (*str1 || *str2) return false; return true; } bool
operator>=(const String& s) { return *this > s || *this == s; } size_t size() {
return _size; } size_t capacity() { return _capacity; } private: size_t _size;
size_t _capacity; char* _str; };
<> Modern writing
// Implement a simple string Modern writing class MyString { private: char* _str; public: MyString(
const char* str = "") :_str(new char[strlen(str) + 1]) { strcpy(_str, str); }
MyString(const MyString& s) :_str(nullptr) //delete The space released should be empty { MyString tmp(s.
_str); swap(tmp._str, _str); } // Style 1 : MyString& operator=(const MyString& s) {
if (this != &s) { MyString tmp(s); swap(tmp._str, _str); } return *this; } // Writing method 2
//s1 = s2 //s In fact s2 A copy , namely s1 What you want //s Out of scope s Of _str Released MyString& operator=(
MyString s) { swap(_str, s._str); return *this; } };

Technology
©2019-2020 Toolsou All rights reserved,
Gude Haowen serial - You deserve to be an engineer ( Preface ) A single key controls multiple water lamp states Bitcoin in ten years ,VDS Opportunity or fraud CSS architecture design Programmer Tanabata Valentine's Day confession code Python+OpenCV Detailed explanation of face recognition technology Bug Can data be used as the basis of technical personnel assessment KPI?Jsp+Ajax+Servlet+Mysql Add, delete, modify and query ( one ) Thorough explanation from Zhongtai Unity Scene loading asynchronously ( Implementation of loading interface )