当前位置:网站首页>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 .
边栏推荐
- Si446 usage record (I): basic data acquisition
- 【目标跟踪】|SiamFC
- ROS knowledge point - message_filters
- 微信小程序 —— 上下浮动的箭头
- 【GAMES101】作业4 Bézier 曲线
- Use the API port of the bridge of knowledge and action to provide resources for partners to access
- Income and risk of linear programming example investment
- 链表求和[dummy+尾插法+函数处理链表引用常见坑位]
- traceroute命令讲解
- 线性规划例题 投资的收益与风险
猜你喜欢

体验居家办公完成项目有感 | 社区征文

Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei

ETH数据集下载及相关问题

Timing / counter of 32 and 51 single chip microcomputer

Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
![[web technology] 1233 seconds understand web component](/img/42/c98d8112dc4ece0a92dda97647be5c.jpg)
[web technology] 1233 seconds understand web component

From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)

Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers

ThreadLocal

Alibaba Tianchi SQL learning notes - Day3
随机推荐
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
IDEA2021.1 安装教程
Update iteration of cloud communication interface -- the official launch of subail API V4
PCL knowledge points - voxelized grid method for down sampling of point clouds
Simple linear programming problem
QWebEngineView崩溃及替代方案
Introduction to nexus and detailed tutorial of Xiaobai using idea to package and upload to nexus3 private server
Blog theme "text" summer fresh Special Edition
13、Darknet YOLO3
Believe in yourself and finish the JVM interview this time
Vscode knowledge points - Common Errors
Microservice architecture practice: Construction of scalable distributed database cluster
VScode知识点——常见报错
13、Darknet YOLO3
How to transfer business data with BorgWarner through EDI?
Timing / counter of 32 and 51 single chip microcomputer
[shutter] dart data type (dynamic data type)
Niuke JS2 file extension
CEPH principle
【GAMES101】作业4 Bézier 曲线