c++ primer 第五版 練習13.5

來源:互聯網
上載者:User

編寫一個StrVec類
其中涉及到動態記憶體管理,記憶體配置,for_each,lambda運算式在類中使用,初始化列表等問題。

#include <bits/stdc++.h>using namespace std;class StrVec{public:    StrVec():        elements(nullptr),first_free(nullptr),cap(nullptr){}    StrVec(const initializer_list<string> &vs)    {        elements=nullptr;        first_free=nullptr;        cap=nullptr;        for(auto it:vs)            this->Push_back(it);    }    StrVec(const StrVec&);    StrVec &operator=(const StrVec&);    ~StrVec();    void Push_back(const string&);    size_t Size() const { return first_free - elements;}    size_t Capacity() const { return cap-elements; }    string *Begin() const { return elements; }    string *End() const { return first_free; }    void Reserve(const size_t&);    void Resize(size_t );//重新分配大小,相對於Size    void Resize(size_t ,const string&);//private:    static allocator<string> alloc;    void chk_n_alloc()    {        if (Size() == Capacity() )            reallocate();    }    pair<string*,string*> alloc_n_copy(const string*,const string *);    void Free();    void reallocate();    string *elements;    string *first_free;    string *cap;};allocator<string> StrVec::alloc;void StrVec::Push_back(const string &s){    chk_n_alloc();    alloc.construct(first_free++,s);}pair<string*,string*> StrVec::alloc_n_copy(const string *b, const string *e){    auto data = alloc.allocate(e-b);    return {data, uninitialized_copy(b,e,data)};}void StrVec::Free(){    if(elements)    {        //此處傳遞到lambda參數應該是*element而不是element,所以應該是destroy(&p)        for_each(elements,first_free,[this](string &p){ alloc.destroy(&p);});        /*        for(auto p=first_free;p!=elements;)            alloc.destroy(--p);        */        alloc.deallocate(elements,cap - elements);    }}StrVec::StrVec(const StrVec &s){    auto newdata = alloc_n_copy(s.Begin(),s.End());    elements = newdata.first;    first_free = cap = newdata.second;}StrVec::~StrVec(){    Free();}StrVec &StrVec::operator=(const StrVec &rhs){    auto data = alloc_n_copy(rhs.Begin(),rhs.End());    Free();    elements = data.first;    first_free = cap = data.second;    return *this;}void StrVec::reallocate(){    auto newcapacity = Size() ? 2 * Size() : 1;    auto newdata = alloc.allocate(newcapacity);//申請兩倍記憶體    auto dest = newdata;//alloc返回的首指標    auto elem = elements;    for(size_t i=0;i!=Size();++i)        alloc.construct(dest++,move(*elem++));    Free();    elements = newdata;//新的首指標    first_free = dest;    cap = elements + newcapacity;}void StrVec::Reserve(const size_t& len)//重新分配大小{    if(len<=cap-elements)//小於或等於不申請        return ;    //未構造的記憶體減少    auto newcapacity = len;    auto newdata = alloc.allocate(newcapacity);//申請len倍記憶體    auto dest = newdata;//alloc返回的首指標    auto elem = elements;    for(size_t i=0;i!=Size();++i)        alloc.construct(dest++,move(*elem++));    Free();    elements = newdata;//新的首指標    first_free = dest;    cap = elements + newcapacity;}void StrVec::Resize(size_t n){    if(n>Size())//重新分配空間,並初始化構造    {        if(n>cap-elements)//如果比記憶體空間長度還要大        {            Reserve(n);        }        else        {            string s="";            while(first_free-elements!=n)            {                alloc.construct(first_free++,s);//添加構造            }        }    }    else    {        while(first_free-elements!=n)        {            alloc.destroy(--first_free);//銷毀構造        }    }}void StrVec::Resize(size_t n,const string &s){    if(n == Size())        return;    if(n>Size())//重新分配空間,並初始化構造    {        if(n>cap-elements)//如果比記憶體空間長度還要大        {            Reserve(n);            for(size_t i=Size();i!=n;++i)                alloc.construct(first_free++,s);        }    }    else    {        while(first_free-elements!=n)        {            alloc.destroy(--first_free);//銷毀構造        }    }}int main(){    ios::sync_with_stdio(false);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.