#pragmaoncenamespace My_vector
{template<class T>class vector{public:typedef T* iterator;//typedef受访问限定符限制,要放成公有typedefconst T* const_iterator;iterator begin(){return _start;}iterator end(){return _finish;//end和_finish都指向最后一个数的下一个}const_iterator begin()const{return _start;}const_iterator end()const{return _finish;}size_tcapacity()const{return _endofstorage - _start;}size_tsize()const{return _finish - _start;}//无参构造vector():_start(nullptr),_finish(nullptr),_endofstorage(nullptr){}//n个val值构造vector(size_t n,const T val =T()){reserve(n);for(size_t i =0; i < n; i++){push_back(val);}}vector(int n,const T val =T()){reserve(n);for(int i =0; i < n; i++){push_back(val);}}//拷贝构造vector(const vector<T>& v):_start(nullptr),_finish(nullptr),_endofstorage(nullptr){reserve(v.capacity());for(auto& e : v){push_back(e);}}//v3 = v1voidswap(vector<int>& v){std::swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_endofstorage, v._endofstorage);}vector<T>& operator=(vector<T> tmp)//传参用了拷贝构造{swap(tmp);return*this;}//迭代区间构造//类模板里可以套函数模板template<class InputIterator>vector(InputIterator first, InputIterator last):_start(nullptr),_finish(nullptr),_endofstorage(nullptr){while(first != last){push_back(*first);++first;}}~vector(){delete[] _start;_start = _finish = _endofstorage = nullptr;}voidreserve(size_t n){size_t sz =size();if(n >capacity()){T* tmp = new T[n];if(_start){//memcpy(tmp, _start, sizeof(T) * sz);for(size_t i =0; i < sz; i++){tmp[i]= _start[i];}delete[] _start;}_start = tmp;_finish = _start + sz;_endofstorage = _start + n;}}//void rsize(size_t n, T val = T())voidrsize(size_t n,const T& val =T())//匿名对象具有常性,要加const{if(n >size()){reserve(n);while(_finish < _start + n){*_finish = val;++_finish;}}if(n <=size()){_finish = _start + n;}}voidpush_back(const T& x){if(_finish == _endofstorage){/*size_t sz = size();size_t cp = capacity() == 0 ? 4 : capacity() * 2;T* tmp = new T[cp];if (_start){memcpy(tmp, _start, sizeof(T) * sz);delete[] _start;}_start = tmp;_finish = _start + sz;_endofstorage = _start + cp;*/reserve(capacity()==0?4:capacity()*2);}*_finish = x;++_finish;}//insert、erase操作后迭代器都会失效,不能再访问voidinsert(iterator pos,const T& x){assert(pos >= _start);assert(pos <= _finish);if(_finish == _endofstorage){size_t len = pos - _start;reserve(capacity()==0?4:capacity()*2);pos = _start + len;//扩容后要更新pos的相对位置}iterator end = _finish -1;while(end >= pos){*(end +1)=*end;--end;}*pos = x;++_finish;}iterator erase(iterator pos){assert(pos >= _start);assert(pos < _finish);iterator it = pos +1;while(it < _finish){*(it -1)=*it;it++;}--_finish;return pos;}T& operator[](size_t pos){assert(pos <size());return _start[pos];}const T& operator[](size_t pos)const{assert(pos <size());return _start[pos];}private:iterator _start;iterator _finish;iterator _endofstorage;};voidTest1(){vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(6);for(size_t i =0; i < v.size(); i++){cout << v[i]<<" ";}cout << endl;vector<int>::iterator it = v.begin();while(it != v.end()){*it *=10;cout <<*it <<' ';it++;}cout << endl;for(auto e : v){cout << e <<" ";}cout << endl;}voidTest2(){vector<int*> v1;v1.rsize(5);vector<string> v2;//v2.rsize(5, string("xxx")); //匿名对象v2.rsize(5,"xxx");//隐式类型转换for(auto e : v1){cout << e <<' ';}cout << endl;for(auto e : v2){cout << e <<' ';}cout << endl;}voidTest3(){vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(6);for(auto e : v){cout << e <<' ';}cout << endl;v.insert(v.begin()+2,30);v.insert(v.begin(),0);for(auto e : v){cout << e <<' ';}cout << endl;v.erase(v.begin());v.erase(v.begin()+2);for(auto e : v){cout << e <<' ';}cout << endl;}voidTest4(){vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(6);v.push_back(6);v.push_back(7);v.push_back(8);auto it = v.begin();while(it != v.end()){if(*it %2==0){it = v.erase(it);//it = it - 1;}else{it++;//不删除再++}}for(auto e : v){cout << e <<' ';}cout << endl;}voidTest5(){vector<string> v;v.push_back("111111111111111111111");v.push_back("111111111111111111111");v.push_back("111111111111111111111");v.push_back("111111111111111111111");v.push_back("111111111111111111111");for(auto e : v){cout << e <<" ";}cout << endl;}voidTest6(){vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);for(auto e : v){cout << e <<" ";}cout << endl;vector<int>v2(v);for(auto e : v2){cout << e <<" ";}cout << endl;vector<int> v3;v3.push_back(3);v3.push_back(4);v3.push_back(5);v3.push_back(6);v3.push_back(7);v2 = v3;for(auto e : v2){cout << e <<" ";}cout << endl;}voidTest7(){vector<int>v1(10,15);vector<int>v2(v1.begin(), v1.end());for(auto e : v2){cout << e <<" ";}cout << endl;vector<string>v3(5,"xxx");vector<string>v4(v3.begin(), v3.end());for(auto e : v4){cout << e <<" ";}cout << endl;}}
2、Test.cpp
#define_CRT_SECURE_NO_WARNINGS1#include<iostream>#include<assert.h>#include<string.h>#include<vector>
using namespace std;#include"1、vector的模拟实现.h"intmain(){My_vector::Test7();return0;}
1.首先在postman新建要批量运行的接口文件夹,新建一个接口,并设置好全局变量。 2.然后在Test里面设置好要断言的方法
如:
tests["Status code is 200"] responseCode.code 200;
tests["Response time is less than 10000…