C + + "String class" string deletes a single character, removing the function implementation of a string

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.