问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求STL中vector中insert函数的源代码 最好加上注释

发布网友 发布时间:2023-06-08 12:06

我来回答

3个回答

热心网友 时间:2024-01-11 01:31

还是直接看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 _Off
if (_Mysize < _Off || _Right.size() < _Roff)
_String_base::_Xran();// _Off or _Roff off end
size_type _Num = _Right.size() - _Roff;
if (_Num < _Count)
_Count = _Num;// trim _Count to size
if (npos - _Mysize <= _Count)
_String_base::_Xlen();// result too long

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
if (this == &_Right)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + (_Off < _Roff ? _Roff + _Count : _Roff),
_Count);// substring
else
_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 _Off
if (_Inside(_Ptr))
return (insert(_Off, *this,
_Ptr - _Myptr(), _Count));// substring
if (_Mysize < _Off)
_String_base::_Xran();// _Off off end
if (npos - _Mysize <= _Count)
_String_base::_Xlen();// result too long
size_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 _Off
return (insert(_Off, _Ptr, _Traits::length(_Ptr)));
}

_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
size_type _Count, _Elem _Ch)
{// insert _Count * _Ch at _Off
if (_Mysize < _Off)
_String_base::_Xran();// _Off off end
if (npos - _Mysize <= _Count)
_String_base::_Xlen();// result too long
size_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 _Where
return (insert(_Where, _Elem()));
}

iterator __CLR_OR_THIS_CALL insert(iterator _Where, _Elem _Ch)
{// insert _Ch at _Where
size_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 _Where
size_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 _Where
insert(_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 iterators
replace(_Where, _Where, _First, _Last);
}

void __CLR_OR_THIS_CALL insert(iterator _Where, const_pointer _First, const_pointer _Last)
{// insert [_First, _Last) at _Where, const pointers
replace(_Where, _Where, _First, _Last);
}

void __CLR_OR_THIS_CALL insert(iterator _Where, const_iterator _First, const_iterator _Last)
{// insert [_First, _Last) at _Where, const_iterators
replace(_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 _Right
return (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 end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off;// trim _N0 to size
size_type _Num = _Right.size() - _Roff;
if (_Num < _Count)
_Count = _Num;// trim _Count to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen();// result too long

size_type _Nm = _Mysize - _N0 - _Off;// length of preserved tail
size_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 carefully
if (_Mysize < _Off)
_String_base::_Xran();// _Off off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off;// trim _N0 to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen();// result too long
size_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 up
size_type _Num;
if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0))
{// make room and rearrange
if (_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 * _Ch
if (_Mysize < _Off)
_String_base::_Xran();// _Off off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off;// trim _N0 to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen();// result too long
size_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 up
size_type _Num;
if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0))
{// make room and rearrange
if (_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 _Right
return (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 * _Ch
return (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 * _Ch
return (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 pointers
if (_First2 == _Last2)
erase(_Pdif(_First, begin()), _Pdif(_Last, _First));
else
replace(_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_iterators
if (_First2 == _Last2)
erase(_Pdif(_First, begin()), _Pdif(_Last, _First));
else
replace(_Pdif(_First, begin()), _Pdif(_Last, _First),
&*_First2, _Last2 - _First2);
return (*this);
}

热心网友 时间:2024-01-11 01:31

在程序安装目录下面就有源代码 C:\Program Files\Microsoft Visual Studio 10\VC\include里的vector。

热心网友 时间:2024-01-11 01:32

这代码也太不规范了,叫人怎么读啊……
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
win7窗口淡入淡出慢 iPhone 小技巧:如何将 Safari 浏览器中的网页添加到桌面? 锰粉是什么颜色的金属 铁精粉的常见颜色是什么 烟花爆竹中的金属粉末如何使其呈现绚丽色彩? 金粉有哪些种类 灰土每层回填最大厚度 灰土基础简史 如何让一个喜欢你的女孩讨厌你! 西环里新区基本资料 工资卡被冻结了开工资钱怎么给我 工资打到冻结卡能撤回吗 正月十五元宵节能扔垃圾吗 正月十五可以出门丢垃圾吗 海尔曲面电视主板LQ55AL88Y81和LQ55AL88Y7l两种主板通用吗?_百度... 511.15和512.78这两个显卡版本哪个更好 CPU和显卡和主板是通用的么 酷睿4代和5代主板通用吗 asc5510变频器的cpu板从1.1~160kw都通用吗? 大年初七能去祭拜老人吗? 麝香手串女士可以戴吗麝香手串什么人可以戴 为什么没结婚的人不能用麝香? 我父亲检查发现 尿素氮10 尿蛋白 NAG113 RBP2.58 是否严重 尿液检查中视黄醇结合蛋白rbp是什么慨念 Upcoming Events! March – Dragon City Game guide 中岛沙树的演出作品 Alex Masters的long way home 的中文歌词 how to train your dragon和 dragon masters一样吗 dragonmasters里面的各个孩子对应的龙 开天起皇族胜什么梗 read music的意思 为什么std vector insert复杂度是线性的 挑日子剖腹产出生的小孩命 选吉日吉时剖腹产,宝宝就有“富贵命”吗? 男孩剖腹产选单日子还是双日子好 有何说法 2022年影响高速公路通行费征收的原因 花呗专属提额码 PNCR与SNCR脱硝设备有什么不一样? 雇佣兵是什么兵种 梦见跟曾经的朋友洗碗的预兆 财能的词语财能的词语是什么 羡财的词语羡财的词语是什么 横财的词语横财的词语是什么 斥绌的词语斥绌的词语是什么 浪客行女主当说客是什么意思 《浪客行》里的杰西卡虽然很坏,但是太A了 东北人管饼干叫berber怎么写 柏柏尔人简介 ...解释这句话的出处吗?在现实社会中,它所表达的含义与原意有何区别... 上海虹桥站到嘉定客运中心怎么坐车 上海虹桥高铁站到嘉定育英路怎么走