C++数据类型

来源:互联网 发布:你见过最美的女生知乎 编辑:程序博客网 时间:2024/05/16 15:10

C++数据类型

C++一般会接触到五种C数据类型: void, int, float, double, char.

数据类型描述void没有具体的数据类型int整型float浮点型double双精度浮点型char字符

作为补充,C++增加了两种类型:bool 和 wchar_t.

数据类型描述bool布尔型, true或者falsewchar_t宽字符

类型修饰符

前述类型中的一些可以使用signed, unsigned, short, long等关键字进行修饰。当这些关键字单独使用的时候,(被修饰的)基础数据类型假定为int。以下是可能的数据类型的完整列表(等价的类型列在一行中)。

整数类型

检查整数除法 防止除0

检查整数溢出(每个值,中间计算值)

sizeof(char[]) = string.length() +1(/0)

sizeof(char*) = 4

printf(“%llu %lli %lld”

bool size 1(可增加boo变量来简化复杂的判断)
char size 1
signed char
unsigned charwchar_tshortshort intsigned shortsigned short intunsigned shortunsigned short intintsignedsigned intunsignedunsigned intlonglong intsigned longsigned long intunsigned longunsigned long int

浮点型

避免数量级相差巨大的数之间的加减运算

避免等量判断

处理舍入误差问题:1.用双精度浮点数2 用二进制表示十进制3把浮点变量变成整形变量(*10000)

float 
专业的解法是这样的:
| u - v | / |u| <= e and | u - v | / |v| <= e(1)
| u - v | / |u| <= e or | u - v | / |v| <= e (2)
(1)式用来判断两个浮点数非常接近
2)式用来判断两个浮点数足够接近
上面的解法是BOOST库中的算法
一般常用的方法是fabs(a-b) <= numeric_limits<float>::epsilon()
或者fabs(a-b)<1e-6,后面的1e-6代表需要到达的小数精度
doublelong double(编译器)可选支持的整数类型long longlong long intsigned long longsigned long long intunsigned long longunsigned long long int
int printf ( const char * format, ... );

Print formatted data to stdout

Writes to the standard output (stdout) a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format.

Parameters

format
String that contains the text to be written to stdout.
It can optionally contain embedded format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested.
The number of arguments following the format parameters should at least be as much as the number of format tags.
The format tags follow this prototype:

%[flags][width][.precision][length]specifier
Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument:
specifierOutputExamplecCharacterad or iSigned decimal integer392eScientific notation (mantise/exponent) using e character3.9265e+2EScientific notation (mantise/exponent) using E character3.9265E+2fDecimal floating point392.65gUse the shorter of %e or %f392.65GUse the shorter of %E or %f392.65oUnsigned octal610sString of characterssampleuUnsigned decimal integer7235xUnsigned hexadecimal integer7faXUnsigned hexadecimal integer (capital letters)7FApPointer addressB800:0000nNothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
%A % followed by another % character will write % to stdout.%
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:

flagsdescription-Left-justify within the given field width; Right justification is the default (see width sub-specifier).+Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.(space)If no sign is going to be written, a blank space is inserted before the value.#Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).
widthdescription(number)Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.*The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
.precisiondescription.numberFor integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For e, E and f specifiers: this is the number of digits to be printed after the decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
.*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
lengthdescriptionhThe argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).lThe argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.LThe argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).

additional arguments
Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.

数据类型长度与取值范围

数据类型的长度和取值范围是与编译器架构相关的。可以使用sizeof操作符检测数据类型的长度(通常表述为字节数)。好在许多编译器都将数据类型长度实现为一种标准。整型和浮点型通常是32位,字符是8位,双精度(double)类型一般是64位。bool类型一般实现为8位,long long类型是64位。头文件”cfloat” (或者 “float.h”)定义了浮点型的长度和取值范围,头文件”climits” (或者 “limits.h”)定义了整型的长度和取值范围。

头文件<limits>定义了数值的取值范围。模板化的limits提供了系统相关的C++数据类型的数值范围。请选用适当的方法,同时提供如下表所示的数据类型作为模板参数。请注意,numeric_limits对于用户自定义类型是可以重载的。

方法或
常量返回值描述is_specializedbool radixintbase of exponentdigitsintnumber of radix digits in mantissadigits10intnumber of base 10 digits in mantissais_signedbool is_integerbool is_exactbool min()<type>smallest number that can be respresented (not the most negative)max()<type>largest numberepsilon()<type>inherent representation error valueround_error()<type>maximum rounding adjustment possibleinfinity()<type> quiet_NaN()<type>invalid number that does not signal floating point errorsignaling_NaN()<type>invalid number that signals floating point errordenorm_min()<type> min_exponentint min_exponent10int max_exponentint max_exponent10int has_infinitybool has_quiet_NaNbool has_signaling_NaNbool has_denorm<type>_denorm_style has_denorm_lossbool is_iec559boolconforms to IEC-559is_boundedbool is_modulobool trapsbool tinyness_beforebool round_stylefloat_round_style { round_to_nearest, … } 

最常见的用途是越界检查,即检测某种数据型类能够保存的最小值和最大值。以下代码输出了所在系统中short的最大值和最小值。

为了理解复杂声明,请遵循以下三条规则:

  1. 从变量名开始(上述例子中分别是dfoo)
  2. 到数据类型结束(上述例子中分别是doublechar )
  3. 先尽量往右看,直到必须往左的时候才往左看。(如:遇到括号)

举例:

ExpressionMeaning double **d[8];   double **d[8]; d is … double double **d[8]; d is an array of 8 … double double **d[8]; d is an array of 8 pointer to … double double **d[8]; d is an array of 8 pointer to pointer to double

另一个例子:

ExpressionMeaning char *(*(**foo [][8])())[]   char *(*(**foo [][8])())[] foo is … char char *(*(**foo [][8])())[] foo is an array of … char char *(*(**foo [][8])())[] foo is an array of an array of 8 … char char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to … char char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to … char char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning … char char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to … char char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to array of … char char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to array of pointer to char