c/c++ vector 的簡單實現

來源:互聯網
上載者:User

說其簡單,是因為沒有實現allocate 類, 而採用簡單的malloc 分配記憶體。

iterator 也採用直接的value_type 指標, 但保留了vector 的基本介面和方法。對理解vector 工作原理有協助。

代碼及測試代碼如下:

#include "stdafx.h"template < class T >class vector{enum{ SPACE_CAPACITY = 4 };//初始容量public:////////////////////////////////////////////////////////////////////////////////// 構造器與析構器//////////////////////////////////////////////////////////////////////////////////用explicit避免隱式類型轉換explicit vector( int size = 0 ):m_size( size ),m_capacity( size + SPACE_CAPACITY ){_p = new T[ m_capacity ];}//copy constructorvector( const vector & rhs ):_p( NULL ){ operator=( rhs ); }~vector(){ delete [] _p; }//////////////////////////////////////////////////////////////////////////////////運算子////////////////////////////////////////////////////////////////////////////////const vector & operator= ( const vector & rhs ){if( this != &rhs ){//如果是本身的話就不用再操作了delete [] _p;m_size = rhs.size();m_capacity = rhs.capacity();_p = new T[ capacity() ];for( int k = 0; k < m_size; k++ )//拷貝元素_p[k] = rhs._p[k];}return *this;}T & operator[] ( int index ) { return _p[ index ]; }const T & operator[] ( int index ) const { return _p[ index ]; }////////////////////////////////////////////////////////////////////////////////// 指標, iterator 操作////////////////////////////////////////////////////////////////////////////////typedef T * iterator;//用原生指標替代迭代器typedef const T * const_iterator;iterator begin() { return &_p[0]; }const_iterator begin() const { return &_p[0]; }iterator end() { return &_p[ m_size ]; }const_iterator end()   const { return &_p[ m_size ]; }//////////////////////////////////////////////////////////////////////////////////插入元素, 彈出元素////////////////////////////////////////////////////////////////////////////////void push_back( const T & x ){if( m_size == m_capacity )ReAllocate( 2*m_capacity + 1 );_p[ m_size++ ] = x;}void pop_back() { m_size--; }////////////////////////////////////////////////////////////////////////////////// 屬性介面////////////////////////////////////////////////////////////////////////////////int size() const { return m_size; }int capacity() const { return m_capacity; }bool empty() const { return size() == 0; }const T & back() const { return _p[ m_size-1 ]; }private:int m_size; //元素的個數int m_capacity; //容量T *_p; //指向new分配的資源////////////////////////////////////////////////////////////////////////////////// 記憶體配置(分配器簡化處理)//////////////////////////////////////////////////////////////////////////////////每次空間不夠的時候,就重新獲得2倍於當前容量的空間void resize( int newSize ){if( newSize > m_capacity )ReAllocate( newSize*2 ); m_size = newSize;}//擷取新的空間,拷貝,釋放舊的資料void ReAllocate( int newCapacity ){if( newCapacity < m_size ) return;T *oldArray = _p;_p = new T[ newCapacity ];for( int k = 0; k < m_size; k++ )_p[k] = oldArray[k];m_capacity = newCapacity;delete [] oldArray;}};int main(int argc, char *argv[]){vector<int> v;v.push_back(4);v.push_back(5);vector<int>::iterator it;for(it=v.begin(); it!=v.end(); it++){printf("data is %d\n",*it);}_getch();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.