当前位置:网站首页>The bottom simulation implementation of vector
The bottom simulation implementation of vector
2022-07-02 17:33:00 【Nostalgia~】
List of articles
- vector Member variables of
- Simulation Implementation of iterator
- obtain size,capacity, Empty operation
- reserve and resize Simulation Implementation of ( a key )
- pop_back ( Deletion at the end ) and push_back( Tail insertion )
- insert( Insertion at any position ) and erase ( Delete anywhere )
- structure , destructor , Copy structure , Assignment overload
- About using when expanding memcpy Some problems will be caused
vector Member variables of
What is? vector Well ? vector Is a variable array size sequence container .( It is similar to the simulation implementation sequence table )
Simulate the member variables of the implementation sequence table : The pointer ,size,capacity.
Simulation Implementation of iterator
iterator begin()
{
return _start;
}
iterator end()
{
reutrn _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
reutrn _finish;
}
obtain size,capacity, Empty operation
When data is not modified , We suggest adding const
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
bool empty() const
{
return _start == _finish;
}
reserve and resize Simulation Implementation of ( a key )
reserve in the light of capacity, When the space to be adjusted is greater than capacity Will be adjusted when , Less than capacity Don't deal with it ( But it may not be adjusted to the specified size ,
Because no matter C++ Compiler or Linux Compilers have their own ways to increase capacity )
resize Aim at size , If the number to be adjusted is less than size, Just cut off . however The number of adjustments is greater than size, Fill in the data after capacity expansion
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
//memcpy(tmp,_start,sizeof(T)*size()) If you use memcpy There will be some mistakes
// Put this at the end
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz; // there _finis , _endofstorage All need to refresh
_endofstorage = _start + n;
}
}
void resize(size_t n, const T& val = T())
{
if (n < size())
{
_finish = _start + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while (size() < n)
{
*_finish++ = val;
}
}
}
pop_back ( Deletion at the end ) and push_back( Tail insertion )
void push_back(const T& val)
{
if (_finish == _endofstorage) // Here is to judge whether it is full , Do not use _start To compare
{
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
}
*_finish = val;
_finish++;
}
void pop_back()
{
if (empty())
{
cout << "no data to delete" << endl;
return;
}
else
{
_finish--;
}
}
insert( Insertion at any position ) and erase ( Delete anywhere )
iterator insert(iterator pos, const T& val)
{
if (_finish == _endofstorage)
{
size_t len = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
// If capacity expansion occurs here ,pos Just refresh
// Otherwise it will fail
pos = _start + len;
}
iterator it = _finish - 1;
while (it >= pos)
{
*(it + 1) = *it;
--it;
}
*pos = val;
_finish++;
return pos;
}
void insert(iterator pos,size_t n ,const T& val)
{
size_t max = n + size();
if ( max > capacity())
{
size_t len = pos - _start;
reserve(max);
// If capacity expansion occurs here ,pos Just refresh
// Otherwise it will fail
pos = _start + len;
}
size_t len = n;
iterator it = _finish - 1;
while (it >= pos)
{
*(it + len) = *it;
it--;
}
while (n)
{
n--;
*pos = val;
pos++;
}
_finish = _finish + len;
}
iterator erase(iterator pos)
{
if (empty())
{
cout << " no data to delete" << endl;
}
iterator it = pos ;
while (it < _finish-1)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
structure , destructor , Copy structure , Assignment overload
vector()
:_start(nullptr)
, _finish(nullptr)
,_endofstorage(nullptr)
{
}
vector(size_t n, const T& val = T())
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
_start = new T[n];
for (size_t i = 0; i < n; i++)
{
_start[i] = val;
}
_finish = _start + n;
_endofstorage = _start + n;
}
// v1(v2)
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
_start = new T[v.capacity()];
for (size_t i = 0; i < v.size(); i++)
{
_start[i] = v._start[i];
}
_finish = _start + v.size();
_endofstorage = _start + v.capacity();
}
//v1 = v2
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
// Template functions in class templates
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
int len = last - first;
_start =_finish = new T[len];
while (first != last)
{
*_finish++ = *first++;
}
_endofstorage = _start + len;
}
void swap(vector<T>& v)
{
::swap(_start, v._start);
::swap(_finish, v._finish);
::swap(_endofstorage, v._endofstorage);
}
About using when expanding memcpy Some problems will be caused



summary : If resource management is involved in the object , Never use memcpy Copy between objects , because memcpy yes
Shallow copy , Otherwise, it may cause memory leakage and even program crash .
边栏推荐
- 深度之眼(二)——矩阵及其基本运算
- Sword finger offer 25 Merge two sorted linked lists
- ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
- Vscode knowledge points - Common Errors
- Qwebengineview crash and alternatives
- 从收集到输出:盘点那些强大的知识管理工具——优秀笔记软件盘点(四)
- 酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
- Eye of depth (II) -- matrix and its basic operations
- Migrate your accelerator based SAP commerce cloud storefront to Spartacus
- JS20 array flattening
猜你喜欢

Does digicert SSL certificate support Chinese domain name application?

Geoserver: publishing PostGIS data sources

Eye of depth (II) -- matrix and its basic operations

剑指 Offer 26. 树的子结构

ThreadLocal

Smart trash can (V) - light up OLED

The difference of message mechanism between MFC and QT

剑指 Offer 27. 二叉树的镜像

871. Minimum refueling times

After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
随机推荐
visibilitychange – 指定标签页可见时,刷新页面数据
线性规划例题 投资的收益与风险
A case study of college entrance examination prediction based on multivariate time series
Alibaba Tianchi SQL learning notes - Day3
HBuilderX运行到手机或模拟器提示没有找到设备
class和getClass()的区别
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
常用SQL语句(完整范例)
One year is worth ten years
13、Darknet YOLO3
How to quickly distinguish controlled components from uncontrolled components?
牛客 JS3 分隔符
13、Darknet YOLO3
ROS知识点——消息过滤器 ( message_filters)
剑指 Offer 22. 链表中倒数第k个节点
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
Smart trash can (V) - light up OLED
什么是敏捷开发流程