当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Geoserver: publishing PostGIS data sources
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
871. 最低加油次数
Believe in yourself and finish the JVM interview this time
LeetCode:1380. Lucky number in matrix -- simple
Si446 usage record (II): generate header files using wds3
Example nonlinear integer programming
Sword finger offer 24 Reverse linked list
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
随机推荐
Ocio V2 reverse LUT
简单线性规划问题
One year is worth ten years
什么是敏捷开发流程
【GAMES101】作业4 Bézier 曲线
chmod命令原理及用法详解[通俗易懂]
TCP congestion control details | 2 background
How to transfer business data with BorgWarner through EDI?
Meanings of SNAT, DNAT and masquerade in iptables
最长无重复子数组
剑指 Offer 22. 链表中倒数第k个节点
如何给 SAP Spartacus Storefront 创建新的页面
一年頂十年
Experience home office, feel the completion of the project | community essay solicitation
How openharmony starts fa (local and remote)
线性规划例题 投资的收益与风险
OpenHarmony如何启动FA(本地和远程)
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
QWebEngineView崩溃及替代方案
Qwebengineview crash and alternatives