numeric_limits 模板的相关知识点
说白了,它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个类的成员变量与成员函数中,我们可以了解到int的很多特性:可以表示的最大值,最小值,是否是精确的,是否是有符号等等。如果用其他任意(非内建类型)来特化这个模板类,比如string,string怎么可能有最大值?我们从MSDN上可以了解到,这对string,成员变量与成员函数是没有意义的,要么返回0要么为false。
具体的一些用法:
#include <limits>
#include <iostream>
using namespace std;
int main() {
cout << boolalpha;
cout << "max(short): " << numeric_limits<short>::max() << endl;
cout << "min(short): " << numeric_limits<short>::min() << endl;
cout << "max(int): " << numeric_limits<int>::max() << endl;
cout << "min(int): " << numeric_limits<int>::min() << endl;
cout << "max(long): " << numeric_limits<long>::max() << endl;
cout << "min(long): " << numeric_limits<long>::min() << endl;
cout << endl;
cout << "max(float): " << numeric_limits<float>::max() << endl;
cout << "min(float): " << numeric_limits<float>::min() << endl;
cout << "max(double): " << numeric_limits<double>::max() << endl;
cout << "min(double): " << numeric_limits<double>::min() << endl;
cout << "max(long double): " << numeric_limits<long double>::max() << endl;
cout << "min(long double): " << numeric_limits<long double>::min() << endl;
cout << endl;
cout << "is_signed(char): "
<< numeric_limits<char>::is_signed << endl;
cout << "is_specialized(string): "
<< numeric_limits<string>::is_specialized << endl;
}
运行结果:
max(short): 32767
min(short): -32768
max(int): 2147483647
min(int): -2147483648
max(long): 2147483647
min(long): -2147483648
max(float): 3.40282e+038
min(float): 1.17549e-038
max(double): 1.79769e+308
min(double): 2.22507e-308
max(long double): 1.79769e+308
min(long double): 2.22507e-308
is_signed(char): true
is_specialized(string): false
请按任意键继续. . .
关于为什么float的最小值竟然是正的?我也存在疑问,从结果中,我们看出,min返回的是float型别可以表示的最小的正值,
而不是最小的float数。
从这个例子中,我们差不多了解到numeric_limits的基本用法。
3. 基本成员函数
我以float类型来展示:
#include <limits>
#include <iostream>
using namespace std;
int main() {
cout << boolalpha;
// 可以表示的最大值
cout << "max(float): " << numeric_limits<float>::max() << endl;
// 可以表示的大于0的最小值,其他类型的实现或与此不同
cout << "min(float): " << numeric_limits<float>::min() << endl;
// 标准库是否为其实现了特化
cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;
// 是否是有符号的,即可以表示正负值
cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;
// 不否是整形的
cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;
// 是否是精确表示的
cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;
// 是否存在大小界限
cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;
// 两个比较大的数相加而不会溢出,生成一个较小的值
cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;
// 是否符合某某标准
cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;
// 不加+-号可以表示的位数
cout << "digits(float): " << numeric_limits<float>::digits << endl;
// 十进制数的个数
cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;
// 一般基数为2
cout << "radix(float): " << numeric_limits<float>::radix << endl;
// 以2为基数的最小指数
cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;
// 以2为基数的最大指数
cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;
// 以10为基数的最小指数
cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;
// 以10为基数的最大指数
cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;
// 1值和最接近1值的差距
cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;
// 舍入方式
cout << "round_style(float): " << numeric_limits<float>::round_style << endl;
}
运行结果:
max(float): 3.40282e+038
min(float): 1.17549e-038
is_specialized(float): true
is_signed(float): true
is_integer(float): false
is_exact(float): false
is_bounded(float): true
is_modulo(float): false
is_iec559(float): true
digits(float): 24
digits10(float): 6
radix(float): 2
min_exponent(float): -125
max_exponent(float): 128
min_exponent10(float): -37
max_exponent10(float): 38
epsilon(float): 1.19209e-007
round_style(float): 1
请按任意键继续. . .
Vector用于存储对象数组
常用方法
1.push_back 在数组的最后添加一个数据
2.pop_back 去掉数组的最后一个数据
3.at 得到编号位置的数据
4.begin 得到数组头的指针
5.end 得到数组的最后一个单元+1的指针
6.front 得到数组头的引用
7.back 得到数组的最后一个单元的引用
8.max_size 得到vector最大可以是多大
9.capacity 当前vector分配的大小
10.size 当前使用数据的大小
11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve 改变当前vecotr所分配空间的大小
13.erase 删除指针指向的数据项
14.clear 清空当前的vector
15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty 判断vector是否为空
18.swap 与另一个vector交换数据
numeric_limits 模板的相关知识点的更多相关文章
- Python开发一个csv比较功能相关知识点汇总及demo
Python 2.7 csv.reader(csvfile, dialect='excel', **fmtparams)的一个坑:csvfile被csv.reader生成的iterator,在遍历每二 ...
- UITableView相关知识点
//*****UITableView相关知识点*****// 1 #import "ViewController.h" // step1 要实现UITableViewDataSou ...
- Android开发涉及有点概念&相关知识点(待写)
前言,承接之前的 IOS开发涉及有点概念&相关知识点,这次归纳的是Android开发相关,好废话不说了.. 先声明下,Android开发涉及概念比IOS杂很多,可能有很多都题不到的.. 首先由 ...
- IOS开发涉及有点概念&相关知识点
前言,IOS是基于UNIX的,用C/C+/OC直通系统底层,不想android有个jvm. 首先还是系统架构的分层架构 1.核心操作系统层 Core OS,就是内存管理.文件系统.电源管理等 2.核心 ...
- IOS之UI--小实例项目--添加商品和商品名(使用xib文件终结版) + xib相关知识点总结
添加商品和商品名小项目(使用xib文件终结版) 小贴士:博文末尾有项目源码在百度云备份的下载链接. xib相关知识点总结 01-基本使用 一开始使用xib的时候,如果要使用自定义view的代码,就需要 ...
- 学习记录013-NFS相关知识点
一.NFS相关知识点 1.NFS常用的路径/etc/exports NFS服务主配置文件,配置NFS具体共享服务的地点/usr/sbin/exportfs NFS服务的管理命令,exportfs -a ...
- TCP/IP 相关知识点与面试题集
第一部分:TCP/IP相关知识点 对TCP/IP的整体认 链路层知识点 IP层知识点 运输层知识点 应用层知识点 (这些知识点都可以参考:http://www.cnblogs.com/newwy/p/ ...
- Caffe学习系列(二)Caffe代码结构梳理,及相关知识点归纳
前言: 通过检索论文.书籍.博客,继续学习Caffe,千里之行始于足下,继续努力.将自己学到的一些东西记录下来,方便日后的整理. 正文: 1.代码结构梳理 在终端下运行如下命令,可以查看caffe代码 ...
- php正则相关知识点
关于正则,其实简单就是搜索和匹配.php,java,python等都是支持正则的,php正则兼容perl.好多同学觉得正则比较难,比较抽象,其实正则是非常简单的,主要是一个熟悉和反复练习的结果,还有一 ...
随机推荐
- 关于Cocos2d-x中节点的获取
方法一: 1.在.h文件的属性里面先声明要使用的节点或者变量. private: Label *scorelabel; 2.在.cpp文件中创建并使用这个节点或者变量. scorelabel = La ...
- 谈谈Java中整数类型(short int long)的存储方式
在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对ja ...
- Swing开发图形界面有如下优势
Swing开发图形界面有如下优势 : Swing组件不再依赖于本地平台的GUI,无须采用各种平台的GUI交集,因此Swing提供了大量图形界面组件,远远超出了AWT所提供的图形界面组件集. Swing ...
- (转)I,P,B帧和PTS,DTS的关系
基本概念: I frame :帧内编码帧 又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随机访问的参考点,可以当成图象. ...
- Android Studio怎样查看资源或者函数在哪些类中被引用
很多人在做完Keymap匹配到Eclispe快捷键后,发现查看资源或者函数在哪些地方被引用的快捷键"Ctrl+Shift+G"不灵 了.你选中某个函数后,使用这个快捷键.发现仅仅会 ...
- 转载:erlang程序优化点的总结
erlang程序优化点的总结(持续更新) 转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确 ...
- 一、Open CV3.0.0 与 VS2012配置
原创:博乐Bar,转载请注明出处. 第一步,准备软件及开发环境 1.OpenCV 3.0.0 下载地址:http://www.opencv.org.cn/index.php/Download ,下载最 ...
- 使用Ultra Librarian转换芯片的Altium Designer封装格式
第一步:找到对应芯片的CAD文件,以OPA350为例: http://www.ti.com/product/opa350 RE: 使用Ultra Librarian转换TI芯片的Altium De ...
- KEGG orthology (KO) 数据库简介
KEGG, 简称京都基因组百科全书,包含了许多的数据库,对于研究基因功能来说,KEGG orthology 数据库是最基本的一个数据库: KEGG Orthology 简称KO, 对于每个功能已知的基 ...
- 调用外部 DLL 中的函数(1. 早绑定)
,b,t,);end; end.