我觉得学习是需要正反馈了
如果觉得标准库源代码很难读懂的话
是时候出现一些给予自己正反馈的内容
来看在 < bits/stl_algobase.h >里的min与max
/**
//作者的注释说了,这里就是做了 你觉得的哪些东西
* @brief This does what you think it does.
* @ingroup sorting_algorithms
* @param __a A thing of arbitrary type.
* @param __b Another thing of arbitrary type.
* @return The lesser of the parameters.
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
min(const _Tp& __a, const _Tp& __b)
{
// concept requirements
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
//return __b < __a ? __b : __a;
if (__b < __a)
return __b;
return __a;
}
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
max(const _Tp& __a, const _Tp& __b)
{
// concept requirements
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
//return __a < __b ? __b : __a;
if (__a < __b)
return __b;
return __a;
}
毫无疑问的主体就是 if的内容 至于宏则可以先不去管他
我们自己提炼一下
namespace lthstd {
template< typename _Tp>
inline const _Tp&
min (const _Tp &__a , const _Tp& __b ){
if( __a < __b )
return __a;
return __b ;
}
template < typename _Tp >
inline const _Tp&
max ( const _Tp& __a , const _Tp & __b ){
if( __a > __b )
return __a ;
return __b ;
}
}
是不是得到了正反馈 :)