#include <iostream> #include <stdlib.h> #include <assert.h>using namespace std;class string{public: string (CONST&NBSP;CHAR*&NBSP;STR) &NBSP;&NBSP;&NBSP;:_STR (New char[strlen (str) + 1]) { _size = strlen (str); _ capacity = _size + 1; strcpy (_str, str ); } string (const string& s) :_str (NULL) { string tmp (S._STR); swap (_STR,&NBSP;TMP._STR); } ~string () { if (_STR) { delete[] _str; _ size = 0; _capacity = 0; _str = NULL; } } //Delete a single character /*void erase (Size_t pos) { assert (pos <=_size); //pos>_size error int begin = pos; while (begin <= _size) { _str[begin] = _str[begin + 1]; begin++; } --_size; }*/ // Delete String void erase (Size_t pos, int len) { assert (pos <= _size); int len1 = len + 1; while (len1--) { _str[pos] = _str[pos + len +1]; pos++; } _size = _size - len; } char* c_str () { return _str; } private: char* _str; int _size; int _ capacity;}; Void test () { string s ("Abcdefghijk"); s.erase (2,4); cout << s.c_str () << endl;} Int main () { test (); system ("Pause"); return 0;}
C + + "String class" string deletes a single character, removing the function implementation of a string