Header file "String.h" #include <iostream> #include <assert.h>using namespace std;class string{public: string (const char* str = "") { int len = strlen (str); _capacity = len + 1; _size = len; _str = new char[_capacity]; strcpy (_STR,&NBSP;STR); } //interchange function void _swap (string& s) { swap (_STR,&NBSP;S._STR); swap (_size, s._size); Swap (_capacity, s._capacity); } string (const string& s) :_str (New char[1] ()) { string tmp = s._str; _ Swap (TMP); } //destructor ~string () { if (_STR) { delete[ ]_str; _str = null; } } void display ( ) { cout << _str << endl; } /********************************************************/ /*********************** Overloading individual operators *******************/ /**************** / string& Operator= (string s) { string _ Swap (s); return *this; } bool operator> (const string& s) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;STRCMP (_STR,&NBSP;S._STR) > 0 ? true : false; } bool operator< (const string & s) { return strcmp (_STR, &NBSP;S._STR) >0 ? false : true; } bool operator== (const String & s) { return strcmp (_STR, &NBSP;S._STR) == 0 ? true : false; } bool operator>= (const string& s) { &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;STRCMP (_STR,&NBSP;S._STR) >= 0 ? true : false; } bool operator<= (Const String& s) &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;STRCMP (_STR,&NBSP;S._STR) <= 0 ? true : false; } /****** / /*********************** Implementing string Common Functions ***********/ /************************************************************/ //Tail plug void pushback (char ch) { _checkcapacity (_size + 2); _str[_size++] = ch; _str[_size] = ' + '; } //pos position insertion character ch void Insert (size_t pos, char ch) { assert (pos <= _size); _checkcapacity (_size &NBSP;+&NBSP;2); int end = _size + 1; while (End >= pos) { _str[end] = _str[end - 1]; --end; } _str[pos - 1] = ch; ++_size; } /* int len = 1; _size += len; _str = _checkcapacity (); for (size_t i = _size; i >= pos; i++) { _str[i] = _str[i - 1]; } _str[pos - 1] = ch; */ //pos Position Inserts a string void inSERT (SIZE_T&NBSP;POS,&NBSP;CONST&NBSP;CHAR*&NBSP;STR) { assert (pos <= _size); size_t strsize = strlen (str); _checkcapacity (_size + 1 + strsize); int end = _ size; while (end >= (int) POS) { _ str[end + strsize] = _str[end]; --end; } while (*STR) { _str[pos++] = *str++; } _size += strsize; } char & operator[] (Size_t index) { return _str[index]; } //find a character int find (char ch) { for (size_t i = 0; i < _size; i++) { if (_str[i] == &NBSP;CH) { return i; } } return -1; } //find A String int find (CONST&NBSP;CHAR*&NBSP;STR) { const char* pscr = _str; const char* psub = str; int scrlen = _size; int sublen = strlen (str); int scrIndex = 0; while (Scrindex <= scrlen - sublen) { int i = scrindex; int j = 0; while (scrlen > i&&j < SUBLEN&&PSCR[I]&NBSP;==&NBSP;PSUB[J]) { i ++; j++; } if (J == sublen) { return scrindex; } scrindex++; } return -1; } //n characters after deleting pos position void erase (size_t pos, size_t n) { assert (pos < _size); for (size_t i = pos; i < _size; i++) { _str[i] = _str[i + n]; } _size -= n; } //Delete the pos position character void erase (Size_t pos) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; assert (pos < _size); for ( size_t i = pos; i < _size; i++) { _str[i] = _str[ i + 1]; } _size--; }private: //Check that the pointer is pointing to the size of the space and expanding void _checkcapacity (size_t size) { if (size >= _capacity) { int newCapacity = size > 2 * _capacity ? size : 2 * _capacity; _str = (char*) realloc (_str, newcapacity); _capacity = newCapacity; } } char *_str; //string Pointer size_t _size; //string Capacity size_t _capacity; //string space size };//Test Void test1 () { string s1 ("Hello world"); s1. Display (); string s2 (S1); s2. Display (); &NBSP;&NBSP;&NBSP;&NBSP;STRING&NBSP;S3 ("Change world"); s1 = S3; s1. Display ();} Void test2 () { string s1 ("Ello world"); s1. Insert (1, ' H'); s1. Display ();} Void test3 () { string s1 ("Hello rld"); char* str = "Wo"; s1. Insert (6,&NBSP;STR); s1. Display ();} Void test4 () { string s1 ("Hello"); cout << s1[0] << endl;} Void test5 () { string s1 ("Hello world"); cout << s1. Find ("ll") << endl;} Void test6 () { string s1 ("Hello world"); s1. Erase (2, 3); s1. Display ();} " Test.cpp "#include " String.h "Int main () { test1 (); test2 (); test3 (); test4 (); test5 (); test6 (); system ("PAuse "); return 0;}
This article from "Wooden Man" blog, declined reprint!
C + + String class