Custom string interface and string Interface

Source: Internet
Author: User

Custom string interface and string Interface
Use char * to manage the memory of the String class. The new class is dynamically allocated. In the destructor, delete the new class memory pointed to by char, A string class needs to implement those interfaces can refer to the string in the standard library: http://www.cplusplus.com/reference/string/string/

  • The implementation idea is: re-create the memory and release the original memory when creating a String or a String that requires scaling (for example, you need to adjust the size when assigning values, and add strings to a larger space; the space of the string in the standard library is pre-allocated and there is extra space available. If the string needs to be scaled, a new memory space will be created.
  • > The reload method is to use cin. getline to read the space pointed to by the standard input to the character pointer. The space has a preset size, such as 100, which can receive strings containing spaces.
  • String Initialization is an empty String ("\ 0" or "" is equivalent). You also need a new character array to delete the String. If char * data = "\ 0" is used ", you cannot use delete to release memory that is not new.
  • [] Two Versions need to be reloaded. The const version const char & operator [] (size_t index) const; The this pointer of the const member function is implicitly converted to a pointer to a constant: operator (const String * this, size_t index), the const object can only call the const menber function
  • You do not need to reload the unreferenced * and get the address &. They also support custom types.
  • + = Operator + is called. Why does operator + return a reference instead of an object?
  • No reloaded relational operators are implemented
  • Compared with the standard library, there are few custom String-Implemented interfaces and it seems that nothing can be done. They can only be created, assigned values, outputs, access elements, or even be difficult to use in the development of string in the Standard Library: why do most open-source C ++ libraries prefer to implement a string by themselves?
  • Does memory leak exist in new memory management and is there an implicit error?
1 # include <iostream> 2 # include <string> 3 # include <vector> 4 # include <algorithm> 5 # include <cstdio> 6 # include <complex> 7 # include <new> 8 # include <memory> 9 # include <exception> 10 using namespace std; 11 // interfaces shocould supported 12 // use char * to manage memory 13 class String 14 {15 public: 16 String (); 17 String (const char * s ); 18 String (size_t n, const char c); // fill String 19 String (const String & s) with character c; 20 S Tring & operator = (const String & rhs); 21 String & operator = (const char * c); 22 char & operator [] (size_t index ); // The two versions of [] 23 const char & operator [] (size_t index) const; 24 String & operator * (); // It is unnecessary to dereference the overload, the language itself supports unreferencing all types of pointers, including the custom type 25 String & operator + (const String & rhs ); 26 String & operator + = (const String & rhs); 27 bool operator = (const String & rhs); 28 friend bool operator <(const String & lhs, const Stri Ng & rhs); 29 friend ostream & operator <(ostream & OS, const String & rhs); 30 friend istream & operator> (istream & in, String & rhs ); 31 size_t size (); // if it is size_t length (), it will cause ..... 32 bool empty (); 33 const char * c_str (); 34 ~ String (); 35 private: 36 char * data; 37 size_t length; 38 39}; 40 String: String () // menber function defined outside the class, domain identifier 41 {42 this-> data = new char [1] (); // empty string, strlen calculates the data Length as 0 43 data [0] = '\ 0'; 44 (* this ). length = 0; 45} 46 String: String (const char * s) // allow implicit type conversion 47 {48 // data = s is not supported; this causes the created String object to share characters with s, instead of the Copy 49 length = strlen (s); 50 data = new char [length + 1] (); // value initializatio N, applied for the memory area 51 strcpy (data, s) pointed by the character pointer; 52 * (data + length) = '\ 0'; 53} 54 String :: string (size_t n, const char c) 55 {56 length = n; 57 data = new char [length + 1] (); 58 for (size_t I = 0; I <length; I ++) 59 {60 * (data + I) = c; 61} 62 * (data + length) = '\ 0'; 63} 64 String:: String (const String & s) 65 {66 length = s. length; 67 data = new char [length + 1] (); // untill deconstructor to destory newe D memory 68 for (size_t I = 0; I <length; I ++) 69 {70 data [I] = s. data [I]; 71} 72 data [length] = '\ 0'; 73} 74 String & String: operator = (const String & rhs) 75 {76 if (this-> data = rhs. data) return * this; // does * this = rhs support link comparison? 77 78 // assign is not initialized and does not need to apply for memory space. The copy structure is required, but the size of the original String and rhs may be different. 79 delete [] data; // will the memory specified by data be deleted repeatedly ?? 80 81 length = rhs. length; 82 data = new char [length + 1] (); 83 // if an exception 84 85 if (data = NULL) throw "allocate memory failed in copy ctr "; 86 for (size_t I = 0; I <length; I ++) 87 {88 data [I] = rhs. data [I]; 89} 90 data [length] = '\ 0'; 91} 92 char & String: operator [] (size_t index) 93 {94 if (index> = length | index <0) throw "out of range"; 95 return data [index]; // If the string that data points to is "dhaj" and cannot be modified As a result, the returned reference cannot contain the s [I] = 'C' Modification Operation 96} 97 String & String: operator * () 98 {99 if (this = NULL) throw "dereferrence null pointer"; 100 return * this; 101} 102 String & String: operator + (const String & rhs) // a + B format, it does not change a103 {104 String s; // It cannot be scaled. The result 105 int len = strlen (this-> data) + strlen (rhs. data); 106 char * p = new char [len + 1] (); 107 for (size_t I = 0; I <strlen (this-> data); I ++) 108 {109 p [I] = this-> da Ta [I]; 110} 111 for (int j = strlen (this-> data), I = 0; j <len; j ++, I ++) 112 {113 p [j] = rhs. data [I]; 114} 115 p [len] = '\ 0'; 116 String * t = new String (p); // a new p, a t, you can release p because t is a copy of p. 117 delete [] p; 118 return * t; // returns the reference on the stack. After c = a + B is used, c will be destructed, but the invisible object in the middle will not be destructed 119} 120 String & String: operator + = (const String & rhs) 121 {122 * this = * this + rhs; 123 // release the original a and apply for a new space 124 return * this; 125} 126 bool String: operato R = (const String & rhs) // compare whether the values are equal, instead of the same 127 {128 if (this-> length! = Rhs. length) return false; 129 int I = 0; 130 for (; (* this ). data [I] = rhs. data [I]; I ++); 131 if (I! = (Rhs. length + 1) return false; 132 else return true; 133} 134 bool operator <(const String & lhs, const String & rhs) 135 {136 return true; 137} 138 ostream & operator <(ostream & OS, const String & rhs) // declare it as a friend function. The call method is cout <s, the Declaration is men fun, and the call form is s <cout139 {140 OS <rhs. data; // If the output is null, pointer will undefined the behavior 141 return OS; 142} 143 istream & operator> (istream & is, String & rhs) 144 {// enter a string to a character pointer. The character pointer needs to allocate a certain amount of space in advance. how to adjust the space size according to the input string by 145 delete [] rhs. 146 rhs. data = new char [50] (); 147 is. getline (rhs. data, 50); // the last position is automatically set to '\ 0'. Therefore, a maximum of 49 characters can be saved and a space of 148 rhs can be received. length = strlen (rhs. data); 149 return is; 150} 151 size_t String: size () 152 {153 Return length; 154} 155 bool String: empty () 156 {157 if (length = 0) return true; 158 else return false; 159} 160 const char * String :: c_str () // returns the reference pointing to the partial part of the object. pointer. Can I modify the original object? 161 {// if it is an empty String, return data162 return data; 163 164} 165 String ::~ String () 166 {// is the structure incorrect? 167 // The memory is new when the String is created. When the program ends or the local String object exits the scope, the Destructor is called to release the memory by 168 delete [] data; // If the String is not new, the pointer p points to it, and delete p will cause an error. If the String is new, you can call delete, and then call ~ String169 data = NULL; 170} 171 172 // int main () 173 // {174 // String s; 175 // cout <s <endl; 176 // cout <strlen (s. c_str () <endl; 177 // String s1 = "whah"; 178 // cout <s1 <endl; 179 // String s2 = s1; 180 // cout <"s2 length:" <s2.size () <endl; 181 // s = s2; 182 // cout <"s after s2 Value assignment:" <s <endl; 183 // String s3 (10, 'C '); 184 // cout <"character filling initialization s3:" <s3 <"" <"s3 len =" <s3.size () <endl; 185 // s3 [0] = 'a'; 186 // cout <"after the subscript is changed to s3:" <s3 <endl; 187 // String s4 = s2 + s3 + "ad"; // supports continuous + 188 // cout <s4 <endl; 189 // String * sp = & s4; 190 // cout <* sp <endl; 191 // cout <strlen (* sp ). c_str () <endl; 192 // String * q = new String (* sp); 193 // cout <* q <endl; 194 // delete q; 195 // 196 // char * sq = const_cast <char *> (s4.c _ str (); 197 // sq [0] = 'l '; 198 // cout <sq <endl; 199 // cout <s4 <endl; 200 // const char * cptr = s2.c _ str (); 201 // string s5 = ""; 202 // string s6 = "\ 0 "; 203 // cout <s5 <"**" <s6 <s5.size () <s6.size () <endl; 204 // string str = "jadj "; 205 // char * sptr = const_cast <char *> (str. c_str (); 206 // sptr [0] = 'D'; 207 // cout <sptr <"" <str <endl; 208 // String s7 (s2); 209 // s7 + = s4; 210 // cout <"s7:" <s7.size () <s7 <endl; 211 // const String s8 ("daj"); 212 // s8 [0]; [] Call to the const object, non-const menber operator [] cannot be used. You must provide const char & operator [] () const reload version 213 // s8 = "djwajk0 "; 214 //} 215 216 int main () 217 {218 string s2; 219 cin> s2; 220 cout <s2 <endl; 221 String s, s1; 222 cin> s> s1; // use Enter to separate the string. the string containing spaces can be connected to the string 223 cout <s <"\ n" <s1 <endl; 224 cout <s1.size () <endl; 225}
String type

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.