Many times before the vector is not forget, from now on, each time the number of knocks down, until 108 times so far. (Yoga is done 108 times a day, here with the idea of yoga)
Why do you have to knock so many times? With a word from NLP: The most basic is the most essential!
Why do you have to knock 108 times? With the idea of 108 times of worship in yoga.
This article is for individual study, summary .....
Don't say much nonsense ... Open up!
/** file Description: vector-related declaration and implementation of Analog STL (1th Pass) * Author: High Minor * Date: 2016-12-18* integrated development environment: Microsoft Visual Studio */#ifndef __vector1_h__# Define __vector1_h__template<typename t>class vector{public:typedef t* iterator;typedef Const T * ConstIterator; public://default member function//constructor vector (): _start (null), _finish (null), _endofstorgy (null) {}//copy construction vector (const vector & V): _ Start (null), _finish (null), _endofstorgy (null) {if (V._arr! = null) {//copied object not empty size_t size = V.size ();//number of valid elements size_t Capacity = V.capacity ();//Capacity _arr = new t[capacity];//Open Memory _copy (_arr,v.begin (), V.end ());//copy data _finish = _arr + size;//update _finish_endofstorgy = _arr + capacity;//Update _endofstorgy}}//assignment operator overloading Vector & operator= (const vector &v) {if (_arr!= V._arr) {Vector tmp (v); _swap (v);}} destructor ~vector () {_destory ();} public://public interface function//tail plug void pushback (const T & E) {Insert (End (), e);} Tail delete void Popback () {Erase (end ());} Insert a single element void insert (Iterator pos,const T &e) {size_t sub = pos-_start;//records the relative distance between the current position and the _finish _checkcapacity ();// Expansion detection and processing pos = _start + sub;//update pos, prevents the iterator from failing after expansion iterator It = _finish;while (pos<it) {*it = * (It-1);//Mobile data--it;} *pos = e;//Insert Data _finish++;//update--_finish}//Delete an element void Erase (Iterator pos) {if (Empty ()) {assert (FALSE);// The current vector is empty return;} Iterator Cur = Pos;iterator end = End (), while (cur<end) {*cur = * (cur+1);//loop over Cur position data cur++;} --_finish;//update _finish}//to determine if null bool empty () {return (_start==_finish);} Number of valid elements size_t Size () {return _finish-_start;} Capacity size_t capacity () {return _endofstorgy-_start;} Subscript operator Overload T &operator[] (size_t index) {assert (Index<size ()); return _start[index];} Const T &operator[] (size_t index) Const{assert (Index<size ()); return _start[index];} public://iterator-related operations iterator Begin () {return _start;} Constiterator Begin () Const{return _start;} Iterator End () {return _finish;} Constiterator End () Const{return _finish;} private://detection expansion and processing void _checkcapacity () {size_t oldsize = size (); size_t oldcapacity = Capacity (); if (oldsize== oldcapacity) {size_t newcapacity = oldcapacity*2+3;iterator NewArr = new T[newcapacity];if (_start!=nulL) {_copy (Newarr,begin (), End ());d elete[] _start; _start = Newarr;_finish = _start + oldsize;_endofstorgy = _start + newcapacity;}} copy void _copy (Iterator dst,iterator Start,iterator end) {while (start!=end) {*dst++ = *start++;}} swap void _swap (const Vector &v) {swap (_start,v._start); swap (_finish,v._finish); Swap (_endofstorgy,v._ Endofstorgy);} destroys void _destory () {if (_start!=null) {delete[] _start;_start = _finish = _endofstorgy = NULL;}} Private:iterator _start;//start position Iterator _finish;//valid data end position Iterator _endofstorgy;//end position}; #endif
/** file Description: Test vector-related functions * Author: High Minor * Date: 2016-12-18* integrated development environment: Microsoft Visual Studio */#include <iostream> #include <assert.h>using namespace std; #include "Vector1.h"//test function void Vectortest () {vector<int> v1;//test Pushback, _ Checkcapacity, insert function v1.pushback (1); V1. Pushback (2); V1. Pushback (4); V1. Pushback (5); V1. Insert (v1. Begin (), 0); v1. Insert (v1. Begin () +3,3);//test begin, End, function vector<int>::iterator It1 = v1. Begin (); while (IT1!=V1. End ()) {cout<<*it1<< ""; ++it1;} cout<<endl;//Test Erase function V1.popback ();//tail Delete 5v1. Erase (v1. Begin ());//head Delete 0V1. Erase (v1. Begin () +2);//delete Intermediate element 3//test the size function, [] overload for (size_t i=0; i<v1. Size (); ++i) {cout<<v1[i]<< "";} Cout<<endl;} int main () {vectortest (); return 0;}
Bug Summary:
According to the last 11 days of hitting the vector, some things have been forgotten.
1. When you overload const and iterator-related functions, you forget to add a const to the function name
2. In the Insert function, to prevent the iterator from invalidating, the sub, inexplicably began to think of _finish-pos
3. Think of a half-day insert inserts an interval function how to write, suddenly think of this thing is in the list.
Today... That's it!
108 practice simulations to achieve vector in STL (i)