CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

没看懂,帮我看看吧

楼主adashm(猫)2005-06-01 13:39:29 在 VC/MFC / 基础类 提问

这是以前的帖子中关于max和min函数的回复,有的地方不懂  
  Summary  
   
  Finds   and   returns   the   maximum   of   a   pair   of   values.  
   
  Synopsis  
   
  #include   <algorithm>  
  template   <class   T>  
    const   T&   max(const   T&,   const   T&);  
  template   <class   T,   class   Compare>  
    const   T&   max(const   T&,   const   T&,   Compare);  
   
  Description  
   
  The   max   algorithm   determines   and   returns   the   maximum   of   a   pair   of   values.   The   optional   argument   Compare   defines   a   comparison   function   that   can   be   used   in   place   of   the   default   operator<.  
   
  max   returns   the   first   argument   when   the   arguments   are   equal.  
   
  Example  
   
  //  
  //   max.cpp  
  //  
    #include   <algorithm>    
    #include   <iostream>  
    #include   <functional>  
    using   namespace   std;  
    int   main(void)  
    {    
        double   d1   =   10.0,   d2   =   20.0;    
     
        //   Find   minimum    
        double   val1   =   min(d1,   d2);  
        //   val1   =   10.0  
   
    这里不明白,为啥min函数要返回最大值呢  
        //   the   greater   comparator   returns   the   greater   of   the  
        //   two   values.            
        double   val2   =   min(d1,   d2,   greater<double>());  
        //   val2   =   20.0;  
     
        //   Find   maximum  
        double   val3   =   max(d1,   d2);  
        //   val3   =   20.0;  
   
     
        //   the   less   comparator   returns   the   smaller   of    
        //   the   two   values.  
        //   Note   that,   like   every   comparison   in   the   STL,   max   is    
        //   defined   in   terms   of   the   <   operator,   so   using   less   here  
        //   is   the   same   as   using   the   max   algorithm   with   a   default  
        //   comparator.  
  这里为啥还是返回最大值呢  
        double   val4   =   max(d1,   d2,   less<double>());  
        //   val4   =   20    
        cout   <<   val1   <<   "   "   <<   val2   <<   "   "    
                  <<   val3   <<   "   "   <<   val4   <<   endl;  
        return   0;  
    }  
   
  Program   Output  
   
  10   20   20   20  
   
  See   Also  
   
  max_element,   min,   min_element  
  问题点数:20、回复次数:1Top

1 楼xkit(乌普)回复于 2005-06-01 20:32:33 得分 20

stl中   less   和   max   的实现  
  template   <class   _Tp>  
  struct   less   :   public   binary_function<_Tp,_Tp,bool>    
  {  
      bool   operator()(const   _Tp&   __x,   const   _Tp&   __y)   const   {   return   __x   <   __y;   }  
  };  
   
  template   <class   _Tp,   class   _Compare>  
  inline   const   _Tp&   (max)(const   _Tp&   __a,   const   _Tp&   __b,   _Compare   __comp)   {  
      return   __comp(__a,   __b)   ?   __b   :   __a;  
  }  
   
  template   <class   _Tp>  
  inline   const   _Tp&   (max)(const   _Tp&   __a,   const   _Tp&   __b)   {     return     __a   <   __b   ?   __b   :   __a;   }  
   
  若用less   则max(d1,d2)和max(d1,d2,less<double>())中d1,d2进行相同的比较,若用greater可以得到最小值Top

相关问题

  • 帮我看懂它
  • 有几个程序。。我没看懂,能请看懂了的朋友帮我解释一下吗?
  • 没看懂的文档!帮看看!
  • 你能看懂吗? 帮我注释!
  • 这个笑话我没看懂.
  • 完成端口写的一个小的测试,没有看懂。先写个给高手帮我看下。
  • MSDN中有一段代码没有看懂.希望大家帮我做个注释!
  • NOCHECK 什么意识啊,帮助没看懂
  • szayang(炸弹) !你JDK的环境设置我没有看懂!
  • 模板有什么作用啊?我怎么没看懂呢?

关键词

  • template
  • tp
  • val
  • max
  • const
  • greater
  • compare
  • maximum
  • min
  • double

得分解答快速导航

  • 帖主:adashm
  • xkit

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo