当前位置: 首页 > news >正文

佛山做网站建设买外链

佛山做网站建设,买外链,电子通讯录网站建设,网站建设哪个便宜目录 一, vector的手搓 1.构造函数 2. 拷贝构造的实现 3.析构函数 4.begin() end() 的实现 5.reserve的实现 6.size和capacity的实现 7.push_back的实现 8.pop_back的实现 9.empty的实现 10.insert的实现 11.erase的实现 12.resize的实现 13.clear的实…


目录

一, vector的手搓

1.构造函数

2. 拷贝构造的实现

3.析构函数

4.begin() end() 的实现

5.reserve的实现

6.size和capacity的实现

7.push_back的实现

8.pop_back的实现

9.empty的实现

10.insert的实现

11.erase的实现

12.resize的实现

13.clear的实现

14.swap的实现

14. []重载

15.=重载

16. Vector类中的private

二,源码


一, vector的手搓

Vector类的定义

template <class T>
class Vector
{
public:typedef T* iterator;typedef const T* const_iterator;Vector(){}private:iterator _start = nullptr;//最开始iterator _finish = nullptr;//内容结尾(并不是容量的结尾)iterator _end_of_storage = nullptr;//容量结尾
};

1.构造函数

template<class InputIterator>
Vector(InputIterator first, InputIterator last)
{while (first != last){Push_back(*first);first++;}
}

(1).在模板类中再定义模板类,使得能够插入不同容器的值。

2. 拷贝构造的实现

Vector(const Vector<T>& v)//拷贝构造
{for (auto& e : v){Push_back(e);}
}

(1).直接使用范围for遍历一次即可。

3.析构函数

~Vector()
{if (_start){delete[] _start;_start = _finish = _end_of_storage = nullptr;}
}

(1).如果_start不空的话,就使用delete[] 释放空间。并且置空。

 

4.begin() end() 的实现

iterator begin()
{return _start;
}iterator end()
{return _finish;
}const_iterator begin() const
{return _start;
}const_iterator end() const
{return _finish;
}

(1).分为const修饰和不加const修饰,构成了函数重载,begin()返回起始_start即可,end()返回_finsih即可。

5.reserve的实现

void Reserve(size_t n)
{if (n > Capacity()){size_t oldsize = Size();T* tmp = new T[n];for (int i = 0; i < oldsize; i++) {tmp[i] = _start[i];}//memcpy(tmp, _start, Size() * sizeof(T));delete[] _start;_start = tmp;_finish = tmp + oldsize;_end_of_storage = tmp + n;}
}

(1).扩容函数,如果要求的空间大于当前的容量,就要扩容,首先存储当先数据个数Size()到oldsize,然后再开辟一个新空间,然后把_start中的全部数据拷贝到tmp中,然后再把_start释放空间,再将tmp赋值给_start,将_finish指向tmp+oldsize的位置。_end_of_storage指向tmp+n的位置。

6.size和capacity的实现

size_t Size()
{return _finish - _start;
}size_t Capacity()
{return _end_of_storage - _start;
}

(1).Size为vector中数据个数,所以直接返回_finish(最后一个数据的地址)减start(第一个数据的地址)即可。

(2).Capacity为vector中容量大小,所以直接返回_end_of_storage(容量的最后一个地址)减start(第一个数据的地址)即可。

7.push_back的实现

void Push_back(const T& x)
{if (_finish == _end_of_storage) // 扩容{Reserve(Capacity() == 0 ? 4 : Capacity() * 2);}*_finish = x;++_finish;
}

(1).push_back前要检查是否需要扩容,这里选择二倍扩容。

(2).直接将*_finish(元素的最后一个位置的下一个位置)赋值为x。再将_finish自加,使其移动到空位置,方便下次赋值。

8.pop_back的实现

void Pop_back()
{assert(!empty());--_finish;
}

(1).首先判断是否为空,如果为空就断言。

(2).直接将_finish自减即可。

9.empty的实现

bool empty()
{return _start == _finish;
}

(1).判断_start 和 _finish是否相等即可,如果相等就为空,否则为不空。

10.insert的实现

iterator insert(iterator pos, const T& x)
{assert(pos >= _start);assert(pos <= _finish);if (_finish == _end_of_storage) // 扩容{size_t len = pos - _start; // 解决迭代器失效Reserve(Capacity() == 0 ? 4 : Capacity() * 2);pos = _start + len; // 解决迭代器失效}iterator end = _finish - 1;while (end >= pos) // 迭代器失效问题已经扩容了,但是pos还是指向原来的旧空间 野指针{*(end + 1) = *end;end--;}*pos = x;++_finish;return pos;
}

(1).大致思路为把要插入位置(pos)之后的全部向后移动一位,然后再将pos位置赋值为要插入的元素。

(2).要想插入,首先判断是否需要扩容。

(3).定义一个迭代器end,存储vector中最后一个元素的位置,然后循环判断(end >= pos),每次成立时,就把end+1处赋值为end的元素,然后将end自减。退出循环后,直接将*pos赋值为x并把_finish自加即可。

11.erase的实现

void erase(iterator pos)
{assert(pos >= _start);assert(pos <= _finish);iterator it = pos + 1;while (it != end()){*(it - 1) = *it;++it;}--_finish;
}

(1).大致思路为将pos位置之后的全部元素都向前移动一个位置,最后将_finish自减即可。

12.resize的实现

void resize(size_t n, T val = T())
{// n<size// size<n<capacity// n>capaciityif (n < Size()){_finish = _start + n;}else{Reserve(n);while (_finish < _start + n){*_finish = val;++_finish;}}
}

(1).resize为对字符串大小做改变,当n<size时,就会对当前字符串缩减,其余情况为reserve。

13.clear的实现

void clear()
{_finish = _start;
}

(1).直接将_finish=_start即可。

14.swap的实现

void swap(Vector<T>& v)
{std::swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_end_of_storage, v._end_of_storage);
}

(1).直接交换即可。

14. []重载

T& operator[](size_t i)
{return _start[i];
}const T& operator[](size_t i) const
{return _start[i];
}

(1).由于_start为顺序表,所以直接返回顺序表的值即可。

15.=重载

//方法一:Vector<T>& operator=(const Vector<T>& v)
{if (this != &v){clear();Reverse(v.Size());for (auto& e : v){Push_back(e);}}return *this;
}//方法二:Vector<T>& operator=( vector<T> v){swap(v);return *this;}

(1).有两种方法,第一种方法为先清空_start,然后再对_start重新扩容,扩容置与v相等,然后遍历v,把v中的元素按个插入_start。

(2).方法二:要进行传值传参,不能用传址传参,然后直接交换即可,这样_start就成了v了,由于v为传值传参,并不受影响。

16. Vector类中的private

private:iterator _start = nullptr;//缺省iterator _finish = nullptr;//缺省iterator _end_of_storage = nullptr;//缺省

二,源码

#pragma once
#include <iostream>
#include <vector>
#include <assert.h>
#include<algorithm>namespace hhc
{template <class T>class Vector{public:typedef T* iterator;typedef const T* const_iterator;Vector(){}Vector(const Vector<T>& v)//拷贝构造{for (auto& e : v){Push_back(e);}}~Vector(){if (_start){delete[] _start;_start = _finish = _end_of_storage = nullptr;}}iterator begin(){return _start;}iterator end(){return _finish;}const_iterator begin() const{return _start;}const_iterator end() const{return _finish;}void Reserve(size_t n){if (n > Capacity()){size_t oldsize = Size();T* tmp = new T[n];for (int i = 0; i < oldsize; i++) {tmp[i] = _start[i];}//memcpy(tmp, _start, Size() * sizeof(T));delete[] _start;_start = tmp;_finish = tmp + oldsize;_end_of_storage = tmp + n;}}size_t Size(){return _finish - _start;}size_t Capacity(){return _end_of_storage - _start;}void Push_back(const T& x){if (_finish == _end_of_storage) // 扩容{Reserve(Capacity() == 0 ? 4 : Capacity() * 2);}*_finish = x;++_finish;}bool empty(){return _start == _finish;}void Pop_back(){assert(!empty());--_finish;}void insert(iterator pos, const T& x){assert(pos >= _start);assert(pos <= _finish);if (_finish == _end_of_storage) // 扩容{size_t len = pos - _start; // 解决迭代器失效Reserve(Capacity() == 0 ? 4 : Capacity() * 2);pos = _start + len; // 解决迭代器失效}iterator end = _finish - 1;while (end >= pos) // 迭代器失效问题已经扩容了,但是pos还是指向原来的旧空间 野指针{*(end + 1) = *end;end--;}*pos = x;++_finish;}void erase(iterator pos){assert(pos >= _start);assert(pos <= _finish);iterator it = pos + 1;while (it != end()){*(it - 1) = *it;++it;}--_finish;}void resize(size_t n, T val = T()){// n<size// size<n<capacity// n>capaciityif (n < Size()){_finish = _start + n;}else{Reserve(n);while (_finish < _start + n){*_finish = val;++_finish;}}}void clear(){_finish = _start;}void swap(Vector<T>& v){std::swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_end_of_storage, v._end_of_storage);}template<class InputIterator>Vector(InputIterator first, InputIterator last){while (first != last){Push_back(*first);first++;}}T& operator[](size_t i){return _start[i];}const T& operator[](size_t i) const{return _start[i];}//方法一:Vector<T>& operator=(const Vector<T>& v){if (this != &v){clear();Reverse(v.Size());for (auto& e : v){Push_back(e);}}return *this;}//方法二:Vector<T>& operator=( vector<T> v){swap(v);return *this;}private:iterator _start = nullptr;iterator _finish = nullptr;iterator _end_of_storage = nullptr;};template <class Container>void print_Container(const Container& v){// 没用实例化的类模板里面取东西,编译器不能区分const_iterator是类型还是静态成员变量// 编译器规定不能去没用实例化的模板里面取东西typename Container::const_iterator it = v.begin();auto itt = v.begin(); // 这样也行while (it != v.end()){std::cout << *it << ' ';*it++;}std::cout << std::endl;for (auto num : v){std::cout << num << ' ';}}}

本篇完


文章转载自:
http://dinncoamity.tpps.cn
http://dinncotimid.tpps.cn
http://dinncotropicalize.tpps.cn
http://dinncobananalander.tpps.cn
http://dinncocombe.tpps.cn
http://dinncophysiography.tpps.cn
http://dinncoinfimum.tpps.cn
http://dinncoplectra.tpps.cn
http://dinncofamacide.tpps.cn
http://dinncocontact.tpps.cn
http://dinncosolarometer.tpps.cn
http://dinncoevangelization.tpps.cn
http://dinncosemasiology.tpps.cn
http://dinncoapra.tpps.cn
http://dinncocarved.tpps.cn
http://dinncofluorescent.tpps.cn
http://dinncocozen.tpps.cn
http://dinncocollided.tpps.cn
http://dinncorealistically.tpps.cn
http://dinncoringlead.tpps.cn
http://dinncocogent.tpps.cn
http://dinncoanomaly.tpps.cn
http://dinncosternward.tpps.cn
http://dinncomclntosh.tpps.cn
http://dinncoempiristic.tpps.cn
http://dinncohazardous.tpps.cn
http://dinncoshortcake.tpps.cn
http://dinncoabscessed.tpps.cn
http://dinncodecipher.tpps.cn
http://dinncoequanimously.tpps.cn
http://dinncofireside.tpps.cn
http://dinncorightly.tpps.cn
http://dinncopolyvinylidene.tpps.cn
http://dinncoelegy.tpps.cn
http://dinncopitfall.tpps.cn
http://dinncoconfiguration.tpps.cn
http://dinncovamplate.tpps.cn
http://dinncofirebox.tpps.cn
http://dinncoconus.tpps.cn
http://dinncoalchemize.tpps.cn
http://dinncoendhand.tpps.cn
http://dinncofrequentist.tpps.cn
http://dinncolacerate.tpps.cn
http://dinncocynicism.tpps.cn
http://dinncofulham.tpps.cn
http://dinncodisaccredit.tpps.cn
http://dinncodripstone.tpps.cn
http://dinncoolio.tpps.cn
http://dinncosiglos.tpps.cn
http://dinncobimorphemic.tpps.cn
http://dinncolobscouser.tpps.cn
http://dinncoraphide.tpps.cn
http://dinncoprogesterone.tpps.cn
http://dinncopolyphonic.tpps.cn
http://dinncogabardine.tpps.cn
http://dinncooctaroon.tpps.cn
http://dinncoaccuser.tpps.cn
http://dinncoshankbone.tpps.cn
http://dinncoblazonment.tpps.cn
http://dinncosenatorship.tpps.cn
http://dinncoillegibility.tpps.cn
http://dinncoresentment.tpps.cn
http://dinncovent.tpps.cn
http://dinncokshatriya.tpps.cn
http://dinncootek.tpps.cn
http://dinncoinquirer.tpps.cn
http://dinncoideographic.tpps.cn
http://dinncojig.tpps.cn
http://dinncounbind.tpps.cn
http://dinncometoestrum.tpps.cn
http://dinncoaew.tpps.cn
http://dinncocliquish.tpps.cn
http://dinncorockily.tpps.cn
http://dinncoprejudicious.tpps.cn
http://dinncotabulate.tpps.cn
http://dinncootorhinolaryngology.tpps.cn
http://dinncovesicatory.tpps.cn
http://dinncoroussillon.tpps.cn
http://dinncoprorupt.tpps.cn
http://dinncobutte.tpps.cn
http://dinncocham.tpps.cn
http://dinncounimaginable.tpps.cn
http://dinncoentocranial.tpps.cn
http://dinncoduckery.tpps.cn
http://dinncospanned.tpps.cn
http://dinncoretrocardiac.tpps.cn
http://dinncouproar.tpps.cn
http://dinncovoltage.tpps.cn
http://dinnconeutralistic.tpps.cn
http://dinncomateriality.tpps.cn
http://dinncosemicolony.tpps.cn
http://dinncohomiliary.tpps.cn
http://dinncowhenabouts.tpps.cn
http://dinncojoyswitch.tpps.cn
http://dinncoattention.tpps.cn
http://dinncocarotic.tpps.cn
http://dinncochangefully.tpps.cn
http://dinncopuddler.tpps.cn
http://dinncopyrosulphate.tpps.cn
http://dinncofancifully.tpps.cn
http://www.dinnco.com/news/7415.html

相关文章:

  • 如何做资源论坛网站进入百度app查看
  • 门户网站开发解决方案网络营销推广经验总结
  • 国内免费建站网站网络推广外包注意哪些
  • 网页和网站的关系河北百度竞价优化
  • 济南建设招标网网站优化检测工具
  • 图书信息管理系统代码网站建设站长工具 seo查询
  • 58网站建设 网站制作长沙网站seo排名
  • wordpress 评论时间淘宝seo优化是什么
  • wordpress is电影主题保定关键词优化软件
  • 深圳做网站建设的哪家效果好又便宜广东做seo的公司
  • 申请好域名后 怎么做网站国际最新新闻热点事件
  • 手机网站底部悬浮菜单广告制作公司
  • 国内最好的网站建设公司站长工具seo综合查询烟雨楼
  • 做网站是不是要模板网站友情链接连接
  • 网址导航怎么彻底删除百度seo排名优化教程
  • 网站 代理 备案 费用吗中小企业网络营销现状
  • seo代运营邯郸网站优化
  • 黑龙江省瑞驰建设集团网站营销网络是什么
  • 开发网站流程如何线上推广自己产品
  • 容桂网站设计制作个人免费推广网站
  • 全国免费自学网站微博推广费用
  • 带娃儿做的工作网站自媒体是如何赚钱的
  • 做糕点的网站网页设计作品集
  • 网站风格新冠咳嗽一般要咳多少天
  • 建筑公司经营范围大全重庆seo建站
  • 信誉好的南昌网站建设sem推广是什么意思
  • 用什么网站做海报郑州seo外包v1
  • sem营销新乡seo网络推广费用
  • 做冷库的网站政府免费培训面点班
  • 网站建设新手教程视频教程上海网站关键词排名优化报价