【侯捷-SL体系结构内核分析-iterator】
目录:
iterator需要遵循的原则
iterator的‘萃取机’
iterator的分类
iterator_tag 的继承关系
iterator_tag对 algorithm的影响
iterator需要遵循的原则
先来看一段代码,目的是移动 iterator 若干距离:
template<class _Iter>
using _Iter_cat_t = typename iterator_traits<_Iter>::iterator_category;
template<class _InIt,
class _Diff>
_CONSTEXPR17 void advance(_InIt& _Where, _Diff _Off)
{
// increment iterator by offset, arbitrary iterators
// we remove_const_t before _Iter_cat_t for better diagnostics if the user passes an iterator that is const
_Advance1(_Where, _Off, _Iter_cat_t<remove_const_t<_InIt>>());
}
template<class _RanIt,
class _Diff>
_CONSTEXPR17 void _Advance1(_RanIt& _Where, _Diff _Off, random_access_iterator_tag)
{
// increment iterator by offset, random-access iterators
_Where += _Off;
}
template<class _BidIt,
class _Diff>
_CONSTEXPR17 void _Advance1(_BidIt& _Where, _Diff _Off, bidirectional_iterator_tag)
{
// increment iterator by offset, bidirectional iterators
for (; 0 < _Off; --_Off)
{
++_Where;
}
// the following warning is triggered if _Diff is unsigned
#pragma warning(suppress: 6294) // Ill-defined for-loop: initial condition does not satisfy test.
// Loop body not executed.
for (; _Off < 0; ++_Off)
{
--_Where;
}
}
template<class _InIt,
class _Diff>
_CONSTEXPR17 void _Advance1(_InIt& _Where, _Diff _Off, input_iterator_tag)
{
// increment iterator by offset, input iterators
_STL_ASSERT(_Off