求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
这代码也太不规范了,叫人怎么读啊……