C++ 内置类型

面向对象编程本质是设计并扩展自己的数据类型。创建自己的数组类型之前,需要了解并理解C++的内置类型,因为这些类型是创建自己类型的基本组件。

内置C++类型分为两组:基本类型和复合类型。

基本类型——整数和浮点数

类型:short、int、long、long long及其无符号类型。

short至少16位

int至少与short一样长

long至少32位,且至少与int一样长

long long至少64位,且至少与long一样长

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
38
39
#include <iostream>
#include <climits>

using namespace std;

int main()
{
char charA = 0;
short shortA = 0;
int intA = 0;
long longA = 0;
long long longLongA = 0;

cout << "char所占字节数为:" << sizeof(charA) << endl;
cout << "short所占字节数为:" << sizeof(shortA) << endl;
cout << "int所占字节数为:" << sizeof(intA) << endl;
cout << "long所占字节数为:" << sizeof(longA) << endl;
cout << "long long所占字节数为:" << sizeof(longLongA) << endl;
cout << "--------------------------" << endl;
cout << "CHAR_MAX:" << CHAR_MAX << endl;
cout << "SHRT_MAX:" << SHRT_MAX << endl;
cout << "INT_MAX:" << INT_MAX << endl;
cout << "LONG_MAX:" << LONG_MAX << endl;
cout << "LLONG_MAX:" << LLONG_MAX << endl;
cout << "--------------------------" << endl;
cout << "UCHAR_MAX:" << UCHAR_MAX << endl;
cout << "USHRT_MAX:" << USHRT_MAX << endl;
cout << "UINT_MAX:" << UINT_MAX << endl;
cout << "ULONG_MAX:" << ULONG_MAX << endl;
cout << "ULLONG_MAX:" << ULLONG_MAX << endl;
cout << "--------------------------" << endl;
cout << "CHAR_MIN:" << CHAR_MIN << endl;
cout << "SHRT_MIN:" << SHRT_MIN << endl;
cout << "INT_MIN:" << INT_MIN << endl;
cout << "LONG_MIN:" << LONG_MIN << endl;
cout << "LLONG_MIN:" << LLONG_MIN << endl;

return 0;
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char所占字节数为:1
short所占字节数为:2
int所占字节数为:4
long所占字节数为:8
long long所占字节数为:8
--------------------------
CHAR_MAX:127
SHRT_MAX:32767
INT_MAX:2147483647
LONG_MAX:9223372036854775807
LLONG_MAX:9223372036854775807
--------------------------
UCHAR_MAX:255
USHRT_MAX:65535
UINT_MAX:4294967295
ULONG_MAX:18446744073709551615
ULLONG_MAX:18446744073709551615
--------------------------
CHAR_MIN:-128
SHRT_MIN:-32768
INT_MIN:-2147483648
LONG_MIN:-9223372036854775808
LLONG_MIN:-9223372036854775808

可以看到,在64位Windows 10中,longlong long长度相同。

climits中包含语句:*#define* CHAR_BIT __CHAR_BIT__

#defineC中一种定义常量的方式,比如定义pi=3.14。但是在C++中有更好的创建符号常量的方法,即const。但是如果希望自己的头文件能够被CC++同时使用,那就要使用#define

初始化变量的四种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
int a;
a = 0;
cout << a << endl;
int b = 0;
cout << b << endl;
int c{0};
cout << c << endl;
int d = {0};
cout << d << endl;
return 0;
}

变量范围溢出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <climits>

using namespace std;

int main()
{
int int_a = INT_MAX;
cout << "int_a:" << int_a << endl;
cout << "int_a + 1:" << int_a + 1 << endl;
cout << "-------------------" << endl;
unsigned int u_a = UINT_MAX;
cout << "u_a:" << u_a << endl;
cout << "u_a + 1:" << u_a + 1 << endl;

return 0;
}

结果:

1
2
3
4
5
int_a:2147483647
int_a + 1:-2147483648
-------------------
u_a:4294967295
u_a + 1:0

这是一个环。

不同进制表达数字

如果第1位是1~9,则为十进制

如果第1位是0,第二位为1~7,则为八进制。

如果前两位是0x0X,则为十六进制。

显式控制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
int a_octal = 100; //八进制
int a_decimal = 100; //十进制
int a_hexadecimal = 100; //十六进制

cout << oct;
cout << "八进制:" << a_decimal << endl;
cout << dec;
cout << "十进制:" << a_octal << endl;
cout << hex;
cout << "十六进制:" << a_hexadecimal << endl;
return 0;
}

输出:

1
2
3
八进制:100
十进制:144
十六进制:64

除非有理由存储为其他类型,否则存储为int。(使用能存的下的最小以下类型:int,long,long long)

“理由”:数字后指定类型后缀、值太大,int存储不下。

十六进制数(表示地址)会被表示为unsigned int而不是long

char类型

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
char a;
cout << "请输入a的值:_\b";
cin >> a;
cout << "您输入的值为:" << a << endl;
return 0;
}

结果:

1
2
请输入a的值:M
您输入的值为:M

输入了M,保存的是其编码77。输出的不是编码77,而是M,这些都是因为cincout的原因。

C++对字符使用单引号,对字符串使用双引号。

使用cout.put()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
char a;
cout << "请输入a的值:_\b";
cin >> a;
a++;
cout << "您输入的值为:";
cout.put(a);
cout << endl;
cout << "您输入的值为:" << a << endl;
return 0;
}

输出:

1
2
3
请输入a的值:A
您输入的值为:B
您输入的值为:B

cout<<cout.put()功能一模一样?为什么还需要cout.put()

原因:C++的release 2.0版本以前,cout把字符变量显示为字符(P42

char默认既不是有符号也不是无符号,而是由C++实现决定。

如果字符被用于数值类型,那么可以显式声明unsigned char或者signed char。

bool类型

非零为true,零为false

字面值为true和false

const限定符

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <climits>

using namespace std;

int main()
{
const short Months = 12;
// Months = 13; //将报错
return 0;
}

const值和#define相比,const可以明确指定类型,其次const有作用域规则。在C++中可以使用const值声明数组长度。

浮点类型

float至少32位,double至少48位,且不少于float,long double至少和double一样多。

三种类型的有效位数可以一样多。通常float为32位,double为64位,long double为80、96、128位。3种类型的指数范围至少为-37~37。

cfloat.h中能找到限制。

默认情况下,8.24或是2.4E8都会被存储为double类型。如果希望是float类型,需要后缀f或者F。如果希望是long double类型,需要后缀L或者l。

浮点数能够表示整数之间的值,能够表示的范围更大。运算速度比整数慢,精度低。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <float.h>

using namespace std;

int main()
{
cout.setf(ios_base::fixed, ios_base::floatfield);
float x = 20.0 / 0.3;
double y = 20.0 / 0.3;
const float million = 1E6;
cout << "乘以 10 的 6 次方:" << x * million << endl;
cout << "乘以 10 的 6 次方:" << y * million << endl;

cout << "float 的有效位数:" << FLT_DIG << endl;
cout << "float 尾数的位数:" << FLT_MANT_DIG << endl;
cout << "double 的有效位数:" << DBL_DIG << endl;
float a = 2.34E22f;
cout << a << endl;
float b = a + 1.0f;
cout << a << endl;
cout << b - a << endl;
return 0;
}

输出:

1
2
3
4
5
6
7
8
乘以 10 的 6 次方:66666664.000000
乘以 10 的 6 次方:66666666.666667
float 的有效位数:6
float 尾数的位数:24
double 的有效位数:15
23400001102275227418624.000000
23400001102275227418624.000000
0.000000

运算符

1
int dues = 20 * 5 + 24 * 6;

先计算哪个乘法?事实上,C++把这个问题留给了实现,让它来决定在系统中的最佳顺序。对这个例子来说,两种顺序结果相同,但是也有两种顺序不同的例子。(第5张递增运算符)

/对于不同类型的数字执行不同的操作,整数除以整数,执行int除法;long除以long执行long除法。因此这是一个运算符重载的例子。

类型转换

列表初始化

整型提升

强制类型转换

1
2
3
// thorn 为 int 类型
(long) thorn; // 来自 C语言
long (thorn) // 来自 C++,函数调用

四个强制类型转换符static_cast<typeName>(value)

auto 声明

自动推断类型,但是并非简单用于初始化简单变量。处理复杂类型,如标准模块库(STL)中的类型时,自动类型推断的优势才能显现出来。

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×