Simulation of string in C + + implementation _c language

Source: Internet
Author: User
Tags strlen

The string class in C + + can implement a series of operations on a string object, as shown in the following diagram as part of a string that is intercepted from the Cplusplus:

Next I'll simply simulate a few function implementations

First, we're going to give the complete string class, including constructors, destructors, private member char* STR

and declare the function to implement in the class (this article I only implemented operator=,operator[],pushback (), and three operator+=, five inserts, etc.)

#include <iostream> #include <cstring> using namespace std; Class String {friend ostream& operator<< (ostream& os,string& s); public:string (const char* str = ""  //constructor: _SZ (strlen (str)), _capacity (strlen (str) +1), _str (new Char[strlen (str) +1]) {cout<< "String (const char*
 STR) "<<endl;
 strcpy (_STR,STR); } String (const string& s)//copy constructs: _SZ (S._SZ), _capacity (strlen (S._STR) +1), _str (New Char[strlen (S._STR) +1]) {Co
 ut<< "String (const string& s)" <<endl;
 strcpy (_STR,S._STR);
 } ~string () {cout<< "~string ()" <<endl;
 if (*_str!= NULL) {delete[] _str;
 _str = NULL;
 _SZ = 0;
 _capacity = 0;
 } string& operator= (const string& s);
 string& operator= (const string& s);
 char& operator[] (int index);
 void Push_back (char c);
 string& operator+= (const string& str);
 string& operator+= (const char* s);
 string& operator+= (char c); string& Insert (Size_t POS, const string& str);
 string& Insert (size_t pos1, const string& STR, size_t pos2, size_t N);
 string& Insert (size_t POS, const char* S, size_t N);
 string& Insert (size_t POS, const char* s);
string& Insert (size_t pos, size_t N, char c);
 private:char* _str;
 int _SZ;
int _capacity;
 };

Implement each function outside the class separately:

1. operator=

Assign a value to a new object with a known object (the effect is the same as the copy construct, but the implementation is different): implementing this operator overload should pay attention to determining whether two objects are equal, and that parameter and return values are reference types can increase efficiency

string& string::operator= (const string& s)
{
 if (this!= &s)//Determine whether two objects are equal to
 {
 delete[] _str;
 _str = new Char[strlen (S._STR) +1];
 strcpy (_STR,S._STR);
 _SZ = S._SZ;
 _capacity = s._capacity;
 }
 return *this;
}

An assignment operator overload also has a simpler practice of swapping the string pointers of two objects directly

string& string::operator= (const string& s)
{
 std::swap (_STR,S._STR);
 return *this;
}

2. operator[]

overloaded array coordinates [] is simpler and returns the coordinates of the object directly

char& string::operator[] (int index)
{return
 _str[index];
}

Because _STR is a pointer to the string, _str[index is a dereference of the character labeled Index, so the return value type is char

3. Push_back ()

To insert an element at the end of a character array, you must first think about the problem of capacity, so you need to define two members _SZ,_CAPACITY,_SZ the current capacity, _capacity represents the number of times to enlarge the capacity, and then you can use a checkcapacity () function to resolve capacity issues, including detection and augmentation. Because the function is automatically detected when it is used, it does not need to be known to the external user, so you can declare the function as private in the class

Private:
 void checkcapacity (int count)
 {
 if (_sz+count >= _capacity)
 {
 int newcapacity = 2* _capacity > (_capacity + count)? 2*_capacity: (_capacity+count);
 char* temp = new char[newcapacity];
 strcpy (TEMP,_STR);
 Delete[] _STR;
 _str = temp;
 _capacity = newcapacity;
 }
 

Solve the problem of capacity, then you can insert it, just insert the character to the last position and add ' I '.

void String::P ush_back (char c)
{
 checkcapacity (1);
 _str[_sz++] = C;
 _STR[_SZ] = ' I ';
}

4. operator+=

Cases:

 String str1 ("abcdef");
 String str2 ("123456");
 STR1 + = str2;  ------------>>abcdef123456
 str1 + = "123456";---------->>abcdef123456
 str1 + = ' L ';  ----------->>ABCDEFL

If it is a string, it can be appended to the previous string by using the Strcat function, and if it is a character, the processing method is similar to the push_back

string& string::operator+= (const string& str)
{
 checkcapacity (strlen (STR._STR));
 strcat (_STR,STR._STR);
 _SZ + + strlen (STR._STR);
 _STR[_SZ] = ' the ';
 return *this;
}
string& string::operator+= (const char* s)
{
 checkcapacity (strlen (s));
 strcat (_str,s);
 _SZ + = strlen (s);
 _STR[_SZ] = ' the ';
 return *this;
}
string& string::operator+= (char c)
{
 checkcapacity (1);
 _str[_sz++] = C;
 _STR[_SZ] = ' the ';
 return *this;
}

5. Insert

Cases:

String str1 ("12345");
 String str2 ("ABCDE");
 string& Insert (size_t pos, const string& str); Str1.insert (3,STR2);
 ------>> 12abcde345//string& Insert (size_t pos1, const string& STR, size_t pos2, size_t N);
 Str1.insert (2,str2,3,2);----->>1cd2345//string& Insert (size_t POS, const char* S, size_t N);
 Str1.insert (2, "abcdef", 3);----->>1abc2345//string& Insert (size_t POS, const char* s);
 Str1.insert (4, "abc");----->>123abc45//string& Insert (size_t pos, size_t N, char c); Str1.insert (2,5, ' C ');------>>1ccccc2345 string& String::insert (size_t pos, const string& str) {if (P
 os>0) && (Pos<=strlen (_STR))//Judge Pos is reasonable {checkcapacity (strlen (STR._STR));
 char* tmp = new Char[strlen (_STR)-pos+2]; strcpy (TMP,_STR+POS-1);
 Save _str POS bit and later//we can use the Strcat function to append a string, but only if the previous string (STR1) is terminated with the "" and "_str[pos-1" = ';
 strcat (_STR,STR._STR);
 strcat (_STR,TMP);
 _SZ + + strlen (STR._STR); _sTR[_SZ] = ' the ';
 Delete[] tmp;
return *this; } string& String::insert (size_t pos1, const string& STR, size_t pos2, size_t N) {if (pos1>0) && p Os1<=strlen (_STR))//To determine if POS1 is reasonable {if (pos2>0) && (Pos2<=strlen (STR._STR))//To determine if POS2 is reasonable {if (n>0
 ) && (N<=strlen (STR._STR)-pos2))//Check if n is reasonable {checkcapacity (n);
 char* tmp = new Char[strlen (_STR)-pos1+2]; strcpy (tmp,_str+pos1-1);
 Save the POS1 bit of _str and the content after it _str[pos1-1] = ';
 Strncat (_str,str._str+pos2-1,n);
 strcat (_STR,TMP);
 _SZ + + strlen (STR._STR);
 _STR[_SZ] = ' the ';
 Delete[] tmp;
}} return *this; } string& String::insert (size_t pos, const char* S, size_t N) {if (pos>0) && (Pos<=strlen (_STR)))//
 To judge whether Pos is reasonable {checkcapacity (n);
 char* tmp = new Char[strlen (_STR)-pos+2]; strcpy (TMP,_STR+POS-1);
 Save _str POS bit and after content _str[pos-1] = ';
 Strncat (_str,s,n);
 strcat (_STR,TMP);
 _SZ + = strlen (s);
 _STR[_SZ] = ' the ';
 Delete[] tmp;
return *this; } String& String::insert (size_t pos, const char* s) {if ((pos>0) && (Pos<=strlen (_STR))//Judge Pos Reasonable {Che
 Ckcapacity (strlen (s));
 char* tmp = new Char[strlen (_STR)-pos+2]; strcpy (TMP,_STR+POS-1);
 Save _str POS bit and after content _str[pos-1] = ';
 strcat (_str,s);
 strcat (_STR,TMP);
 _SZ + = strlen (s);
 _STR[_SZ] = ' the ';
 Delete[] tmp;
return *this; } string& String::insert (size_t pos, size_t N, char c) {if (pos>0) && (Pos<=strlen (_STR))//Judge Pos is
 No reasonable {checkcapacity (n);
 int i = 1;
 char* tmp = new Char[strlen (_STR)-pos+2];
 strcpy (tmp,_str+pos-1);//2345//_str[pos-1] = ';
 For (I=1 i<=n; i++) {_str[i] = C;
 } _str[i] = ';
 strcat (_STR,TMP);
 _SZ + = n;
 _STR[_SZ] = ' the ';
 Delete[] tmp;
return *this;
 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.