当前位置:网站首页>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 .
边栏推荐
- Uniapp H5 page calls wechat payment
- easyswoole3.2重启不成功
- 13、Darknet YOLO3
- Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- Migrate your accelerator based SAP commerce cloud storefront to Spartacus
- OpenHarmony如何启动FA(本地和远程)
- A case study of college entrance examination prediction based on multivariate time series
- Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
- Microservice architecture practice: Construction of scalable distributed database cluster
猜你喜欢
QWebEngineView崩溃及替代方案
ThreadLocal
13、Darknet YOLO3
From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)
Smart trash can (V) - light up OLED
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
Eth data set download and related problems
TCP congestion control details | 2 background
Sword finger offer 25 Merge two sorted linked lists
随机推荐
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
chrome浏览器快速访问stackoverflow
剑指 Offer 26. 树的子结构
Explanation of traceroute command
What if the default browser cannot be set?
Si446 usage record (II): generate header files using wds3
详细介绍scrollIntoView()方法属性
Briefly introduce the use of base64encoder
牛客JS2 文件扩展名
剑指 Offer 27. 二叉树的镜像
Eth data set download and related problems
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
ETH数据集下载及相关问题
easyswoole3.2重启不成功
Does digicert SSL certificate support Chinese domain name application?
[shutter] dart data type (dynamic data type)
How to create a new page for SAP Spartacus storefront
Helm kubernetes package management tool
Niuke js3 separator
默认浏览器设置不了怎么办?