<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                好久沒動手寫一點C++程序了,以后沒事多模仿STL吧,雖然達不到標準的STL的程序,但簡單的功能還是要實現的。STL確實博大精深:泛型編程、容器、算法、適配器...有的是內容可以學。下面是根據STL源碼,寫的一個非常簡單的vector,實現了部分接口。其實vector還是相對很簡單的容器了,元素按在內存中連續排列,只需要三個指針就能實現很多的接口。還有一個就是內存的分配,這里采用了一個C++提供的allocator配置器,所以分配起來還是蠻簡單的,SGI版本的STL中的配置器為了達到效率的極致,使用了另外的分配器,相當復雜,這里就沒有寫了。 ~~~ #ifndef __MYVECTOR_H__ #define __MYVECTOR_H__ template <class T> class Vector { public: typedef T value_type; typedef value_type* iterator; // vector的迭代器就是原生指針 typedef value_type* pointer; typedef value_type& reference; typedef size_t size_type; public: Vector() : start(NULL), finish(NULL), end_of_storage(NULL) { } Vector(size_type n, const value_type &value) { start = alloc.allocate(n); end_of_storage = finish = start + n; uninitialized_fill_n(start, n, value); } Vector(size_type n) { start = alloc.allocate(n); end_of_storage = finish = start + n; uninitialized_fill_n(start, n, value_type()); } ~Vector() { // 順序調用元素的析構函數 for (iterator i = start; i != finish; ++i) alloc.destroy(i); // 銷毀分配的空間 if (start != NULL) alloc.deallocate(start, end_of_storage - start); } iterator begin() const { return start; } iterator end() const { return finish; } size_type size() const { return end() - begin(); // 使用接口函數,包裹性更好 } size_type capacity() const { return end_of_storage - begin(); // 使用接口函數,包裹性更好 } bool empty() const { return begin() == end(); } // 返回的引用可被修改 reference front() { return *(begin()); } // 返回的引用可被修改 reference back() { return *(end() - 1); } reference operator[] (const size_type n) { return *(begin() + n); } const reference operator[] (const size_type n) const { return *(begin() + n); } void push_back(const value_type &value) { if (finish == end_of_storage) reallocate(); // 存儲空間已滿,則重新分配內存 alloc.construct(finish, value); ++finish; } void reallocate(); void pop_back() { --finish; alloc.destroy(finish); // 析構最后一個函數,但不釋放空間 } // 清除一個元素 iterator erase(iterator position) { if (position + 1 != finish) copy(position + 1, finish, position); --finish; alloc.destroy(finish); return position; } // 清除一段元素 iterator erase(iterator first, iterator last) { if (first < start || last > finish) throw exception("Invalid input."); copy(last, finish, first); int len = last - first; while (len--) alloc.destroy(--finish); return first; } void clear() { erase(begin(), end()); } private: iterator start; iterator finish; iterator end_of_storage; private: static std::allocator<value_type> alloc; // 空間配置器,采用靜態屬性節省空間 }; template <class Type> std::allocator<Type> Vector<Type>::alloc; template <class Type> void Vector<Type>::reallocate() { size_type oldsize = size(); size_type newsize = 2 * (oldsize == 0 ? 1 : oldsize); // 分配新的內存空間 iterator newstart = alloc.allocate(newsize); uninitialized_copy(start, finish, newstart); // 順序調用每個元素的析構函數 for (iterator i = start; i != finish; ++i) alloc.destroy(i); // 銷毀分配的空間,銷毀之前主要檢查是否為NULL if (start != NULL) alloc.deallocate(start, end_of_storage - start); // 更新下標 start = newstart; finish = start + oldsize; end_of_storage = start + newsize; } #endif ~~~ insert操作應該算是最復雜的一個接口了,設計到元素的搬移、(可能)重新分配內存等等,這里我只實現了一個最簡單的形式: ~~~ template <class Type> void Vector<Type>::insert(iterator position, const value_type &value) { size_type diff = position - start; if (finish == end_of_storage) reallocate(); position = start + diff; // 注意,這里不能使用copy,因為目的地最后一個位置還沒有被構造, // 賦值涉及析構操作,對未構造的對象進行析構,行為未定義 alloc.construct(finish, *(finish - 1)); ++finish; copy_backward(position, finish - 1, finish); // 不能使用uninitialized_copy,因為這個函數是從前向后構造,這會造成覆蓋 //uninitialized_copy(position, finish, position + 1); // 插入新對象,直接賦值即可 *position = value; } ~~~ 測試程序: ~~~ int main() { Vector<int> v; v.push_back(1); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; v.push_back(2); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; v.push_back(3); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; v.push_back(4); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; v.push_back(5); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; Vector<int>::iterator iter1 = v.begin(); Vector<int>::iterator iter2 = iter1 + 3; v.erase(iter1, iter2); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; v.clear(); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; v.push_back(123); cout << "size = " << v.size() << endl; cout << "capacity = " << v.capacity() << endl; for (Vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) cout << *iter << endl; system("pause"); return 0; } ~~~ 運行結果: ![](https://box.kancloud.cn/2016-08-11_57ac4c8bcc81c.jpg) 參考: 《STL源碼剖析》 《C++ primer》
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看