美文网首页
limits头文件使用

limits头文件使用

作者: geaus | 来源:发表于2020-02-10 16:20 被阅读0次

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;
}

相关文章

网友评论

      本文标题:limits头文件使用

      本文链接:https://www.haomeiwen.com/subject/cxnaxhtx.html