1. numeric_limits是什么?
一般来说,数值型别的极值是一个与平台相关的特性。C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言,所采用的预处理常数。新的极值概念有两个优点,第一是提供更好的型别安全性,第二是程序员可借此写出一些template以核定这些极值。
2. 小例展示numeric_limits的基本用法:
#include#include using namespace std; int main() { cout << boolalpha; cout << "max(short): " << numeric_limits ::max() << endl; cout << "min(short): " << numeric_limits ::min() << endl; cout << "max(int): " << numeric_limits ::max() << endl; cout << "min(int): " << numeric_limits ::min() << endl; cout << "max(long): " << numeric_limits ::max() << endl; cout << "min(long): " << numeric_limits ::min() << endl; cout << endl; cout << "max(float): " << numeric_limits ::max() << endl; cout << "min(float): " << numeric_limits ::min() << endl; cout << "max(double): " << numeric_limits ::max() << endl; cout << "min(double): " << numeric_limits ::min() << endl; cout << "max(long double): " << numeric_limits ::max() << endl; cout << "min(long double): " << numeric_limits ::min() << endl; cout << endl; cout << "is_signed(char): " << numeric_limits ::is_signed << endl; cout << "is_specialized(string): " << numeric_limits ::is_specialized << endl; }
我机器上的运行结果:
max(short): 32767
min(short): -32768
max(int): 2147483647
min(int): -2147483648
max(long): 2147483647
min(long): -2147483648
max(float): 3.40282e+038
min(float): 1.17549e-038
max(double): 1.79769e+308
min(double): 2.22507e-308
max(long double): 1.79769e+308
min(long double): 2.22507e-308
is_signed(char): true
is_specialized(string): false
请按任意键继续. . .