可按Ctrl+D收藏 蚂蚁资源网

蚂蚁资源网

vector源码(vector长度)

  • 时间:2021-02-12 01:30 编辑:张织慧 来源:蚂蚁资源 阅读:226
  • 扫一扫,手机访问
摘要:大家好,今天给大家介绍关于vector源码(vector长度)的相关内容,详细讲解去哪里下载 c++ 标准库stl vector 的源码,如何查看c++ vector容器源代码,C++ 哪位高手有vector容器实现代码等,希望可以帮助到您。
去哪里下载 c++ 标准库stl vector 的源码,例如,您没有使用该向量,是有必要导入矢量吗?因此,文件开头存在如此句子:#include,然后右键单击向量,将有选项打开文件或转到定义,并且该选项将打开。当然,我告诉综合发展环境。
如何查看c++ vector容器源代码,1)。 #包括使用命名空间std的#include; int main(){int a [7] = {1,2,3,4,5,6,7};矢量VA(A,A + 7);对于int i = 0;我va,然后在输出中。 test.txt文本内容如下:结果如下://程序代码如下:#include #include #include#include @ consember #includ; int main(){va va ifstream in(“test.txt”); for(String s; in >> s;)va.push_back(s); for(int i = 0; i C++ 哪位高手有vector容器实现代码,使用功能模板发送邮箱以实现ach #include“vh”templatev :: v(){this-> size = 10;这个 - >容量= 10;} templatev :: v(int m = 10):size(m),容量(m){元素= new t [size]; for(int i = 0; i {元素[i] = 0;}} templatev :: v(int m,t * a):size(m),容量(m){元素= new t [size]; for (int i = 0; i {元素[i] = a [i];}} templatev :: v(const v){this-> size = x.size;>容量= x.capacity;元素= new T [容量]; for(int i = 0; i {元素[i] = x.element [i];}}}}}}}}}}}}}}}}}}}}}} choptionv&v ::运算符=(const v&x){if(==&x)返回*这个;否则{if(容量!= x.capacity){delete []元素;元素= new t [x.capacity]; size = x。大小;容量= x.capacity;} for(int i = 0 ; I {元素[I] = x.Element [I];} *返回此;}} templatt&v ::运算符[](int i)const {if(i <0||(i> size || i>容量)){over out_of_range(“索引超出范围”);否则{return元素[i];}} templatevoid v ::调整大小(int m){if(m> 0 && m!capacity){t * newArray = new t [m]; int n = 0; if(m {size = m; n = size;} n = size; t * sptr =元素; t * dptr = newArray;而(n--){* dptr = * sptr;删除[]元素;元素=纽阵列容量= m;}}}}}}}}} templatevoid v :: push_back(const t&val){if(size ==容量){if(容量!= 0)调整大小(2 *容量);否则调整大小(1);}元素[ size ++] = val; templatevoid v :: pop_back(){if(size <0){throw uchflowflow_error(“vector为空”);} size - ;} templatev v ::运算符+(v x)const {if(size!= x.size){throw lenge_error(“size错误”);否则{vv = new v(size); for(int i = 0;我{velement [i] =元素[i] + x。元素[i];}}}}}}}}} templatev v ::运算符 - (v&x)const {i(size!= x.size){throw lenge_error(“size错误”);} else {vv = new v(尺寸); for(int i = 0; i {v.element [i] =元素[i] -x.element [i];} return v;}}

责任编辑(张织慧

以上就是关于**vector源码,vector长度**的全部内容,了解更多请关注蚂蚁资源网。
  • 全部评论(3)
  • 万岳科技
  • 求STL中vector中insert函数的源代码 最好加上注释,还是直接看vector standard header比较好,直接贴出来不好阅读。源代码VC和VS的目录里有,比如VC8路径:C:\Program Files\Microsoft Visual Studio 8\VC\include里的vector。//insert_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,const _Myt& _Right, size_type _Roff, size_type _Count){// insert _Right [_Roff, _Roff + _Count) at _Offif (_Mysize < _Off || _Right.size() < _Roff)_String_base::_Xran();// _Off or _Roff off endsize_type _Num = _Right.size() - _Roff;if (_Num < _Count)_Count = _Num;// trim _Count to sizeif (npos - _Mysize <= _Count)_String_base::_Xlen();// result too longif (0 < _Count && _Grow(_Num = _Mysize + _Count)){// make room and insert new stuff_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off, _Mysize - _Off);// empty out holeif (this == &_Right)_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Myptr() + (_Off < _Roff ? _Roff + _Count : _Roff),_Count);// substringelse_Traits_helper::copy_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Right._Myptr() + _Roff, _Count);// fill hole_Eos(_Num);}return (*this);}_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,const _Elem *_Ptr, size_type _Count){// insert [_Ptr, _Ptr + _Count) at _Offif (_Inside(_Ptr))return (insert(_Off, *this,_Ptr - _Myptr(), _Count));// substringif (_Mysize < _Off)_String_base::_Xran();// _Off off endif (npos - _Mysize <= _Count)_String_base::_Xlen();// result too longsize_type _Num;if (0 < _Count && _Grow(_Num = _Mysize + _Count)){// make room and insert new stuff_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off, _Mysize - _Off);// empty out hole_Traits_helper::copy_s<_Traits>(_Myptr() + _Off, _Myres - _Off, _Ptr, _Count);// fill hole_Eos(_Num);}return (*this);}_Myt& __CLR_OR_THIS_CALL insert(size_type _Off, const _Elem *_Ptr){// insert [_Ptr, <null>) at _Offreturn (insert(_Off, _Ptr, _Traits::length(_Ptr)));}_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,size_type _Count, _Elem _Ch){// insert _Count * _Ch at _Offif (_Mysize < _Off)_String_base::_Xran();// _Off off endif (npos - _Mysize <= _Count)_String_base::_Xlen();// result too longsize_type _Num;if (0 < _Count && _Grow(_Num = _Mysize + _Count)){// make room and insert new stuff_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off, _Mysize - _Off);// empty out hole_Chassign(_Off, _Count, _Ch);// fill hole_Eos(_Num);}return (*this);}iterator __CLR_OR_THIS_CALL insert(iterator _Where){// insert <null> at _Wherereturn (insert(_Where, _Elem()));}iterator __CLR_OR_THIS_CALL insert(iterator _Where, _Elem _Ch){// insert _Ch at _Wheresize_type _Off = _Pdif(_Where, begin());insert(_Off, 1, _Ch);return (begin() + _Off);}void __CLR_OR_THIS_CALL insert(iterator _Where, size_type _Count, _Elem _Ch){// insert _Count * _Elem at _Wheresize_type _Off = _Pdif(_Where, begin());insert(_Off, _Count, _Ch);}template<class _It>void __CLR_OR_THIS_CALL insert(iterator _Where, _It _First, _It _Last){// insert [_First, _Last) at _Where_Insert(_Where, _First, _Last, _Iter_cat(_First));}template<class _It>void __CLR_OR_THIS_CALL _Insert(iterator _Where, _It _Count, _It _Ch,_Int_iterator_tag){// insert _Count * _Ch at _Whereinsert(_Where, (size_type)_Count, (_Elem)_Ch);}template<class _It>void __CLR_OR_THIS_CALL _Insert(iterator _Where, _It _First, _It _Last,input_iterator_tag){// insert [_First, _Last) at _Where, input iteratorsreplace(_Where, _Where, _First, _Last);}void __CLR_OR_THIS_CALL insert(iterator _Where, const_pointer _First, const_pointer _Last){// insert [_First, _Last) at _Where, const pointersreplace(_Where, _Where, _First, _Last);}void __CLR_OR_THIS_CALL insert(iterator _Where, const_iterator _First, const_iterator _Last){// insert [_First, _Last) at _Where, const_iteratorsreplace(_Where, _Where, _First, _Last);}//replace_Myt& __CLR_OR_THIS_CALL replace(size_type _Off, size_type _N0, const _Myt& _Right){// replace [_Off, _Off + _N0) with _Rightreturn (replace(_Off, _N0, _Right, 0, npos));}_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,size_type _N0, const _Myt& _Right, size_type _Roff, size_type _Count){// replace [_Off, _Off + _N0) with _Right [_Roff, _Roff + _Count)if (_Mysize < _Off || _Right.size() < _Roff)_String_base::_Xran();// _Off or _Roff off endif (_Mysize - _Off < _N0)_N0 = _Mysize - _Off;// trim _N0 to sizesize_type _Num = _Right.size() - _Roff;if (_Num < _Count)_Count = _Num;// trim _Count to sizeif (npos - _Count <= _Mysize - _N0)_String_base::_Xlen();// result too longsize_type _Nm = _Mysize - _N0 - _Off;// length of preserved tailsize_type _Newsize = _Mysize + _Count - _N0;if (_Mysize < _Newsize)_Grow(_Newsize);if (this != &_Right){// no overlap, just move down and copy in new stuff_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// empty hole_Traits_helper::copy_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Right._Myptr() + _Roff, _Count);// fill hole}else if (_Count <= _N0){// hole doesn't get larger, just copy in substring_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Myptr() + _Roff, _Count);// fill hole_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// move tail down}else if (_Roff <= _Off){// hole gets larger, substring begins before hole_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// move tail down_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Myptr() + _Roff, _Count);// fill hole}else if (_Off + _N0 <= _Roff){// hole gets larger, substring begins after hole_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// move tail down_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Myptr() + (_Roff + _Count - _N0), _Count);// fill hole}else{// hole gets larger, substring begins in hole_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,_Myptr() + _Roff, _N0);// fill old hole_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// move tail down_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _N0, _Myres - _Off - _N0, _Myptr() + _Roff + _Count,_Count - _N0);// fill rest of new hole}_Eos(_Newsize);return (*this);}_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,size_type _N0, const _Elem *_Ptr, size_type _Count){// replace [_Off, _Off + _N0) with [_Ptr, _Ptr + _Count)if (_Inside(_Ptr))return (replace(_Off, _N0, *this,_Ptr - _Myptr(), _Count));// substring, replace carefullyif (_Mysize < _Off)_String_base::_Xran();// _Off off endif (_Mysize - _Off < _N0)_N0 = _Mysize - _Off;// trim _N0 to sizeif (npos - _Count <= _Mysize - _N0)_String_base::_Xlen();// result too longsize_type _Nm = _Mysize - _N0 - _Off;if (_Count < _N0)_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// smaller hole, move tail upsize_type _Num;if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0)){// make room and rearrangeif (_N0 < _Count)_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// move tail down_Traits_helper::copy_s<_Traits>(_Myptr() + _Off, _Myres - _Off, _Ptr, _Count);// fill hole_Eos(_Num);}return (*this);}_Myt& __CLR_OR_THIS_CALL replace(size_type _Off, size_type _N0, const _Elem *_Ptr){// replace [_Off, _Off + _N0) with [_Ptr, <null>)return (replace(_Off, _N0, _Ptr, _Traits::length(_Ptr)));}_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,size_type _N0, size_type _Count, _Elem _Ch){// replace [_Off, _Off + _N0) with _Count * _Chif (_Mysize < _Off)_String_base::_Xran();// _Off off endif (_Mysize - _Off < _N0)_N0 = _Mysize - _Off;// trim _N0 to sizeif (npos - _Count <= _Mysize - _N0)_String_base::_Xlen();// result too longsize_type _Nm = _Mysize - _N0 - _Off;if (_Count < _N0)_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// smaller hole, move tail upsize_type _Num;if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0)){// make room and rearrangeif (_N0 < _Count)_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,_Myptr() + _Off + _N0, _Nm);// move tail down_Chassign(_Off, _Count, _Ch);// fill hole_Eos(_Num);}return (*this);}_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Myt& _Right){// replace [_First, _Last) with _Rightreturn (replace(_Pdif(_First, begin()), _Pdif(_Last, _First), _Right));}_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Elem *_Ptr,size_type _Count){// replace [_First, _Last) with [_Ptr, _Ptr + _Count)return (replace(_Pdif(_First, begin()), _Pdif(_Last, _First), _Ptr, _Count));}_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Elem *_Ptr){// replace [_First, _Last) with [_Ptr, <null>)return (replace(_Pdif(_First, begin()), _Pdif(_Last, _First), _Ptr));}_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,size_type _Count, _Elem _Ch){// replace [_First, _Last) with _Count * _Chreturn (replace(_Pdif(_First, begin()), _Pdif(_Last, _First), _Count, _Ch));}template<class _It>_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,_It _First2, _It _Last2){// replace [_First, _Last) with [_First2, _Last2)return (_Replace(_First, _Last,_First2, _Last2, _Iter_cat(_First2)));}template<class _It>_Myt& __CLR_OR_THIS_CALL _Replace(iterator _First, iterator _Last,_It _Count, _It _Ch, _Int_iterator_tag){// replace [_First, _Last) with _Count * _Chreturn (replace(_First, _Last, (size_type)_Count, (_Elem)_Ch));}template<class _It>_Myt& __CLR_OR_THIS_CALL _Replace(iterator _First, iterator _Last,_It _First2, _It _Last2, input_iterator_tag){// replace [_First, _Last) with [_First2, _Last2), input iterators_Myt _Right(_First2, _Last2);replace(_First, _Last, _Right);return (*this);}_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,const_pointer _First2, const_pointer _Last2){// replace [_First, _Last) with [_First2, _Last2), const pointersif (_First2 == _Last2)erase(_Pdif(_First, begin()), _Pdif(_Last, _First));elsereplace(_Pdif(_First, begin()), _Pdif(_Last, _First),&*_First2, _Last2 - _First2);return (*this);}_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,const_iterator _First2, const_iterator _Last2){// replace [_First, _Last) with [_First2, _Last2), const_iteratorsif (_First2 == _Last2)erase(_Pdif(_First, begin()), _Pdif(_Last, _First));elsereplace(_Pdif(_First, begin()), _Pdif(_Last, _First),&*_First2, _Last2 - _First2);return (*this);}
  • 2021-02-12 01:30:06
  • 456
  • 这个要去翻源码了,STL里的代码说实话,真的看不太懂。如果不是太纠结于具体细节,可以简单讲讲基本的实现思路,大致如下:1. vector从功能上来讲,属于顺序存储容器,所以底层实现一般基于数组。2. vector使用模板元编程技术实现,具体一点就是编译器根据使用时指定的实际类型在编译时执行模板特化,编译出对应的代码。也就是说vector<int> v1; vector<double>v2;它们各对应一个特化版本的代码。这提高了代码的抽象级别,但是对带来了代码膨胀的问题。3. vector的重要特性之一就是实现了数组的动态递增。简单来说就是容器内部记录当前的足最大容量和使用量。当添加元素的时候,如果容器类发现当前的容量已耗尽,容器类会自动地重新分配一个更大容量的数组,把当前的所有元素copy过去,然后释放掉旧的数组,从而实现动态自增,这一切对使用者来说完全透明。4. vector提供迭代器来提供统一的遍历访问接口,方便与STL中的其它组件进行交互。这其中会有很多的细节,比如:1. 是否允许vector在必要时缩小自身容量?2. vector容量耗尽后的递增量是多少?3. 是否应该提供线程安全容器?有些东西可能真的需要去翻源码去看才能搞明白。或者可以参考侯捷的《STL源码剖析》。其实vector本身的实现并不会太复杂,它的实现思路也很简单,但是设计层面的一些取舍就需要经过仔细考量了。一般来说,STL是一个足够坚实的后盾,我们会频繁地使用它,以构建健壮高效的软件。能够理解STL里的一些设计思想和实现方式,对提高我们的编程思维和编程能力会所帮助。
  • 2021-02-12 01:30:06
  • gyq
  • //vector 的遍历输出#include <iostream>#include <vector>using namespace std;int main(){int a[7]={1,2,3,4,5,6,7};vector<int> va(a,a+7);for(int i=0;i<va.size();i++)cout<<va[i]<<" ";}
  • 2021-02-25 12:40:20
最新发布的资讯信息
【简历/资料|内地女明星】 殷茹基本资料( YR个人简历介绍)(2020-12-06 15:19)
【简历/资料|内地女明星】 曹菁基本资料( CJ个人简历介绍)(2020-12-06 15:18)
【简历/资料|内地女明星】 王安妮基本资料( WAN个人简历介绍)(2020-12-06 15:18)
【简历/资料|内地女明星】 白琼基本资料( BQ个人简历介绍)(2020-12-06 15:17)
【简历/资料|内地女明星】 王世霞基本资料( WSX个人简历介绍)(2020-12-06 15:17)
【简历/资料|内地女明星】 宋煜基本资料( SY个人简历介绍)(2020-12-06 15:16)
【简历/资料|内地女明星】 钱增基本资料( QZ个人简历介绍)(2020-12-06 15:16)
【简历/资料|内地女明星】 胡晓黎基本资料( HXL个人简历介绍)(2020-12-06 15:15)
【简历/资料|内地女明星】 李佳慧基本资料( LJH个人简历介绍)(2020-12-06 15:15)
【简历/资料|内地女明星】 张洛嘉基本资料( ZLJ个人简历介绍)(2020-12-06 15:14)
联系客服
网站客服 联系客服
手机版

扫一扫进手机版
返回顶部