C + + Primer Fifth Edition Exercise 13.5

Source: Internet
Author: User

writes a Strvec class
that involves dynamic memory management, memory allocation, FOR_EACH,LAMBDA expressions used in classes, initializing lists, and so on.

#include <bits/stdc++.h> using namespace std; Class Strvec {Public:strvec (): Elements (nullptr), First_free (nullptr), Cap (nullptr) {} Strvec (const initial
        Izer_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);//reallocation size, relative to size void Resize (size_t, const string&);//Private:static Allocator<str
    Ing> 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::P ush_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) {//Here is passed to the lambda parameter should be *element rather than element, so it should be destroy (&AMP;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 &AMP;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)//request twice times memory Auto Dest = Newdata;//alloc return the first pointer auto elem = elements;
    For (size_t i=0;i!=size (); ++i) alloc.construct (Dest++,move (*elem++));
    Free ();
    elements = newdata;//new first pointer first_free = dest;
Cap = elements + newcapacity;
    } void Strvec::reserve (const size_t& len)/reassign size {if (len<=cap-elements)/less than or equal to not apply for return;
    Not-constructed memory reduction auto newcapacity = len;
    Auto NewData = alloc.allocate (newcapacity)//Request Len times memory Auto Dest = Newdata;//alloc return the first pointer auto elem = elements;
    For (size_t i=0;i!=size (); ++i) alloc.construct (Dest++,move (*elem++));
    Free (); elements = newdata;//new First pointer
    First_free = dest;
Cap = elements + newcapacity;
        } void Strvec::resize (size_t n) {if (N>size ())//Reallocate space and initialize construct {if (n>cap-elements)//If it is larger than the length of memory space
        {reserve (n);
            else {string s= ""; 
    while (first_free-elements!=n) {alloc.construct (first_free++,s);//Add construct}}
        } else {while (first_free-elements!=n) {Alloc.destroy (--first_free);//Destroy Construct
    }} void Strvec::resize (size_t n,const string &s) {if (n = size ()) return;
            if (N>size ())//Reallocate space, and initialize the construct {if (n>cap-elements)//if larger than the length of the memory space {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);//Destroy Construct
    int main () {Ios::sync_with_stdio (FALSE);
return 0;






 }

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.