C++vector Simple Implementation

Source: Internet
Author: User

const int DEFAULT_CAP = 3;template <typename t>class vector {//int capacity;    T* _data;    int _size;        Public://const Vector (): Capacity (Default_cap), _size (0), _data (new t[capacity]) {}; Vector (vector& v): Capacity (V.cap ()), _size (V.size ()), _data (new t[capacity]) {memcpy (_data, V._data, _size*sizeo    F (T));        Vector (vector&& v): Capacity (V.cap ()), _size (V.size ()) {_size = V.getdata ();    V._data = nullptr;    }//deconst Virtual ~vector () {delete [] _data;} Operator vector& operator= (const vector &v) {/* if (this! = &v) {Delete [] _data            ;            _size = V.size ();            Capacity = V.cap ();            _data = new T[capacity];        memcpy (_data, V._data, _size*sizeof (T));        }*/SW (v);    return *this;    } t& operator[] (int i) const{return _data[i];    }//retrun int size () const {return _size;} int cap () const {return Capacity;}    const int GetData () const noexcept {_data;}        function void expand () {if (_size = = capacity) {capacity = capacity<<1;        T *old = _data;        _data = new T[capacity];        memcpy (_data, old, _size*sizeof (T));        delete [] old;        }} void Push_back (T const& temp) {expand ();    _data[_size++] = temp;        } void each () {for (int i = 0; i < _size; ++i) {cout<<_data[i]<<endl;        }} void SW (vector& v) {swap (_size, v._size);        Swap (capacity, v.capcity);    Swap (_data, v._data); }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C++vector Simple 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.