C++STL之vector,實現屬於自己的my_vector
阿新 • • 發佈:2019-01-11
直接上程式碼
#include<iostream> //#include "my_vector.cpp" template <class elementType> class My_Vector { public: typedef elementType* iterator; My_Vector(); // ~My_Vector(); // int size(); // void reserve(int newCapacity);//申請newCapacity儲存空間 void push_back(const elementType &t);// void pop_back(); void clear();// void erase(iterator &a);// iterator begin(){return &obj[0];}// iterator end(){return &obj[ObjSize];}// elementType &operator[](int index); private: elementType *obj; int ObjSize; int ObjCap; };
測試程式碼#include "my_vector.h" #include<iostream> template <class elementType> My_Vector<elementType>::My_Vector() { ObjSize = 0; ObjCap = 0; // obj = new elementType[ObjCap]; } template <class elementType> My_Vector<elementType>::~My_Vector() { delete[]obj; ObjSize = 0; ObjCap = 0; } template <class elementType> int My_Vector<elementType>::size() { return ObjSize; } template <class elementType> void My_Vector<elementType>::reserve(int newCapacity)//申請newCapacity儲存空間 { if (newCapacity < ObjCap) { return; } else { elementType *oldobj = obj; obj = new elementType[newCapacity]; for (int i = 0; i < ObjSize; ++i) { obj[i] = oldobj[i]; } ObjCap = newCapacity; delete[]oldobj; } } template <class elementType> void My_Vector<elementType>::push_back(const elementType &t) { if (ObjSize + 1 > ObjCap)//容量不夠 { reserve(ObjCap * 2 + 1); obj[ObjSize++] = t; } else //容量夠,直接加入 { obj[ObjSize++] = t; } } template <class elementType> void My_Vector<elementType>::pop_back() { ObjSize--; } template <class elementType> void My_Vector<elementType>::clear() { ObjSize = 0; ObjCap = 1; elementType *oldobj = obj; obj = new elementType[ObjCap]; delete[]oldobj; } template <class elementType> void My_Vector<elementType>::erase(iterator &a) { for (iterator i = a; i <this->end(); i++) *i = *(i + 1); --ObjSize; } template <class elementType> elementType &My_Vector<elementType>::operator[](int index) { return obj[index]; }
測試輸出如下:#include <iostream> #include <string> #include "my_vector.cpp" using namespace std; class test { public: test() { cout << "test..." << endl; } test(const test &t) { cout << "copy test..." << endl; } ~test() { cout << "delete test..." << endl; } }; int main() { /* vector<int> a; cout << a.size() << " " << a.capacity()<<endl; for (int i = 0; i < 24;i++) a.push_back(i); cout << a.size() << " " << a.capacity() << endl; while (a.size() != a.capacity()) a.push_back(0); a.push_back(0); cout << a.size() << " " << a.capacity() << endl; vector< pair<string,int> > q; string s; int a; pair<string, int> p; // cin >> s; while (cin >> p.first >> p.second) { q.push_back(p); }*/ My_Vector<int> IntVector; int i = 1; for (int i = 0; i < 10;i++) IntVector.push_back(i); for (My_Vector<int>::iterator i = IntVector.begin(); i != IntVector.end(); i++) { cout << *i; } cout << endl; My_Vector<int>::iterator n = IntVector.begin(); IntVector.erase(n); for (My_Vector<int>::iterator i = IntVector.begin(); i != IntVector.end(); i++) { cout << *i; } cout << endl; IntVector.clear(); int a = 100; IntVector.push_back(a); for (My_Vector<int>::iterator i = IntVector.begin(); i != IntVector.end(); i++) { cout << *i; } cout << endl; My_Vector<string> stringVector; string word; while (cin >> word) { stringVector.push_back(word); } for (My_Vector<string>::iterator i = stringVector.begin(); i != stringVector.end(); i++) { cout << *i; } cout << endl; My_Vector<test> classvector; test t1; test t2; test t3; classvector.push_back(t1); classvector.push_back(t2); classvector.push_back(t3); classvector.push_back(t1); return 0; }
0123456789
123456789
100
abcd
^Z
abcd
test...
test...
test...
test...
test...
test...
test...
delete test...
test...
test...
test...
test...
test...
test...
test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
delete test...
請按任意鍵繼續. . .