天天看点

VC6.0不支持标准库函数max和min

今天写程序时在VC6.0中用到了函数max()和min()来求最大值和最小值,结果发现VC6.0不支持这两个函数,这应该是vc的问题吧,连标准库函数都不支持,不过这也正常,毕竟VC6.0不支持C++标准。本人自己写了这两个函数,C++标准库是这样实现的,所以可以当作库函数使用。

max()和min()函数的实现如下:

template<class T, class Compare>

inline const T& max(const T& a, const T& b, Compare comp) 

{

  return comp(a, b) ? b : a;

}

  template<class T, class Compare>

inline const T& min(const T& a, const T& b, Compare comp)

  return comp(b, a) ? b : a;

使用方式如下:

bool int_less(int a, int b)

  return a < b;

int main()

  int a = 10;

  int b = 20;

  int result;

  result = max(a, b, int_less);

  cout << "max(a, b): " << result << endl;

  result = min(a, b, int_less);

  cout << "min(a, b): " << result << endl;

  return 0;

     本文转自panpan3210 51CTO博客,原文链接:http://blog.51cto.com/panpan/103074,如需转载请自行联系原作者

继续阅读