C + + Stl:vector implementation

Source: Internet
Author: User

Practice a pitch, primarily by using placement new to create objects on raw memory. Halfway MD Interview phone come, quickly archive, see Java Dafa

#include <iostream>#include<cstdlib>#include<vector>#include<algorithm>using namespacestd;classObject { Public:    Static intcount; Public:    intCurrent ; intID; Object (inti) {ID=i; Current= count++; cout<<"object["<<id<<"] Create:"<<current<<Endl; }        ~Object () {cout<<"object["<<id<<"] Destroy:"<<current<<Endl; } Object (Constobject&obj) { Current= count++; ID=obj.id; cout<<"object["<<id<<"] Copied:"<<current<<"Old :"<<obj.current<<Endl; }};intObject::count =0; template<classT>classMyvector { 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 () {returnSpace_end-start;} Size_type size () {returnFinish-start;} BOOLEmpty () {returnStart = =finish;} Iterator begin () {returnstart;} Iterator End () {returnfinish;} Referenceoperator[] (intIDX) {returnStart[idx];} Reference Front () {return*start;} Reference back () {return* (finish-1);} voidclear () {}voidPop_back () {if(Start >=finish) {            return; } Finish--; Finish->~T (); }        voidPush_back (Constt&x) {if(Finish <space_end) {            //Placement New            New(Finish) T (x); //Adjust end pointerfinish++; } Else if(Space_end = =finish) {            //Space Not enough            intOlen = Finish-start; intNlen = Olen = =0?1: Olen *2; T* New_start = (t*) malloc (Nlen *sizeof(T)); T* New_finish =New_start;  for(intI=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(intI=0; i<olen; i++) {(Start+i)->~T ();                        } free (start); Start=New_start; Finish=New_finish; Space_end= Start +Nlen; } Else {            //State Invalidcerr<<"Error"<<Endl; }    }        ~Myvector () { for(t* S=start; s<finish; s++) {s->~T ();    } free (start); }};#defineuse_org 0intMain () {Long* A =NULL; Long* B = A +1; intn = (Char*) B-(Char*) A; cout<<n<<Endl; intLast_capacity =-1; Object::count=0; #ifuse_orgVector<Object>tmp;#elseMyvector<Object>tmp;#endif     for(intI=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"<<Endl; } myvector<int>ints; Ints.push_back (2); Ints.push_back (3); Ints.push_back (1);        Sort (Ints.begin (), Ints.end ());  for(inti:ints) {cout<<i<<Endl; } System ("Pause"); return 0;}

C + + Stl:vector implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.