C++ STL:vector實現

來源:互聯網
上載者:User

標籤:

練習一發,主要是使用placement new在原始記憶體上建立對象。半路md面試電話來了,趕緊存檔,看Java大法

#include <iostream>#include <cstdlib>#include <vector>#include <algorithm>using namespace std;class Object {public:    static int count;public:    int current;    int id;    Object(int i) {        id = i;        current = count++;        cout<<"object["<<id<<"] create : "<<current<<endl;    }        ~Object() {        cout<<"object["<<id<<"] destroy: "<<current<<endl;    }    Object(const Object& obj) {        current = count++;        id = obj.id;        cout<<"object["<<id<<"] copied : "<<current<<" old : "<<obj.current<<endl;    }};int Object::count = 0;template<class T>class MyVector {public:    typedef T               value_type;    typedef value_type*     pointer;    typedef value_type*     iterator;    typedef value_type&     reference;    typedef size_t          size_type;    typedef ptrdiff_t       difference_type;    private:    iterator  start;    iterator  finish;    iterator  space_end;public:    MyVector() {        start = NULL;        space_end = NULL;        finish = NULL;    }        size_type capacity() {return space_end - start;}    size_type size() { return finish - start; }    bool empty() { return start == finish; }    iterator begin() {return start;}    iterator end() {return finish;}        reference operator[] (int idx) {return start[idx];}    reference front() {return *start;}    reference back() {return *(finish-1);}        void clear() {    }        void pop_back() {        if (start >= finish) {            return;        }        finish--;        finish->~T();    }        void push_back(const T& x) {        if (finish < space_end) {            // placement new            new (finish) T(x);            // adjust end pointer            finish++;        } else if (space_end == finish){            // space not enough            int olen = finish - start;            int nlen = olen == 0 ? 1 : olen * 2;            T* new_start = (T*) malloc(nlen * sizeof(T));            T* new_finish = new_start;            for (int i=0; i<olen; i++) {                // copy old data to new space                new (new_finish++) T(*(start + i));            }            // append pushed element            new (new_finish++) T(x);            // deconstruct old ones            for (int i=0; i<olen; i++) {                (start+i)->~T();            }            free(start);                        start = new_start;            finish= new_finish;            space_end = start + nlen;        } else {            // state invalid            cerr<<"error"<<endl;        }    }        ~MyVector() {        for (T* s=start; s<finish; s++) {            s->~T();        }        free(start);    }};#define USE_ORG 0int main() {    long* a = NULL;    long* b = a + 1;    int n = (char*)b - (char*)a;        cout<<n<<endl;                int last_capacity =-1;    Object::count = 0;    #if USE_ORG    vector<Object> tmp;#else    MyVector<Object> tmp;#endif    for (int i=0; i<5; i++) {        tmp.push_back(Object(i));        if (last_capacity != tmp.capacity()) {            cout<<"======last cap:"<<last_capacity<<" new capacity:"<<tmp.capacity()<<endl;            last_capacity = tmp.capacity();        }        cout<<"\n\n"<<endl;    }    MyVector<int> ints;    ints.push_back(2);    ints.push_back(3);    ints.push_back(1);        sort(ints.begin(), ints.end());        for (int i:ints) {        cout<<i<<endl;    }        system("pause");    return 0;}

 

C++ STL:vector實現

聯繫我們

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