c++中提供了一个limits库,可以直接得到各数值类型的最大、最小值。
这里简略记录下以方便后续查阅。使用方式如下:
#include <limits>
#include <iostream>
using namespace std;
int main(){
// int32
cout<<numeric_limits<int>::max()<<endl;
cout<<numeric_limits<int>::min()<<endl;
// short
cout<<numeric_limits<int>::max()<<endl;
cout<<numeric_Limits<int>::min()<<endl;
}
此外,还有c版本的limits库climits,不需要通过上述模板库得到,而是直接通过常量定义得到各数据类型的最大最小值。
include <climits>
include<iostream>
using namespace std;
int main(){
// int32
cout<<INT_MAX<<endl;
cout<<INT_MIN<<endl;
cout<<UINT_MAX<<endl;
cout<<UINT_MIN<<endl;
cout<<LONG_MAX<<endl;
cout<<LONG_MIN<<endl;
}
网友评论