c++ 源码 之 标准库min与max

我觉得学习是需要正反馈了
如果觉得标准库源代码很难读懂的话
是时候出现一些给予自己正反馈的内容

来看在 < 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 ;
  }
}

是不是得到了正反馈 :)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值