c++primer练习13.42
生活随笔
收集整理的這篇文章主要介紹了
c++primer练习13.42
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
把strblob中的vector<string> 換成strvec
練習13.42
//functions.cc #include <string> #include <iostream> #include "strblob.h" #include "strblobptr.h" #include <vector> #include "strvec.h" using namespace std;//strblob.h成員函數實現 StrBlob::StrBlob():data(make_shared<StrVec>()){} StrBlob::StrBlob(const StrVec &s):data(make_shared<StrVec>(s)){}StrBlob::size_type StrBlob::size()const{return data->size(); } void StrBlob::push_back(const string &ele) {data->push_back(ele); }void StrBlob::pop_back(){data->pop_back(); } string & StrBlob::front() { return data->front();} string & StrBlob::back() { return data->back(); } const string& StrBlob::front()const{return data->front(); } const string& StrBlob::back()const{return data->back(); }bool StrBlob::empty()const { return data->empty(); }/* following is the member functions of class StrBlobPtr */ StrBlobPtr::StrBlobPtr():curr(0){} StrBlobPtr::StrBlobPtr(StrBlob b,size_t i):wptr(b.data),curr(i){}shared_ptr<StrVec> StrBlobPtr::check(size_t i,const string &msg)const{shared_ptr<StrVec> ret = wptr.lock(); //question1:什么情況下,ret會不指向任何對象呢? if(!ret)throw runtime_error("unbounded StrBlobPtr"); else if(i >= ret->size())throw out_of_range(msg); elsereturn ret; }StrBlobPtr StrBlob::begin()const { return StrBlobPtr(*this); } StrBlobPtr StrBlob::end()const { return StrBlobPtr(*this,this->size()); }string & StrBlobPtr::deref()const { auto ret = check(curr,"out of range"); return *(ret->begin() + curr); }StrBlobPtr &StrBlobPtr::incr() {check(curr,"out of range");++ curr;return *this; }//strvec.h StrVec::StrVec():elements(nullptr),first_free(nullptr),cap(nullptr){} allocator<string> StrVec::alloc; string &StrVec::front()const { return *elements;} string & StrVec::back()const { return *(first_free - 1);} string *StrVec::begin()const { return elements; } string * StrVec::end()const { return first_free; }StrVec::StrVec(const StrVec &s) { auto data = allocate_n_copy(s.begin(),s.end()); elements = data.first; cap = first_free = data.second; } StrVec& StrVec::operator=(const StrVec& s) { auto data = allocate_n_copy(s.begin(),s.end()); free(); elements = data.first; cap = first_free = data.second;return *this; }pair<string*,string*> StrVec::allocate_n_copy(const string* b,const string *e) { string * new_elements = alloc.allocate(e - b); string * dest = new_elements; const string * elem = b; while(elem != e){alloc.construct(dest ++,std::move(*elem ++)); }return {new_elements,dest}; }void StrVec::free() { auto elem = elements; while(elem != first_free){alloc.destroy(elem ++);} alloc.deallocate(elements,cap - elements); } size_t StrVec::size()const { return (first_free - elements);} void StrVec::push_back(const string &s) { if(first_free == cap)reallocate(); alloc.construct(first_free ++ ,s); } void StrVec::pop_back() { check(0,"pop on empty StrVec."); alloc.destroy(--first_free); } bool StrVec::empty()const { if(!elements)return true; elsereturn false;} StrVec::~StrVec() { free(); }void StrVec::reallocate() { size_t new_size = size()? 2*size():1; auto new_elements = alloc.allocate(new_size); auto new_first_free = uninitialized_copy(elements,first_free,new_elements); free(); elements = new_elements; first_free = new_first_free; cap = new_elements + new_size; }void StrVec::check(size_t i,const string &msg)const { if(i >= size()) throw out_of_range(msg); }bool StrBlobPtr::operator!=(const StrBlobPtr &s) {auto p = this->wptr.lock();auto q = s.wptr.lock();if(!(p->elements == q->elements && p->first_free == q->first_free && p->cap == q->cap))return true;elsereturn false; } //main.cc#include <string> #include <iostream> #include "strblob.h" #include "strblobptr.h" #include <vector> #include "strvec.h" using namespace std; int main() { StrVec s1; s1.push_back("abc"); s1.push_back("def"); s1.push_back("fgh"); s1.push_back("qhk"); //for(string* ptr = s1.begin(); ptr != s1.end() ;++ ptr) // cout << *ptr << endl; for(auto p = s1.begin(); p != s1.end() ; ++p)cout << *p << endl;StrBlob str(s1); StrBlobPtr pk(str); pk.incr();cout << "deref :" << endl; cout << pk.deref() << endl; cout << "end ." << endl; cout << str.size() << endl; cout << str.empty() << endl; cout << str.front() << endl; cout << str.back() << endl;return 0; } //strblob.h #ifndef STRBLOB_H #define STRBLOB_H#include <iostream> #include <string> #include <memory> #include <vector> #include "strvec.h" using namespace std; class StrBlobPtr; class StrBlob{public:friend class StrBlobPtr;StrBlob();StrBlob(const StrVec &l);using size_type = vector<string>::size_type;size_type size()const;void push_back(const string &ele);void pop_back();string &front();string &back();const string &front()const;const string &back()const;bool empty()const;StrBlobPtr begin()const;StrBlobPtr end()const;private:shared_ptr<StrVec> data;void check(size_type i,const string &msg);};#endif //strblobptr.h#ifndef INCLUDE_H_STRBLOBPTR #define INCLUDE_H_STRBLOBPTR#include <string> #include <iostream> #include <memory> #include <vector> #include "strvec.h" using namespace std; class StrBlobPtr{public: StrBlobPtr(); StrBlobPtr(StrBlob b,size_t curr = 0); string & deref()const; StrBlobPtr & incr(); bool operator!=(const StrBlobPtr &s);private:weak_ptr<StrVec> wptr;size_t curr;shared_ptr<StrVec> check(size_t i,const string &msg)const; };#endif //strvec.h#ifndef STRVEC_H #define STRVEC_H#include <string> #include <iostream> #include <memory> using namespace std; class StrVec {friend class StrBlobPtr;public:StrVec();StrVec(const StrVec &);StrVec& operator=(const StrVec &);~StrVec();string & front()const;string & back()const;string * begin()const;string * end()const;void pop_back();void push_back(const string &s);size_t size()const;bool empty()const;void reallocate();private:static allocator<string> alloc;string * elements;string * first_free;string * cap;pair<string*,string*> allocate_n_copy(const string *,const string *);void free();//主要檢查是否i已經達到容器的尾后位置void check(size_t i,const string &msg)const; };#endif在g++編譯器中執行:
g++ main.cc functions.cc -o exe ./exe得到如下結果:
abc def fgh qhk deref : def end . 4 0 abc qhk結果,目前來來,全部通過編譯。
總結
以上是生活随笔為你收集整理的c++primer练习13.42的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sogou ubuntu安装(最后还是失
- 下一篇: non-member function