概念:
基本格式:std::numeric_limits<T>::max()——其中max()可替换为min()及lowest();T表示数据类型;
std::numeric_limits<T>::max()——返回数据类型T的最大值;
std::numeric_limits<T>::min()——返回数据类型T的"最小正数";
std::numeric_limits<T>::lowest()——返回数据类型T的最小值(为负数或0);
用法举例:
#include<iostream>
#include<limits>
using namespace std;
int main(){
cout << "max(short): " << numeric_limits<short>::max() << endl;//输出short数据类型的最大值
cout << "max(int): " << numeric_limits<int>::max() << endl;
cout << "max(long): " << numeric_limits<long>::max() << endl;
cout << "max(float): " << numeric_limits<float>::max() << endl;
cout << "max(double): " << numeric_limits<double>::max() << endl;
cout << "max(long double): " << numeric_limits<long double>::max() << endl;
return 0;
}
程序运行结果如下: