(1):与MFC的兼容性

MFC程序过程中使用STL一些类编译出错,开始我认为是我写错了,放到Console Application里一切正常。
比如:
void CMyDialog::OnBnClickedButton1()
{
    double min=std::numeric_limits<double>::max();
    void *p=:perator new(count);
}

若在Console中根本没问题。但在MFC中numeric_limits错误提示:
Error 2 error C2589: '(' : illegal token on right side of '::'

operator new错误提示:
Error 1 error C2665: 'operator new' : none of the 5 overloads could convert all the argument types

解决方案:

跟Windows中定义的宏想混淆了
可以用括号改变语句的顺序,强制转换为stl中的函数名
double min=(std::numeric_limits<double>::max)();

(2): 小例展示numeric_limits的基本用法:

[cpp] view plaincopy
  1. #include <limits>
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5. cout << boolalpha;
  6. cout << "max(short): " << numeric_limits<short>::max() << endl;
  7. cout << "min(short): " << numeric_limits<short>::min() << endl;
  8. cout << "max(int): " << numeric_limits<int>::max() << endl;
  9. cout << "min(int): " << numeric_limits<int>::min() << endl;
  10. cout << "max(long): " << numeric_limits<long>::max() << endl;
  11. cout << "min(long): " << numeric_limits<long>::min() << endl;
  12. cout << endl;
  13. cout << "max(float): " << numeric_limits<float>::max() << endl;
  14. cout << "min(float): " << numeric_limits<float>::min() << endl;
  15. cout << "max(double): " << numeric_limits<double>::max() << endl;
  16. cout << "min(double): " << numeric_limits<double>::min() << endl;
  17. cout << "max(long double): " << numeric_limits<long double>::max() << endl;
  18. cout << "min(long double): " << numeric_limits<long double>::min() << endl;
  19. cout << endl;
  20. cout << "is_signed(char): "
  21. << numeric_limits<char>::is_signed << endl;
  22. cout << "is_specialized(string): "
  23. << numeric_limits<string>::is_specialized << endl;
  24. }

我机器上的运行结果:

[c-sharp] view plaincopy
  1. max(short): 32767
  2. min(short): -32768
  3. max(int): 2147483647
  4. min(int): -2147483648
  5. max(long): 2147483647
  6. min(long): -2147483648
  7. max(float): 3.40282e+038
  8. min(float): 1.17549e-038
  9. max(double): 1.79769e+308
  10. min(double): 2.22507e-308
  11. max(long double): 1.79769e+308
  12. min(long double): 2.22507e-308
  13. is_signed(char): true
  14. is_specialized(string): false
  15. 请按任意键继续. . .

关于为什么float的最小值竟然是正的?我也存在疑问,从结果中,我们看出,min返回的是float型别可以表示的最小的正值,

而不是最小的float数。从这个例子中,我们差不多了解到numeric_limits的基本用法。

3. 基本成员函数

我以float类型来展示:

[c-sharp] view plaincopy
  1. #include <limits>
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5. cout << boolalpha;
  6. // 可以表示的最大值
  7. cout << "max(float): " << numeric_limits<float>::max() << endl;
  8. // 可以表示的大于0的最小值,其他类型的实现或与此不同
  9. cout << "min(float): " << numeric_limits<float>::min() << endl;
  10. // 标准库是否为其实现了特化
  11. cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;
  12. // 是否是有符号的,即可以表示正负值
  13. cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;
  14. // 不否是整形的
  15. cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;
  16. // 是否是精确表示的
  17. cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;
  18. // 是否存在大小界限
  19. cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;
  20. // 两个比较大的数相加而不会溢出,生成一个较小的值
  21. cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;
  22. // 是否符合某某标准
  23. cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;
  24. // 不加+-号可以表示的位数
  25. cout << "digits(float): " << numeric_limits<float>::digits << endl;
  26. // 十进制数的个数
  27. cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;
  28. // 一般基数为2
  29. cout << "radix(float): " << numeric_limits<float>::radix << endl;
  30. // 以2为基数的最小指数
  31. cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;
  32. // 以2为基数的最大指数
  33. cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;
  34. // 以10为基数的最小指数
  35. cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;
  36. // 以10为基数的最大指数
  37. cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;
  38. // 1值和最接近1值的差距
  39. cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;
  40. // 舍入方式
  41. cout << "round_style(float): " << numeric_limits<float>::round_style << endl;
  42. }

运行结果:

[cpp] view plaincopy
  1. max(float): 3.40282e+038
  2. min(float): 1.17549e-038
  3. is_specialized(float): true
  4. is_signed(float): true
  5. is_integer(float): false
  6. is_exact(float): false
  7. is_bounded(float): true
  8. is_modulo(float): false
  9. is_iec559(float): true
  10. digits(float): 24
  11. digits10(float): 6
  12. radix(float): 2
  13. min_exponent(float): -125
  14. max_exponent(float): 128
  15. min_exponent10(float): -37
  16. max_exponent10(float): 38
  17. epsilon(float): 1.19209e-007
  18. round_style(float): 1
  19. 请按任意键继续. . .

(3):关于STL:Numberic_limit()

原文链接:http://www.cplusplus.com/reference/limits/numeric_limits/

class template
<limits>

std::numeric_limits

template <class T> numeric_limits;
Numeric limits type

Provides information about the properties of  arithmetic types (either integral or floating-point) in the specific platform for which the library compiles.

This class template is specialized for every  fundamental arithmetic type, with its members describing the properties of type  T. This template shall not be specialized for any other type.

Template parameters

T
A type.
If this is a fundamental arithmetic type, the members of the class describe its properties.

Template instantiations

  • C++98
  • C++11
fundamental arithmetic types
integral types bool
char
wchar_t
signed char
short int
int
long int
unsigned char
unsigned short int
unsigned int
unsigned long int
floating point types float
double
long double

For any other type, its default definition is used.

Members that produce a value of type  T are member functions, while members of specific types are static member constants:

Members

member type property
is_specialized bool true for all arithmetic types (i.e., those for which numeric_limits is specialized).
false for all other types.
min() T Minimum finite value.
For floating types with denormalization (variable number of exponent bits): minimum positive normalized value.
Equivalent to CHAR_MIN, SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, FLT_MIN,DBL_MIN, LDBL_MIN or 0, depending on type.
max() T Maximum finite value.
Equivalent to CHAR_MAX, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX, INT_MAX, UINT_MAX,LONG_MAX, ULONG_MAX, LLONG_MAX, ULLONG_MAX, UINT_LEAST16_MAX, UINT_LEAST32_MAX, FLT_MAX,DBL_MAX or LDBL_MAX, depending on type.
lowest() T Minimum finite value. (since C++11)
For integral types: the same as min().
For floating-point types: implementation-dependent; generally, the negative ofmax().
digits int For integer types: number of non-sign bits (radix base digits) in the representation.
For floating types: number of digits (in radix base) in the mantissa (equivalent toFLT_MANT_DIG, DBL_MANT_DIG or LDBL_MANT_DIG).
digits10 int Number of digits (in decimal base) that can be represented without change.
Equivalent to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.
is_signed bool true if type is signed.
is_integer bool true if type is integer.
is_exact bool true if type uses exact representations.
radix int For integer types: base of the representation.
For floating types: base of the exponent of the representation (equivalent toFLT_RADIX).
epsilon() T Machine epsilon (the difference between 1 and the least value greater than 1 that is representable).
Equivalent to FLT_EPSILON, DBL_EPSILON or LDBL_EPSILON for floating types.
round_error() T Measure of the maximum rounding error.
min_exponent int Minimum negative integer value such that radix raised to (min_exponent-1)generates a normalized floating-point number.
Equivalent to FLT_MIN_EXP, DBL_MIN_EXP or LDBL_MIN_EXP for floating types.
min_exponent10 int Minimum negative integer value such that 10 raised to that power generates a normalized floating-point number.
Equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP or LDBL_MIN_10_EXP for floating types.
max_exponent int Maximum integer value such that radix raised to (max_exponent-1) generates a representable finite floating-point number.
Equivalent to FLT_MAX_EXP, DBL_MAX_EXP or LDBL_MAX_EXP for floating types.
max_exponent10 int Maximum integer value such that 10 raised to that power generates a normalized finite floating-point number.
Equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP or LDBL_MAX_10_EXP for floating types.
has_infinity bool true if the type has a representation for positive infinity.
has_quiet_NaN bool true if the type has a representation for a quiet (non-signaling) "Not-a-Number".
has_signaling_NaN bool true if the type has a representation for a signaling "Not-a-Number".
has_denorm float_denorm_style Denormalized values (representations with a variable number of exponent bits). A type may have any of the following enum values:
denorm_absent, if it does not allow denormalized values.
denorm_present, if it allows denormalized values.
denorm_indeterminate, if indeterminate at compile time.
has_denorm_loss bool true if a loss of accuracy is detected as a denormalization loss, rather than an inexact result.
infinity() T Representation of positive infinity, if available.
quiet_NaN() T Representation of quiet (non-signaling) "Not-a-Number", if available.
signaling_NaN() T Representation of signaling "Not-a-Number", if available.
denorm_min() T Minimum positive denormalized value.
For types not allowing denormalized values: same as min().
is_iec559 bool true if the type adheres to IEC-559 / IEEE-754 standard.
An IEC-559 type always has has_infinity, has_quiet_NaN and has_signaling_NaN set to true; And infinity, quiet_NaN and signaling_NaN return some non-zero value.
is_bounded bool true if the set of values represented by the type is finite.
is_modulo bool true if the type is modulo. A type is modulo if it is possible to add two positive numbers and have a result that wraps around to a third number that is less.
traps bool true if trapping is implemented for the type.
tinyness_before bool true if tinyness is detected before rounding.
round_style float_round_style Rounding style. A type may have any of the following enum values:
round_toward_zero, if it rounds toward zero.
round_to_nearest, if it rounds to the nearest representable value.
round_toward_infinity, if it rounds toward infinity.
round_toward_neg_infinity, if it rounds toward negative infinity.
round_indeterminate, if the rounding style is indeterminable at compile time.

下面是参数的解释

digits10

返回目标类型在十进制下可以表示的最大位数

epsilon

返回目标数据类型能表示的最逼近1的正数和1的差的绝对值

has_denorm

测试目标类型是不是可以非规范化表示示

has_denorm_loss

测试所有类型是不是能测出因为非规范化而造成的精度损失(不是因为结果本身的不精确)

has_infinity

测试目标类型是不是能表示无限(比如被0除,或者其他一些情况)

has_quiet_NaN

检查目标类型是不是支持安静类型的NaN

has_signaling_NaN

检查目标类型是不是支持信号类型的NaN

infinity

检查目标类型的无限类型(如果支持无限表示)

is_bounded

检查目标类型的取值是否有限

is_exact

测试目标类型的计算结果是不是不会造成舍入误差(比如float是0)

is_iec559

测试目标类型是不是符合IEC559标准

is_integer

测试目标类型是不是可以用整型来表示(比如char是1,float是0)

is_modulo

Tests if a type has a modulo representation.

is_signed

测试目标类型是否是带符号的

is_specialized

测试目标类型是不是在numeric_limits .模板类中有特殊定义

max

返回可取的有限最大值

max_exponent

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

max_exponent10

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

min

返回可取的最小值(规范化)

min_exponent

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

min_exponent10

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

quiet_NaN

返回目标类型的安静NAN的表示

radix

Returns the integral base, referred to as radix, used for the representation of a type.

round_error

返回目标类型的最大可能的舍入误差

round_style

Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value.

signaling_NaN

返回目标类型关于信号NAN的表示

tinyness_before

测试目标类型是不是能测定出微小的舍入误差

traps

Tests whether trapping that reports on arithmetic exceptions is implemented for a type.

For all types that are not  fundamental arithmetic types, the default template definition is used:

  • C++98
  • C++11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template <class T> class numeric_limits {
public:static const bool is_specialized = false;static T min() throw();static T max() throw();static const int  digits = 0;static const int  digits10 = 0;static const bool is_signed = false;static const bool is_integer = false;static const bool is_exact = false;static const int radix = 0;static T epsilon() throw();static T round_error() throw();static const int  min_exponent = 0;static const int  min_exponent10 = 0;static const int  max_exponent = 0;static const int  max_exponent10 = 0;static const bool has_infinity = false;static const bool has_quiet_NaN = false;static const bool has_signaling_NaN = false;static const float_denorm_style has_denorm = denorm_absent;static const bool has_denorm_loss = false;static T infinity() throw();static T quiet_NaN() throw();static T signaling_NaN() throw();static T denorm_min() throw();static const bool is_iec559 = false;static const bool is_bounded = false;static const bool is_modulo = false;static const bool traps = false;static const bool tinyness_before = false;static const float_round_style round_style = round_toward_zero;
};

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// numeric_limits example
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limitsint main () {std::cout << std::boolalpha;std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';return 0;
}

Possible output:

Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false

STL:Numberic_limit()相关推荐

  1. STL库(C++11)提供的异步执行方法的方式

    在进行并发编程的时候难免会遇到异步执行时候,现代C++标准库提供了几种异步执行的方式,本文收集整理了一下,以备将来翻阅. Thread方式 Thread 是STL提供的一种快捷创建线程的方式,极大方便 ...

  2. C++ 笔记(24)— STL map 类(map实例化、插入、查找、删除)

    1. STL 映射类简介 map 和 multimap 是键-值对容器,支持根据键进行查找,区别在于,后者能够存储重复的键,而前者只能存储唯一的键. 为了实现快速查找, STL map 和 multi ...

  3. C++ 笔记(23)— STL vector 类(实例化 vector、末尾插入、指定位置插入、数组方式访问元素、指针方式访问元素、删除元素、大小与容量区别)

    1. vector 特点 vector 是一个模板类,提供了动态数组的通用功能,具有如下特点: 在数组末尾添加元素所需的时间是固定的,即在末尾插入元素的所需时间不随数组大小而异,在末尾删除元素也如此: ...

  4. C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)

    1. 实例化和赋值 STL string #include <string> #include <iostream>int main () {using namespace s ...

  5. C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)

    C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...

  6. STL map 简介

    STL map 简介 转载于:http://www.cnblogs.com/TianFang/archive/2006/12/30/607859.html 1.目录 map简介 map的功能 使用ma ...

  7. 【STL源码剖析读书笔记】【第5章】关联式容器之hashtable

    1.hashtable在插入.删除.搜寻操作上具有"常数平均时间"的表现,不依赖输入元素的随机性. 2.hashtable通过hashfunction将元素映射到不同的位置,但当不 ...

  8. C++ stl vector介绍

    转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...

  9. STL vector list deque区别与实现

    1 vector 向量 相当于一个数组     在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacitu ...

最新文章

  1. ACM1881 01背包问题应用
  2. OpenCV-Python,计算机视觉开发利器
  3. 6G目前进展与未来展望
  4. 100个短缺职业排行榜出炉 找工作,这些职业最缺人
  5. UA STAT687 线性模型理论I 线性模型概述
  6. 如何在SAP S/4HANA Cloud系统里创建employee
  7. eclipse 汉化教程(语言包)
  8. 爬取知乎回答点赞数_python3 爬虫 之只需要问题id爬取知乎问题全部回答
  9. web.config中文解释
  10. 04 Workbench几何模型的创建
  11. Hulk容器服务的镜像CI解决方案
  12. vscode 插件-常用插件
  13. 云服务器怎么划分虚拟主机,云服务器 划分虚拟主机
  14. Bugku:简单套娃
  15. 第六章 - 图像变换 - 卷积(cvFilter2D)
  16. Python数据全球人口数据
  17. java基于springboot房产备案管理系统
  18. 移动端对比后端逐渐“式微”?在互联网大环境下如何避免久当生锈的”螺丝钉”去成为一个优秀的Android开发者?
  19. 公众号PHP模板修改,PHP 实现发送模板消息(微信公众号版)
  20. go文件上传断点续传功能

热门文章

  1. INamingContainer接口解决多个自定义控件ID冲突
  2. 微信小程序自定义组件方案
  3. 南怀瑾《处事箴言》摘录
  4. teamtalk的conn框架简介及netlib线程安全问题
  5. Linux学习资料-万用字符与特殊符号
  6. nginx与apache详细性能对比
  7. Android里的shell的系统命令
  8. 在线聊天javascript代码
  9. P3978 [TJOI2015]概率论
  10. MVC与单元测试实践之健身网站(三)-角色与权限