c++中如何返回内置类型的最值——使用
2011-05-26 22:16
288 查看
c++中如何返回内置类型的最值——<limits>使用
在c++的<limits>中定义了如下的一个模板类: template<class Type> class numeric_limits 它可以求出下列内置类型的一些特性: wchar_t, bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double, and long double. 有这么四个函数,比较有用: max() Returns the maximum finite value for a type. min() Returns the minimum normalized value for a type. digits() Returns the number of radix digits that the type can represent without loss of precision. digits10() Returns the number of decimal digits that the type can represent without loss of precision. max()举例如下:msdn2005上的例子 // numeric_limits_max.cpp #include <iostream> #include <limits> using namespace std; int main() { cout << "The maximum value for type float is: " << numeric_limits<float>::max( ) << endl; cout << "The maximum value for type double is: " << numeric_limits<double>::max( ) << endl; cout << "The maximum value for type int is: " << numeric_limits<int>::max( ) << endl; cout << "The maximum value for type short int is: " << numeric_limits<short int>::max( ) << endl; } Output The maximum value for type float is: 3.40282e+038 The maximum value for type double is: 1.79769e+308 The maximum value for type int is: 2147483647 The maximum value for type short int is: 32767 digits()举例: // numeric_limits_digits_min.cpp #include <iostream> #include <limits> using namespace std; int main( ) { cout << numeric_limits<float>::digits <<endl; cout << numeric_limits<double>::digits <<endl; cout << numeric_limits<long double>::digits <<endl; cout << numeric_limits<int>::digits <<endl; cout << numeric_limits<__int64>::digits <<endl; } Output 24 53 53 31 63 digits10举例: // numeric_limits_digits10.cpp #include <iostream> #include <limits> using namespace std; int main( ) { cout << numeric_limits<float>::digits10 <<endl; cout << numeric_limits<double>::digits10 <<endl; cout << numeric_limits<long double>::digits10 <<endl; cout << numeric_limits<int>::digits10 <<endl; cout << numeric_limits<__int64>::digits10 <<endl; } Output 6 15 15 9 18 |
相关文章推荐
- 转载:C++中如何返回内置类型的最值—<limits>使用
- c++中如何返回内置类型的最值——使用
- C++网络编程 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- 改善C++ 程序的150个建议学习之建议19:明白在C++中如何使用C
- 客户端使用Ajax访问struts2 , struts2如何返回一个json数据!json-lib与flexjson
- 如何使用C\C++实现文件的检索&nbsp;?
- 明白在C++中如何使用C
- 如何配置才能使用cl命令编译C/C++程序?
- C++中全局变量如何使用
- C++Builder 2007系列1-如何使用TDD For C/C++
- C++如何使用MySQL数据库
- 在C++中如何将ActiveX控件所使用的BSTR数据转换成CHAR字符数组?
- 如何在Vb.NET中使用带返回的函数作为委托?
- C++和C语言中enum 如何使用
- 如何在同一个程序中混合使用C++和C
- Struts2引用的jar包版本为2.0.9的,该版本没有json包,如何使用ajax返回json数据到页面
- 在POWER平台的Linux如何使用XL C/C++
- 如何使用C++创建COM组件(ATL项目)
- J2EE下使用AJAX(六) 后记以及如何处理复杂的返回值类型
- 如何使用Visual studio C++(VC++)编译C?图解,详!!!