C ++变量类型限制 [英] C++ variable types limits

查看:132
本文介绍了C ++变量类型限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个很简单的问题(我认为),是否有一个STL库方法,它提供了变量类型(例如整数) 的限制? 我知道这些限制在不同的计算机上有所不同,但是必须有一种方法让他们通过一个方法,对吗?



真的很难写一个方法来计算变量类型的限制?



我只是好奇! :)

解决方案

使用 std :: numeric_limits

  // numeric_limits示例
//从链接的页面
#include< iostream>
#include< limits>
using namespace std;

int main(){
cout<< boolalpha;
cout<< int的最小值:< numeric_limits< int> :: min()<< endl;
cout<< int的最大值:< numeric_limits< int> :: max()<< endl;
cout<< int is signed:< numeric_limits< int> :: is_signed<< endl;
cout<< int中的非符号位:< numeric_limits< int> :: digits<< endl;
cout<< int has infinity:< numeric_limits< int> :: has_infinity<< endl;
return 0;
}


here is a quite simple question(I think), is there a STL library method that provides the limit of a variable type (e.g integer) ? I know these limits differ on different computers but there must be a way to get them through a method, right?

Also, would it be really hard to write a method to calculate the limit of a variable type?

I'm just curious! :)

Thanks ;).

解决方案

Use std::numeric_limits:

// numeric_limits example
// from the page I linked
#include <iostream>
#include <limits>
using namespace std;

int main () {
  cout << boolalpha;
  cout << "Minimum value for int: " << numeric_limits<int>::min() << endl;
  cout << "Maximum value for int: " << numeric_limits<int>::max() << endl;
  cout << "int is signed: " << numeric_limits<int>::is_signed << endl;
  cout << "Non-sign bits in int: " << numeric_limits<int>::digits << endl;
  cout << "int has infinity: " << numeric_limits<int>::has_infinity << endl;
  return 0;
}

这篇关于C ++变量类型限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆