您的位置:首页 > 新闻 > 资讯 > 深圳策划公司排名_seo包年推广_深圳网站设计十年乐云seo_用网站模板建站

深圳策划公司排名_seo包年推广_深圳网站设计十年乐云seo_用网站模板建站

2025/1/14 3:03:54 来源:https://blog.csdn.net/N_N12/article/details/145100080  浏览:    关键词:深圳策划公司排名_seo包年推广_深圳网站设计十年乐云seo_用网站模板建站
深圳策划公司排名_seo包年推广_深圳网站设计十年乐云seo_用网站模板建站

一、构造函数

vector()   //构造函数:_start(nullptr),_finish(nullptr),_endofstorage(nullptr)
{}vector(int n, const T& val = T())//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
{reserve(n);while (n--){push_back(val);}
}template<class InputIterator>
vector(InputIterator first, InputIterator last)//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
{while (first != last){push_back(*first);first++;}
}

 二、拷贝构造

vector(const vector<T>& v)//拷贝构造:_start(nullptr),_finish(nullptr),_endofstorage(nullptr)
{reserve(v.capacity());for (auto& c : v){push_back(c);}
}

三、析构

~vector()
{delete[] _start;_start = _finish = _endofstorage = nullptr;
}

四、重载

void swap(vector<T>& 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;
}
T& operator[](size_t pos)
{assert(pos < size());return *(_start + pos);
}
const T& operator[](size_t pos) const
{assert(pos < size());return *(_start + pos);
}

五、扩容及增删

size_t size() const
{return _finish - _start;
}size_t capacity() const
{return _endofstorage - _start;
}void push_back(const T& val)
{//if (_finish == _endofstorage)//{//	reserve(capacity() == 0 ? 4 : capacity() * 2);//}//*_finish = val;//_finish++;insert(end(), val);
}void reserve(size_t n)
{if (capacity() < n){size_t sz = size();T* tmp = new T[n];if (_start){for (size_t i = 0; i < sz; i++){tmp[i] = _start[i];}delete[] _start;}_start = tmp;_finish = _start + sz;_endofstorage = _start + n;}
}void resize(size_t n, const T& val = T())
{if (n <= size()){_finish = _start + n;}else{reserve(n);while (_finish != _start + n){*_finish = val;_finish++;}}
}
void pop_back()
{_finish--;
}
void insert(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;}iterator end = _finish - 1;while (end >= pos){*(end + 1) = *end;end--;}*pos = x;_finish++;
}void erase(iterator pos)
{assert(pos >= _start);assert(pos < _finish);while (pos != end()){*pos = *(pos + 1);pos++;}_finish--;
}

总体:

namespace asd //设一个命名空间避免与std冲突
{template<class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;vector()   //构造函数:_start(nullptr),_finish(nullptr),_endofstorage(nullptr){}vector(int n, const T& val = T())//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr){reserve(n);while (n--){push_back(val);}}template<class InputIterator>vector(InputIterator first, InputIterator last)//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr){while (first != last){push_back(*first);first++;}}vector(const vector<T>& v)//拷贝构造:_start(nullptr),_finish(nullptr),_endofstorage(nullptr){reserve(v.capacity());for (auto& c : v){push_back(c);}}vector<T>& operator=(vector<T> tmp){swap(tmp);return *this;}~vector(){delete[] _start;_start = _finish = _endofstorage = nullptr;}//迭代器iterator begin(){return _start;}iterator end(){return _finish;}const_iterator cbegin() const{return _start;}const_iterator cend() const{return _finish;}size_t size() const{return _finish - _start;}size_t capacity() const{return _endofstorage - _start;}void push_back(const T& val){//if (_finish == _endofstorage)//{//	reserve(capacity() == 0 ? 4 : capacity() * 2);//}//*_finish = val;//_finish++;insert(end(), val);}void reserve(size_t n){if (capacity() < n){size_t sz = size();T* tmp = new T[n];if (_start){for (size_t i = 0; i < sz; i++){tmp[i] = _start[i];}delete[] _start;}_start = tmp;_finish = _start + sz;_endofstorage = _start + n;}}void resize(size_t n, const T& val = T()){if (n <= size()){_finish = _start + n;}else{reserve(n);while (_finish != _start + n){*_finish = val;_finish++;}}}T& operator[](size_t pos){assert(pos < size());return *(_start + pos);}const T& operator[](size_t pos) const{assert(pos < size());return *(_start + pos);}void pop_back(){_finish--;}void swap(vector<T>& v){std:: swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_endofstorage, v._endofstorage);}void insert(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;}iterator end = _finish - 1;while (end >= pos){*(end + 1) = *end;end--;}*pos = x;_finish++;}void erase(iterator pos){assert(pos >= _start);assert(pos < _finish);while (pos != end()){*pos = *(pos + 1);pos++;}_finish--;}private:iterator _start;iterator _finish;iterator _endofstorage;};
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com