c++primer 13.44

#include <iostream>
#include <memory>//allocator

using namespace std;
class String
{
    static allocator<char> alloc;
    private:
        char *start;
        char *first_free;
        char *cap;
    public:
        //默认构造 
        String(): start(nullptr),first_free(nullptr),cap(nullptr){
        cout<<"默认构造"<<endl;}
        String(const char*);
        size_t size() const {
            return first_free-start;
        }
        size_t capacity() const {
            return cap-start;
        }
        void chk_n_alloc(){
            if(size() == capacity())
            {
                reallocate();
            }
        }
        void free(); 
        void reallocate();
        pair<char*,char*> alloc_n_copy(char*,char*);
        String& operator =(String&);
        char* begin(){
            return start;
        }
        char* end(){
            return first_free;
        }
        void show() {
            cout<<start<<endl;
        }

         
        
            
};
allocator<char> String::alloc;//不需要加static 
//void String::alloc_n_copy(char* b,char* e )
pair<char*,char*> String::alloc_n_copy(char* b,char* e )
{
    /*char* newdata=alloc.allocate(e-b);
    char* temp=newdata;
    for(char* p=b;p!=e;++p)
    {
        alloc.construct(newdata++,*p);
    }
    free();//释放旧数组
    start= temp;//新数组的首位置 
    first_free=cap=newdata;*/
    char* newdata=alloc.allocate(e-b);
    return {newdata,uninitialized_copy(b,e,newdata)};
    
}
void String::free()
{
    if(start)
    {
    for(char* p=start;p!=first_free;++p)
        {
            alloc.destroy(p);
        }
        alloc.deallocate(start,capacity());    
    }

}
void String::reallocate()
        {
            size_t oldsize=this->capacity(); 
            size_t newsize =(oldsize)? 2*oldsize:1;
            char* newdata=alloc.allocate(newsize);//新分配的内存起始位置
            char* p=nullptr;
            for(p=newdata;p!=first_free;++p)//旧数组中的元素拷贝到新数组中 
            {
                alloc.construct(p++,*start++);
            }
            free();//释放旧数组内存 
            start=newdata;
            first_free=p;
            cap=newdata+newsize;
            
        }
String& String::operator =(String& rhs)
{
    //char* olddata = alloc.allocate(rhs.size());
    pair<char*,char*> be = alloc_n_copy(rhs.begin(),rhs.end());
    free();
    start=be.first;
    cap=first_free=be.second;
    return *this;
    
}
String::String(const char* s)
{
    //char* p = s;
    /*char p[30];
    strcpy(p,s);
    char* adr=p;
    //char* temp=start;
    while((*adr)!='\0')
    {
        chk_n_alloc();
        alloc.construct(first_free++,*adr++);
    }*/
    //方法2 
    size_t len=sizeof(s);//包含\0的长度 
    //free();
    char* newdata=alloc.allocate(len);
    start=first_free=newdata;
    cap=newdata+len-1;
    first_free=uninitialized_copy(s,s+len,start);
    
    /*size_t len=sizeof(s); 方法1 
    start=first_free=alloc.allocate(len);
    for(size_t i=0;i<len;++i)
    {
        alloc.construct(first_free++,s[i]);
    }
    cap=first_free;*/
    cout<<"c风格"<<endl;
    
}

int main(int argc, char *argv[])
{
    String s1("asd");
    //String s1;
    char let[30]="qwe";
    String s2(let);
    //s1.show();
    s2.show();
    s2=s1;
    s2.show();
    return 0;
}

 

posted @ 2022-03-26 13:59  菠萝超级酸  阅读(25)  评论(0编辑  收藏  举报