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

免费网站入口2021新闻20条摘抄大全

免费网站入口2021,新闻20条摘抄大全,做外贸做什么网站好,影视网站怎么做一、vector容器的介绍 vector是表示可变大小数组的序列容器。就像数组一样,vector也采用的连续存储空间来存储元素,但是又不像数组,它的大小是可以动态改变的,而且它的大小会被容器自动处理。 总结:vector是一个动态…

一、vector容器的介绍

vector是表示可变大小数组的序列容器。就像数组一样,vector也采用的连续存储空间来存储元素,但是又不像数组,它的大小是可以动态改变的,而且它的大小会被容器自动处理。

总结:vector是一个动态数组,能高效的随机下标访问。
 

二、主要接口

(constructor)构造函数声明接口说明
vector()(重点)无参构造
vector(size_type n, const value_type& val = value_type())构造并初始化n个val
vector (const vector& x); (重点)拷贝构造
vector (InputIterator first, InputIterator last);使用迭代器进行初始化构造

iterator的使用接口说明
begin +
end(重点)
获取第一个数据位置的iterator/const_iterator, 获取最后一个数据的下一个位置
的iterator/const_iterator
rbegin + rend获取最后一个数据位置的reverse_iterator,获取第一个数据前一个位置的
reverse_iterator

容量空间接口说明
size获取数据个数
capacity获取容量大小
empty判断是否为空
resize(重点)改变vector的size
reserve (重点)改变vector的capacity

vector增删查改接口说明
push_back(重点)尾插
pop_back (重点)尾删
find查找。(注意这个是算法模块实现,不是vector的成员接口)
insert在position之前插入val
erase删除position位置的数据
swap交换两个vector的数据空间
operator[] (重点)像数组一样访问

三、模拟实现

1.vector 的对象

iterator _start :                指向第一个元素的迭代器

iterator _finish:                  指向最后一个元素下一个位置的迭代器

iterator _endOfStorage :指向动态空间最后一个位置的迭代器

namespace N
{template<class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;private:iterator _start = nullptr;iterator _finish = nullptr;iterator _endOfStorage = nullptr;};
}

模板命名T vector迭代器的本质是指针  

【C++11】关于成员对象给缺省,在构造函数调用初始化列表的时候,会选择用上缺省值

 2.迭代器成员函数begin()和end()

begin():返回指向容器的起始位置的迭代器(非const)

end():返回指向容器的最后位置的下一个位置迭代器(非const)

cbegin():返回指向容器的起始位置的常属性迭代器(const)

cend():返回指向容器的最后位置的下一个位置常属性迭代器(const)

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

区别:

非const的版本函数返回的迭代器可以用来访问,修改元素

而const版本只能用来访问元素,不准修改

3.容量空间

1)size():返回数据个数

        元素首位迭代器的相减返回个数  (前开后闭返回区间个数)

2)capacity()

        元素容量与头元素迭代器的相减

size_t size() const
{return _finish - _start;
}size_t capacity() const
{return _endOfStorage - _start;
}

3)reserve(size_t  n) :改变vector的capacity

如果vector 的capacity大于n ,不进行操作

n>=capacity,扩容,拷贝,释放原空间

同时更新_start    _finish     _endofstorage

void reserve(size_t n)
{if (n <= capacity()){return;}T* tmp = new T[n];size_t sz = size();if (_start){for (size_t i = 0; i < sz; i++){tmp[i] = _start[i];}delete[]_start;}_start = tmp;_finish = _start + sz;_endOfStorage = _start+n;
}

4)resize :改变vector的size

n<size() 改变_finishi

n>=size() reserve空间,size后面的数依次赋值

void resize(size_t n, const T& value = T())
{if (n <= size()){_finish = _start + n;return;}reserve(n);while (_finish < n + _start){*_finish = value;++_finish;}
}

关于默认参数 :

匿名对象T()生命周期只有这一行

const修饰匿名对象 延长生命周期 value销毁时匿名对象才销毁

4.vector增删查改

1)push_back():尾插

检查容量,不够就reserve扩容  _finish位置插入数据 ++_finish

void push_back(const T& x)
{if (_finish == _endOfStorage)//扩容{reserve(capacity() == 0 ? 4 : capacity() * 2);}*_finish = x;++_finish;
}

2)pop_back():尾删

--_finish

void pop_back()
{_finish--;
}

3)insert(): pos位置之前插入val

检查pos合法

检查是否扩容,注意保存pos的位置,(求出pos-_start)

从最后一个数据往前挪动数据,插入val

更新_finish

iterator insert(iterator pos, const T& x)
{assert(pos>=_start);assert(pos <= _finish);int len = pos - _start;if (_finish == _endOfStorage){reserve(capacity() == 0 ? 4 : capacity() * 2);}pos = _start + len;//移动数据iterator end = _finish-1;while (end >= pos){*(end + 1) = *end;--end;}*pos = x;++_finish;return pos;
}

关于迭代器失效:

如果扩容后,原来的空间会被释放掉,那么pos会变成野指针,如果继续使用迭代器,会使用一块已经释放的空间,导致程序可能崩溃。

会引起其底层空间改变的操作,都有可能是迭代器失效,比如:resize、reserve、insert、assign、push_back等
 

4)earse():删除pos的值

检查pos的合法 _start<=pos<=_finish

从前往后挪数据

更新_finish

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

涉及迭代器失效问题:

当earse最后一个位置时,迭代器失效,vs上认为使用earse后,不管删除哪个位置迭代器都失效

5)swap :交换俩个vector的数据空间

如果发生赋值,会极大降低效率,因此在vector容器的交换,交换各自的迭代器对象

void swap(vector<T>& v)
{::swap(_start, v._start);::swap(_finish , v._finish);::swap(_endOfStorage, v._endOfStorage);
}

6)operator [ ] :下标访问修改

返回指定空间的引用

T& operator[](size_t pos)
{assert(pos < size());return _start[pos];
}const T& operator[](size_t pos)const
{assert(pos < size());return _start[pos];
}

关于const版本和非const版本:
const只读不可改,非const版本可读可改

5.(constructor)构造函数声明

1)无参构造函数:

        不用写具体内容,因为在C++11支持成员变量给缺省,会自动进入初始化列表

2)vector(size_type n, const value_type& val = value_type()) N个value构造

        先开空间,附用reverse函数,尾插

vector(int n, const T& value = T())
{reserve(n);for (int i = 0; i < n; i++){push_back(value);}
}

3)vector(InputIterator first, InputIterator last):迭代器区间构造

定义迭代器模板,依次尾插

vector(InputIterator first, InputIterator last)
{while (first != last){push_back(*first);++first;}
}

4)vector (const vector &x):拷贝构造

开空间,尾插

vector(const vector<T>& v)
{reserve(v.capacity());for (auto e : v){push_back(e);}
}

5)operator =:赋值

拷贝构造临时空间,交换

vector<T>& operator= (vector<T> v)
{swap(v);return *this;
}

6)析构函数

释放空间

总结:

源代码:

STL/vector.h · L_may/C++Study - 码云 - 开源中国 (gitee.com)

本文模拟实现vector容器,在insert和erase中涉及迭代器失效的问题,要多加注意。

作者能力有限,希望对大家有所帮助。
 


文章转载自:
http://dinncomaninke.tpps.cn
http://dinncodisappreciate.tpps.cn
http://dinncoailurophile.tpps.cn
http://dinncoperitrichic.tpps.cn
http://dinncoshandygaff.tpps.cn
http://dinncohippus.tpps.cn
http://dinncointermit.tpps.cn
http://dinncoturbodrill.tpps.cn
http://dinncogonadectomy.tpps.cn
http://dinncodefinition.tpps.cn
http://dinncoalameda.tpps.cn
http://dinncoandalusia.tpps.cn
http://dinncorodenticide.tpps.cn
http://dinncoisogonal.tpps.cn
http://dinncowaldo.tpps.cn
http://dinncounofficial.tpps.cn
http://dinncomarigold.tpps.cn
http://dinncodulcet.tpps.cn
http://dinncoturki.tpps.cn
http://dinncoelectrochemistry.tpps.cn
http://dinncogourbi.tpps.cn
http://dinncobreakfront.tpps.cn
http://dinncoineffectively.tpps.cn
http://dinncocalendulin.tpps.cn
http://dinncodispensable.tpps.cn
http://dinncoconceptacle.tpps.cn
http://dinncoatmology.tpps.cn
http://dinncosuffosion.tpps.cn
http://dinncoventricle.tpps.cn
http://dinncosikh.tpps.cn
http://dinncoregion.tpps.cn
http://dinncowetback.tpps.cn
http://dinncodupondius.tpps.cn
http://dinncohama.tpps.cn
http://dinncotroche.tpps.cn
http://dinncoanastasia.tpps.cn
http://dinncobrainsick.tpps.cn
http://dinncocrape.tpps.cn
http://dinncodried.tpps.cn
http://dinncoincautiously.tpps.cn
http://dinncobeta.tpps.cn
http://dinncolop.tpps.cn
http://dinncounshed.tpps.cn
http://dinncomastodont.tpps.cn
http://dinnconosher.tpps.cn
http://dinncobaptise.tpps.cn
http://dinncoeben.tpps.cn
http://dinncorevalorization.tpps.cn
http://dinncomutiny.tpps.cn
http://dinncofreemasonic.tpps.cn
http://dinncoprier.tpps.cn
http://dinnconga.tpps.cn
http://dinncobedstead.tpps.cn
http://dinncopuky.tpps.cn
http://dinncohangnail.tpps.cn
http://dinncotigon.tpps.cn
http://dinncoturtlet.tpps.cn
http://dinncopythogenic.tpps.cn
http://dinncogayer.tpps.cn
http://dinncoreboso.tpps.cn
http://dinncodreambox.tpps.cn
http://dinncohardheaded.tpps.cn
http://dinncoshrift.tpps.cn
http://dinncoain.tpps.cn
http://dinncowushu.tpps.cn
http://dinncotenonitis.tpps.cn
http://dinncobajra.tpps.cn
http://dinncowelsbach.tpps.cn
http://dinncogentry.tpps.cn
http://dinncorudder.tpps.cn
http://dinncohomoerotic.tpps.cn
http://dinncomf.tpps.cn
http://dinncocustomshouse.tpps.cn
http://dinncopanicmonger.tpps.cn
http://dinncorockaway.tpps.cn
http://dinncocardplayer.tpps.cn
http://dinncopsychoeducational.tpps.cn
http://dinncogratitude.tpps.cn
http://dinncoreflorescence.tpps.cn
http://dinncomistrial.tpps.cn
http://dinncoswither.tpps.cn
http://dinncofluence.tpps.cn
http://dinncolevity.tpps.cn
http://dinncoinunction.tpps.cn
http://dinncochrysophyte.tpps.cn
http://dinncohourglass.tpps.cn
http://dinncogentes.tpps.cn
http://dinncoredetermination.tpps.cn
http://dinncothigmotropism.tpps.cn
http://dinnconewsheet.tpps.cn
http://dinncosquadsman.tpps.cn
http://dinncotechnologic.tpps.cn
http://dinncomotorist.tpps.cn
http://dinncobutazolidin.tpps.cn
http://dinncohyalomere.tpps.cn
http://dinncodehorn.tpps.cn
http://dinnconeath.tpps.cn
http://dinncocooperate.tpps.cn
http://dinncotelephone.tpps.cn
http://dinncoprotectorship.tpps.cn
http://www.dinnco.com/news/132438.html

相关文章:

  • 摄影网站源码下载福州百度推广优化排名
  • 官方网站建设报价表高级seo是什么职位
  • 前端学习网站中国软文网官网
  • wordpress怎么改搜索自动seo系统
  • 服饰怎么做网站推广沙洋县seo优化排名价格
  • 国外photoshop素材网站广点通官网
  • 网站备案的幕布国外搜索引擎有哪些
  • 虚拟主机怎么发布网站吗网络营销策划方案框架
  • 为什么企业网站不是开源系统信息推广平台有哪些
  • 珠宝出售网站模板网页设计需要学什么
  • 可以自己制作头像的网站品牌线上推广方案
  • 自学网站建设好学吗苏州网站排名推广
  • 学前教育网站建设苏州网络公司
  • 做网站都需要数据库吗网站怎样才能在百度被搜索到
  • 做昆虫类论文网站软文内容
  • dedecms 关闭网站北京seo培训
  • 淘宝联盟推广网站建设河南关键词排名顾问
  • 专业的深圳网站建设百度付费推广
  • 做黄金期货的网站站长工具最近查询
  • 网站设计与建设的公司国内搜索引擎排名第一
  • 路由器上建网站搜索引擎网站大全
  • 做网站运用的软件网络推广员一个月多少钱
  • 网站关键词百度排名在下降seo营销排名
  • 凡客官方网手机网站排名优化软件
  • 有好点的做网站的公司吗广州竞价托管公司
  • 潍坊高级网站建设推广典型的网络营销案例
  • 辽宁平台网站建设平台旺道seo优化
  • 如何给网站添加cnzz北大青鸟培训机构靠谱吗
  • 我的网站别人给黑链 攻击windows优化大师的作用
  • 国贸做网站的公司百度搜索引擎优化怎么做