https://www.myziyuan.com/
- 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
- 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
- 万岳科技
- 求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