Item 4-5 通用容器的設計

來源:互聯網
上載者:User

給下面的容器添加幾個拷貝建構函式和賦值函數:

template<typename T, size_t size><br />class fixed_vector<br />{<br />public:<br />typedef T* iterator;<br />typedef const T* const_iterator;<br />iterator begin() { return v_; }<br />iterator end() { return v_+size; }<br />const_iterator begin() const { return v_; }<br />const_iterator end() const { return v_+size; }<br />private:<br />T v_[size];<br />};

 

比較好的實現:

template<typename T, size_t size><br />class fixed_vector<br />{<br />public:<br />typedef T* iterator;<br />typedef const T* const_iterator;<br />iterator begin() { return v_; }<br />iterator end() { return v_+size; }<br />const_iterator begin() const { return v_; }<br />const_iterator end() const { return v_+size; }<br />fixed_vector();<br />fixed_vector(const fixed_vector& fv);<br />// more flexible copy ctor<br />template<typename OT, size_t osize><br />fixed_vector(const fixed_vector<OT, osize>& fv)<br />{<br />if (this == &fv) return;<br />copy(fv.begin(),<br /> fv.begin() + min(size, osize),<br /> begin());<br />}<br />fixed_vector& operator = (const fixed_vector& fv);<br />// more flexible copy assignment<br />template<typename OT, size_t osize><br />fixed_vector<T, size>&<br />operator = (const fixed_vector<OT, osize>& fv)<br />{<br />if ((void*)this != (void*)&fv)<br />{<br />copy(fv.begin(),<br /> fv.begin() + min(size, osize),<br /> begin());<br />}<br />return *this;<br />}<br />private:<br />T v_[size];<br />};<br />/** copy ctor<br /> */<br />template<typename T, size_t size><br />fixed_vector<T, size>::fixed_vector(const fixed_vector<T, size>& fv)<br />{<br />if (this == &fv) return;<br />copy(fv.begin(), fv.end(), begin());<br />}<br />/** default ctor<br /> */<br />template<typename T, size_t size><br />fixed_vector<T, size>::fixed_vector()<br />{<br />}<br />/** copy assignment<br /> */<br />template<typename T, size_t size><br />fixed_vector<T, size>&<br />fixed_vector<T, size>::operator = (const fixed_vector<T, size>& fv)<br />{<br />if (this != &fv)<br />{<br />copy(fv.begin(), fv.end(), begin());<br />}<br />return *this;<br />}

 

要想讓容器保證有較高的異常安全層級:

template<typename T, size_t size><br />class fixed_vector<br />{<br />public:<br />typedef T* iterator;<br />typedef const T* const_iterator;<br />fixed_vector() : v_( new T[size] ) { }<br />~fixed_vector() { delete[] v_; }<br />// 能保證較高的異常安全<br />template<typename O, size_t osize><br />fixed_vector( const fixed_vector<O, osize>& other )<br />: v_( new T[size] )<br />{<br />try {<br />copy(other.begin(),<br /> other.begin() + min(size, osize),<br /> begin());<br />}<br /> catch(...) {<br />delete[] v_;<br />throw;<br />}<br />}<br />// 能保證較高的異常安全<br />fixed_vector( const fixed_vector<T, size>& other )<br />: v_( new T[size] )<br />{<br />try {<br />copy(other.begin(), other.end(), begin());<br />}<br /> catch(...) {<br />delete[] v_;<br />throw;<br />}<br />}<br />void Swap( fixed_vector<T, size>& other ) throw()<br />{<br />swap( v_, other.v_ );<br />}<br />// 能保證較高的異常安全<br />template<typename O, size_t osize><br />fixed_vector<T, size>&<br />operator=( const fixed_vector<O, osize>& other )<br />{<br />fixed_vector<T, size> temp( other );// does all the work<br />Swap( temp );// this can't throw<br />return *this;<br />}<br />// 能保證較高的異常安全<br />fixed_vector<T, size>&<br />operator=( const fixed_vector<T, size>& other )<br />{<br />fixed_vector<T, size> temp( other );// does all the work<br />Swap( temp );// this can't throw<br />return *this;<br />}<br />iterator begin() { return v_; }<br />iterator end() { return v_+size; }<br />const_iterator begin() const { return v_; }<br />const_iterator end() const { return v_+size; }<br />private:<br />T* v_;// 不能用原來數組的方式<br />};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.