Although previously done a more complex variety of data structure, but that is only after reading C++prime7 chapter, do not take into account the class copy reflects the class value or class pointer, so write some semi-finished class, but those mainly training data structure, do not want to change, so we will try to imitate the next string, Used to use new and delete, although simple, but not flexible, so deliberately using the Alloctor class to manage memory, so that the "memory allocation" and "object construction," because the object is not a pointer to manage the external resources of the object, so I simply do not destory, This use Alloctor efficiency advantage more embodies, practice hands cooked, generally no difficulty.
#include <iostream>#include<string>#include<memory>using namespacestd;Const intIncerment=Ten;classstring{ Public: Friend IStream&operator>> (IStream & is,ConstString &s); Friend Ostream&operator<< (Ostream &os,ConstString &s); FriendConstStringoperator+ (Conststring& A,Conststring&b); String (): Elements (nullptr), Last_element (nullptr), Space_end (nullptr) {} string (Const Char*s): String () { for(size_t i =0; T[n]; i++) push_back (S[i]); } String (ConstString &s): Elements (nullptr), Last_element (nullptr), Space_end (nullptr) {elements= Alloc.allocate (S.size () *2); Last_element=uninitialized_copy (S.begin (), S.end (), elements); Space_end= elements + s.size () *2; Uninitialized_fill (Last_element, Space_end,0); } String (Constsize_t &len): Elements (nullptr), Last_element (nullptr), Space_end (nullptr) {elements=alloc.allocate (len); Uninitialized_fill_n (elements, Len,0); Last_element=elements; Space_end= elements +Len; } String (Char*b,Char*e): String () {Auto P=b; while(P! =e) {Push_back (*p++); }} String (size_t N,Charch): String () {Elements= Alloc.allocate (n2); Uninitialized_fill_n (elements, n, ch); Last_element= elements +N; Space_end= elements + N *2; } ~String () {alloc.deallocate (Elements, capcity ()); }; voidSwap (String &a, String &b) { usingStd::swap; Swap (a.elements, b.elements); Swap (a.last_element, b.last_element); Swap (a.space_end, b.space_end); } voidClear () {alloc.deallocate (Elements, capcity ()); Elements= Last_element = Space_end =nullptr; } voidPush_back (Charch) { if(Size () >=capcity ()) {Auto Newcap= capcity () +incerment; Auto P=alloc.allocate (Newcap); if(elements) last_element=uninitialized_copy (Elements, Last_element, p); Else{elements= Last_element =p; } space_end= elements +Newcap; Uninitialized_fill (Last_element, Space_end,0); } *last_element++ =ch; } String&operator=(String s) {Swap (* This, s); return* This; } Char&operator[] (Constsize_t i) { returnElements[i]; } Const Char&operator[] (Constsize_t i)Const{ returnElements[i]; } BOOL operator== (ConstString &s)Const{ if(Size ()! =s.size ())return false; for(size_t i =0; elements+i!=last_element;i++)if(elements[i]!=S[i]) { return false; } return true; } BOOL operator!= (ConstString &s)Const{ return!(equal (elements, Last_element, S.begin ())); } String&operator+= (Conststring&s) {Auto Newcap= capcity () +s.capcity (); Auto P=alloc.allocate (Newcap); Uninitialized_copy (Elements, Last_element, p); Last_element= Uninitialized_copy (S.begin (), s.last_element, p +size ()); Alloc.deallocate (Elements,capcity ()); Elements=p; Space_end= elements +Newcap; Uninitialized_fill (Last_element,space_end,0); return* This; } size_t size ()Const{returnlast_element-elements;} size_t capcity ()Const{returnSpace_end-elements;} Char*begin ()Const{returnelements;} Char*end ()Const{returnlast_element;} Private: Allocator<Char>Alloc; Char*elements,*last_element,*Space_end;}; IStream&operator>> (IStream & is, String &s) { Charbuf; while( is>>buf) {S.push_back (BUF); } return is;} Ostream&operator<< (Ostream&os,Conststring&s) {Auto P=S.begin (); while(P! =S.end ()) {OS<< *p++; } returnos;}ConstStringoperator+ (Conststring& A,Conststring&b) {shared_ptr<String> res=make_shared<string>(a); *res + =b; return*Res;}intMain () {{String A ("a"), B ="b", C; cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; C=b; Puts (c= = b?"Yes":"No"); cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; A+=b; cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; C="C"; Puts (c= = b?"Yes":"No"); cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; C+="D"; cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; b= A +C; cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; String d (B.begin ()+3, B.end ()), E ( A,'e'), f="F"+e; cout<<"e.capcity ="<< e.capcity () <<"d ="<<d<<"e ="<<e<<Endl; F+='g'; cout<<"A ="<< a <<"B ="<< b <<"c = ="<< C <<Endl; String h="h"; H= B +h; cout<< h[0] <<" "<< h[h.size ()-1]; } _crtdumpmemoryleaks ();}View Code
Using the Alloctor class to implement the String class