1, Modern writing implementation interface
The first is the realization of modern writing method of copy construction:
string_str(const string_str& st) :str(nullptr) { string_str tem(st.str); swap(this->str, tem.str);
First, set this - > STR to null, then temp calls the constructor and initializes this - > STR with st.str to form a temporary object. Then swap this - > STR and tem.str through swap. In this way, the memory space pointed to by the two objects is exchanged, and TEM calls the destructor when the scope is out. This ensures that the copy constructor runs successfully.
Another way of writing:
string_str& operator=( string_str s) { swap(this->str, s.str); return *this; }
For the s value transfer operation, the address is not transferred, which has no impact on the argument structure. The referred spatial address is exchanged through swap.
operator = Modern Writing
// Compared with the above assignment, which implementation is better? string& operator=(string s) { swap(_str, s._str); return *this; }
This is similar to another writing method of copy construction. It passes the value of s without passing the address, which has no impact on the argument construction. The spatial address of the reference is exchanged through swap.
Another implementation method:
/* string& operator=(const string& s) { if(this != &s) { string strTmp(s); swap(_str, strTmp._str); } return *this; } */
Again, this is similar to the first implementation of the copy constructor. First, set this - > STR to null, then temp calls the constructor and initializes this - > STR with st.str to form a temporary object. Then swap this - > STR and tem.str through swap. In this way, the memory space pointed to by the two objects is exchanged, and TEM calls the destructor when the scope is out. This ensures that the copy constructor runs successfully.
2, Modern writing and other interface implementation
Input / output:
ostream& operator<<( string_str& str, ostream&out) { for (size_t i = 0; i < _size; i++) { out >> str[i]; } return out; } istream& operator<<(string_str& str, istream& in) { str[0] = '\0'; _size = 0; char ch; ch = in.get(); while (ch != ' ' && ch != '\0'){ str += ch; ch = in.get(); } return in; }
Strings cannot be output through cout < < endl and CIN > > endl, but by calling ostream & operator < < (string_str & STR, ostream & out) and istream & operator < < (string_str & STR, istream & in) to operate a single character to achieve string input and output.
The output of a string is actually traversal and printing of individual characters in the string one by one,
String input is a character insertion operation. It will end when a space or carriage return is encountered.
istream& getline( string_str& str, istream& in) { str.clear(); char ch; ch = in.get(); while ( ch != '\0') { str += ch; ch = in.get(); } return in; }
istream& getline( string_str& str, istream& in)
This function is basically the same as the bottom layer of operator > > except that it only encounters line feed end input.
Insert and delete:
void insert(size_t pos, char ch) { assert(pos >= 0 && pos <= _size); if (_size >= _capasity) { size_t num = _capasity == 0 ? 4 : 2 * _capasity; this->reserve(num); } int p = _size-1; /* for (size_t i = this->_size; i >= pos; i--) { this->str[i + 1] = this->str[i]; }*/ while (true) { this->str[p + 1] = this->str[p]; p--; if (p < (int)pos) { break; } } this->_size++; this->str[pos] = ch; } void insert(size_t pos,const char* str1) { assert(pos >= 0 && pos <= _size); size_t len = strlen(str1); if (_size + len > _capasity) { this->reserve(_size + len); } int p = _size - 1; /* for (size_t i = this->_size; i >= pos; i--) { this->str[i + 1] = this->str[i]; }*/ while (true) { this->str[p + len] = this->str[p]; p--; if (p < (int)pos+len) { break; } } int j = 0; for (size_t i = pos; i < pos + len; i++) { str[i] = str1[j++]; } _size += len; str[_size] = '\0'; }
Insert character and string function are function overloads. The basic idea is to move the pos position to the string of npos, and the insert character only moves one bit, while the string Yao moves the len bit to insert ch in str[pos], and the string is inserted circularly.
void erase(size_t pos, int len = -1) { assert(pos < _size&& pos >= 0); if (len == -1|| pos + len >= _size) { str[pos] = '\0'; _size = pos; } else { /* int num = pos; while (true) { str[num] = str[num + len]; num++; if (num + len == _size) { break; } } int p = _size - len; str[p] = '\0'; _size = p;*/ strcpy(str + pos, str + pos + len); int p = _size - len; str[p] = '\0'; _size -= len; } } ostream& oper
There are two situations for deleting strings;
1.len is npos or pos+len is greater than size, so \ 0 can be inserted in str[pos].
2.pos+len is less than or equal to size. Just test the string at str + pos + len to str + pos, and insert str[pos] into \ 0.