当前位置:网站首页>vector的底层模拟实现
vector的底层模拟实现
2022-07-02 15:07:00 【念旧~】
文章目录
vector的成员变量
什么是vector呢? vector是可变数组大小的序列容器。(跟模拟实现顺序表差不多)
模拟实现顺序表的成员变量:指针,size,capacity。
迭代器的模拟实现
iterator begin()
{
return _start;
}
iterator end()
{
reutrn _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
reutrn _finish;
}
获取size,capacity,判空操作
不修改数据的时候,我们建议都加上const
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
bool empty() const
{
return _start == _finish;
}
reserve和resize的模拟实现(重点)
reserve针对capacity,当要调整的空间大于capacity时才会调整,小于capacity不做处理(但是不一定会调整到指定大小,
因为无论是C++编译器还是Linux编译器都有自己的增容方式)
resize针对于size ,如果要调整的个数小于size,直接砍掉。 但是调整的个数大于size,扩容后填数据
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
//memcpy(tmp,_start,sizeof(T)*size()) 如果用memcpy会出现一些错误
//这个放到最后面再说
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz; //这里的_finis , _endofstorage都要重新刷新一下
_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 (尾删)和 push_back(尾插)
void push_back(const T& val)
{
if (_finish == _endofstorage) //这里是判断是否满了,不要用_start去比较了
{
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(任意位置的插入) 和 erase (任意位置的删除)
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);
//这里如果发生了扩容,pos就要刷新一下
//否则就失效了
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);
//这里如果发生了扩容,pos就要刷新一下
//否则就失效了
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;
}
构造,析构,拷贝构造,赋值重载
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<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);
}
关于当扩容的时候用memcpy会引发的一些问题
总结:如果对象中涉及到资源管理时,千万不能使用memcpy进行对象之间的拷贝,因为memcpy是
浅拷贝,否则可能会引起内存泄漏甚至程序崩溃。
边栏推荐
- Tech talk activity preview | building intelligent visual products based on Amazon kVs
- Sword finger offer 22 The penultimate node in the linked list
- ceph 原理
- SAP Commerce Cloud 架构概述
- Chmod command principle and usage details [easy to understand]
- Exploration of mobile application performance tools
- Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
- 13、Darknet YOLO3
- The computer comes with software to make the background color of the picture transparent (matting white background)
- VScode知识点——常见报错
猜你喜欢
Exploration of mobile application performance tools
几行代码搞定RPC服务注册和发现
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
【Leetcode】13. Roman numeral to integer
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
Interpretation of key parameters in MOSFET device manual
Qwebengineview crash and alternatives
Amazon cloud technology community builder application window opens
Sword finger offer 27 Image of binary tree
剑指 Offer 25. 合并两个排序的链表
随机推荐
Connect Porsche and 3PL EDI cases
Vscode knowledge points - Common Errors
class和getClass()的区别
Uniapp H5 page calls wechat payment
Vscode + eslint configuration
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
Configure lamp+supervisor
线性规划例题 投资的收益与风险
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
IPtables中SNAT、DNAT和MASQUERADE的含义
深度之眼(三)——矩阵的行列式
简单线性规划问题
ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
QStyle实现自绘界面项目实战(二)
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
如何给 SAP Spartacus Storefront 创建新的页面
求简单微分方程
Schoolbag novel multithreaded crawler [easy to understand]
executescalar mysql_ ExecuteScalar()
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东