#include //定义了符号常量来表示整型类型的限制,包括了符号常量CHAR_BIT,CHAR_MAX,CHAR_MIN等等
#include //定义了符号常量浮点类型的限制,包括了符号常量DBL_DIG,FLT_DIG等等
对于变量,为什么一定是先定义,再赋值?
防止程序员敲错变量名以至于新建了新的变量,从而导致调试时难以找出问题。
注意:char类型的负值显示不出来
字符集太多,8位的char类型无法表示,使用wchar_t(宽字符类型)表示拓展字符集。前缀:L
它的长度和符合属性和另一种底层类型相同,具体是那种底层类型取决于实现。
无符号16位,前缀:u
无符号32位,前缀:U
C++要求short类型不少于16位
根据实验结果判断,本系统中short类型是16位的
C++要求int类型至少与short类型一样长
根据实验结果判断,本系统中int类型是32位的
C++要求long至少为32位,且至少与int一样长
根据实验结果判断,本系统中long类型是32位的
C++要求long long至少为64位,且至少与long一样长
根据实验结果判断,本系统中long long类型是64位的
只有一位 1表示True 0表示False
有数据表示为True,0表示为False
浮点数书写:
C++要求至少为32位。后缀F or f
C++要求至少为48位,且不少于float
C++要求为80,96,128位,至少和double类型位数一样多
sizeof() — 计算变量所占内存空间,以字节为单位
sizeof的使用方式:sizeof(var) or sizeof var
可以让编译器根据初始值的类型推断变量的类型,只能用于单值初始化,不能用于多值初始化
关键字decltype将变量的类型声明为表达式的指定的类型。
decltype的工作原理比auto的复杂,根据指定的表达式,指定的类型可以为const或引用
格式:decltype(x) y;//其中x为表达式,此式将把y声明为x表达式的类型
应用于模板:
template<typename T, typename U)
void ef(T t, U u)
{
decltype(T*U) tu;
...
}
在函数名和参数列表后面指定返回值类型:
auto f2(double, int) -> double; // new syntax, return type is double
与decltype组合用于模板:使用返回类型后置的原因是在遇到eff前,T和U还不在作用域内,因此需要将其放在后面
template<typename T, typename U)
auto eff(T t, U u) -> decltype(T*U)
{
...
}
nullptr表示一个空指针,它是一个指针不能转换为int类型;为了兼容老版本 nullptr==0 依然会返回true;
但是使用nullptr具备更好的安全性,比如不能将nullptr作为形参类型为int类型的实参。
代码:
/*
Project name : _1Data_type
Last modified Date: 2022年3月10日14点41分
Last Version: V1.0
Descriptions: 总结C++中的数据类型
*/
#include
#include //定义了符号常量来表示整型类型的限制,包括了符号常量CHAR_BIT,CHAR_MAX,CHAR_MIN等等
#include //定义了符号常量浮点类型的限制,包括了符号常量DBL_DIG,FLT_DIG等等
#include
int main()
{
using namespace std;
cout << "整型类型******************************************************************" << endl;
cout << "char类型******************************************************************" << endl;
char var_char = 'a';//定义+赋值传统方法
char var_char1('b');//定义+赋值新方法
signed char var_char2 = {'x'};//定义+赋值新方法
unsigned char var_char3('y');
cout << "Data Type char:var_char = " << var_char<<endl; //Data Type char:var_char = a
cout << "sizeof(char) = " << sizeof(char) << endl; //sizeof(char) = 1---表示1个字节
cout << "CHAR_BIT = " << CHAR_BIT<<endl; //CHAR_BIT = 8
cout << "CHAR_MAX = " << CHAR_MAX<<endl; //CHAR_MAX = 127
cout << "CHAR_MIN = " << CHAR_MIN <<endl; //CHAR_MIN = -128
cout << "Data Type signed char:var_char2 = " << var_char2 << endl; //Data Type signed char:var_char2 = x
cout << "SCHAR_MAX = " << SCHAR_MAX << endl; //CHAR_MAX = 127
cout << "SCHAR_MIN = " << SCHAR_MIN << endl; //CHAR_MIN = -128
cout << "Data Type unsigned char:var_char3 = " << var_char3 << endl; //Data Type unsigned char:var_char3 = y
cout << "UCHAR_MAX = " << UCHAR_MAX << endl; //CHAR_MAX = 255
cout << "wchar_t类型******************************************************************" << endl;
wchar_t var_wchar_t = L'P';//定义+赋值传统方法
wcout << "Data Type char:var_wchar_t = " << var_wchar_t << endl; //Data Type char:var_char = a
cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << endl; //sizeof(wchar_t) = 2---表示2个字节
cout << "char16_t and char32_t类型******************************************************************" << endl;
char16_t var_char16_t = u'P';//定义+赋值传统方法
cout << "Data Type char16_t:var_char16_t = " << var_char16_t << endl; //Data Type char16_t:var_char16_t = 80 输出的是Unicode编码
cout << "sizeof(char16_t) = " << sizeof(char16_t) << endl; //sizeof(char16_t) = 2---表示2个字节
char32_t var_char32_t = U'P';//定义+赋值传统方法
cout << "Data Type char16_t:var_char32_t = " << var_char32_t << endl; //Data Type char16_t:var_char32_t = 80 输出的是Unicode编码
cout << "sizeof(char32_t) = " << sizeof(char32_t) << endl; //sizeof(char32_t) = 4---表示4个字节
cout << "short类型******************************************************************" << endl;
short var_short = 99;//定义+赋值传统方法
unsigned short var_short1(88);//定义+赋值新方法
short var_short2 = { 99 };//定义+赋值新方法
cout << "Data Type short:var_short = " << var_short << endl; //Data Type short:var_short = 99
cout << "sizeof(short) = " << sizeof(short) << endl; //sizeof(short) = 2---表示2个字节
cout << "SHRT_MAX = " << SHRT_MAX << endl; //SHRT_MAX = 32767
cout << "SHRT_MIN = " << SHRT_MIN << endl; //SHRT_MAX = -32768
cout << "Data Type unsigned short:var_short1 = " << var_short1 << endl; //Data Type unsigned short:var_short1 = 88
cout << "USHRT_MAX = " << USHRT_MAX << endl; //USHRT_MAX = 65535
cout << "int类型******************************************************************" << endl;
int var_int = 99;//定义+赋值传统方法
unsigned int var_int1(88);//定义+赋值新方法
unsigned int var_int2 = {999};//定义+赋值新方法
cout << "Data Type int:var_int = " << var_int << endl; //Data Type int:var_int = 99
cout << "sizeof(int) = " << sizeof(int) << endl; //sizeof(int) = 4---表示4个字节
cout << "INT_MAX = " << INT_MAX << endl; //INT_MAX = 2147483647
cout << "INT_MIN = " << INT_MIN << endl; //INT_MIN = -2147483648
cout << "Data Type unsigned int:var_in1t = " << var_int1 << endl; //Data Type unsigned int:var_in1t = 88
cout << "UINT_MAX = " << UINT_MAX << endl; //UINT_MAX = 4294967295
cout << "long类型******************************************************************" << endl;
long var_long = 2147483647;//定义+赋值传统方法
unsigned long var_long1(4294967295);//定义+赋值新方法
unsigned long var_long2 = {9999};//定义+赋值新方法
cout << "Data Type long:var_long = " << var_long << endl; //Data Type long:var_long = 2147483647
cout << "sizeof(long) = " << sizeof(long) << endl; //sizeof(long) = 4---表示4个字节
cout << "LONG_MAX = " << LONG_MAX << endl; //INT_MAX = 2147483647
cout << "LONG_MIN = " << LONG_MIN << endl; //INT_MIN = -2147483648
cout << "Data Type unsigned long:var_long1 = " << var_long1 << endl; //Data Type unsigned long:var_long1 = 4294967295
cout << "ULONG_MAX = " << ULONG_MAX << endl; //ULONG_MAX = 4294967295
cout << "long long类型******************************************************************" << endl;
long long var_long_long = 2147483647;//定义+赋值传统方法
unsigned long long var_long_long1(4294967295);//定义+赋值新方法
unsigned long long var_long_long2 = {999999};//定义+赋值新方法
cout << "Data Type long long:var_long_long = " << var_long_long << endl; //Data Type long long:var_long_long = 2147483647
cout << "sizeof(long long) = " << sizeof(long long) << endl; //sizeof(long long) = 8---表示8个字节
cout << "LLONG_MAX = " << LLONG_MAX << endl; //LLONG_MAX = 9223372036854775807
cout << "LLONG_MIN = " << LLONG_MIN << endl; //LLONG_MIN = -9223372036854775808
cout << "Data Type unsigned long long:var_long_long1 = " << var_long_long1 << endl; //Data Type unsigned long long:var_long_long1 = 4294967295
cout << "ULLONG_MAX = " << ULLONG_MAX << endl; //ULLONG_MAX = 18446744073709551615
cout << "bool类型******************************************************************" << endl;
bool var_bool = -11;
cout << "Data Type bool:var_bool = " << var_bool << endl; //Data Type bool:var_bool = 1
cout << "浮点类型******************************************************************" << endl;
cout << "float类型******************************************************************" << endl;
cout.setf(ios_base::fixed, ios_base::floatfield);//设置cout浮点显示
float var_float = 9.999;
cout << "Data Type float:var_float = " << var_float << endl; //Data Type float:var_float = 9.999000
cout << "sizeof(float) = " << sizeof(float) << endl; //sizeof(float) = 4---整形部分占用4个字节
cout << "FLT_DIG = " << FLT_DIG << endl; //FLT_DIG = 6
cout << "FLT_MANT_DIG = " << FLT_MANT_DIG << endl; //FLT_MANT_DIG = 24
cout << "FLT_MAX_10_EXP = " << FLT_MAX_10_EXP << endl; //FLT_MAX_10_EXP = 38
cout << "FLT_MIN_10_EXP = " << FLT_MIN_10_EXP << endl; //FLT_MIN_10_EXP = -37
cout << "double类型******************************************************************" << endl;
double var_double = 9.999;
cout << "Data Type double:var_double = " << var_double << endl; //Data Type double:var_double = 9.999000
cout << "sizeof(double) = " << sizeof(double) << endl; //sizeof(double) = 8---整形部分占用8个字节
cout << "DBL_DIG = " << DBL_DIG << endl; //DBL_DIG = 15
cout << "DBL_MANT_DIG = " << DBL_MANT_DIG << endl; //DBL_MANT_DIG = 53
cout << "DBL_MAX_10_EXP = " << DBL_MAX_10_EXP << endl; //DBL_MAX_10_EXP = 308
cout << "DBL_MIN_10_EXP = " << DBL_MIN_10_EXP << endl; //DBL_MIN_10_EXP = -307
cout << "long double类型******************************************************************" << endl;
long double var_long_double = 9.999;
cout << "Data Type long double:var_long_double = " << var_long_double << endl; //Data Type long double:var_long_double = 9.999000
cout << "sizeof(long double) = " << sizeof(long double) << endl; //sizeof(long double) = 8---整形部分占用8个字节
cout << "LDBL_DIG = " << LDBL_DIG << endl; //LDBL_DIG = 15
cout << "LDBL_MANT_DIG = " << LDBL_MANT_DIG << endl; //LDBL_MANT_DIG = 53
cout << "LDBL_MAX_10_EXP = " << LDBL_MAX_10_EXP << endl; //LDBL_MAX_10_EXP = 308
cout << "LDBL_MIN_10_EXP = " << LDBL_MIN_10_EXP << endl; //LDBL_MIN_10_EXP = -307
cout << "进制******************************************************************" << endl;
int var_int_data = 2147483647;
cout << "var_int_data = " << (bitset<32>) var_int_data << "(binary for 2147483647)"<<endl;//var_int_data = 01111111111111111111111111111111(binary for 2147483647)
cout << oct;//设置cout为八进制输出,下次需要输出不同进制时要重新设置为相应的模式
cout << "var_int_data = " << var_int_data << "(octal for 2147483647)"<<endl;//var_int_data = 17777777777(octal for 2147483647)
cout << dec;//设置cout为十进制输出,下次需要输出不同进制时要重新设置为相应的模式
cout << "var_int_data = " << var_int_data << "(decimal for 2147483647)" << endl;//var_int_data = 2147483647(decimal for 2147483647)
cout << hex;//设置cout为十六进制输出,下次需要输出不同进制时要重新设置为相应的模式
cout << "var_int_data = " << var_int_data << "(hex for 2147483647)" << endl;//var_int_data = 7fffffff(hex for 2147483647)
cout << dec;
cout << "sizeof*****************************************************************" << endl;
int var = 10;
int vars[4] = {1,2,3,4};
cout << "sizeof(var) = " << sizeof(var) << endl;//sizeof(var) = 4
cout << "sizeof(vars) = " << sizeof(vars) << endl;//sizeof(vars) = 16
cout << "sizeof(vars[1]) = " << sizeof(vars[1]) << endl;//sizeof(vars[1]) = 4
cout << "auto********************************************************************" << endl;
cout.setf(ios_base::fixed, ios_base::floatfield);
auto n = 100;//n是int类型
cout << "n = " << n << endl;
auto x = 1.5;//x是double类型
cout << "x = " << x << endl;
auto y = 1.3e12L;//y是long double类型
cout << "y = " << y << endl;
double x;
int n;
decltype(x* n) q; // q same type as x*n, i.e., double
decltype(&x) pd; // pd same as &x, i.e., double *
int j = 3;
int& k = j;
const int& n = j;
decltype(n) i1 = j; // i1 type const int &
decltype(j) i2 = j; // i2 type int
decltype((j)) i3 = j; // i3 type int &
decltype(k + 1) i4 = j; // i4 type int
return 0;
}
运行结果:
自动类型转换*********************************************************
初始化和赋值进行的转换(传统=赋值)************************************
(1)******************************************************************
var_double = 9.897654
var_float = 9.897655
(2)******************************************************************
var_double1 = 9.897654
var_int = 9
(3)******************************************************************
var_long = 2147483647
var_short = -1
(4)******************************************************************
var_bool = true
var_bool = false
以{}方式初始化时进行的转换(C++11允许)************************************
(1)******************************************************************
var_long1 = 2147483647
(2)******************************************************************
var_double2 = 9.897654
表达式中的转换************************************
var_double3 + var_long_double = 999.998000
D:\Prj\_C++Self\_3Typecast\Debug\_3Typecast.exe (进程 14084)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
此为本人读C++ Primer总结的笔记,如有错误或知识缺口,请在评论区告知。如本文有在实践中帮到您,是本人的荣幸。