基本的内置类型
C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:
| 类型 | 关键字 |
|---|---|
| 布尔型 | bool |
| 字符型 | char |
| 整型 | int |
| 浮点型 | float |
| 双浮点型 | double |
| 无类型 | void |
| 宽字符型 | wchar_t |
| 类型 | 描述 |
|---|---|
| bool | 布尔类型,存储值 true 或 false,占用 1 个字节。 |
| char | 字符类型,用于存储 ASCII 字符,通常占用 1 个字节。 |
| int | 整数类型,通常用于存储普通整数,通常占用 4 个字节。 |
| float | 单精度浮点值,用于存储单精度浮点数。单精度是这样的格式,1 位符号,8 位指数,23 位小数,通常占用4个字节。![]() |
| double | 双精度浮点值,用于存储双精度浮点数。双精度是 1 位符号,11 位指数,52 位小数,通常占用 8 个字节。![]() |
| void | 表示类型的缺失。 |
| wchar_t | 宽字符类型,用于存储更大范围的字符,通常占用 2 个或 4 个字节。 |
下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类
型的变量所能存储的最大值和最小值。
**注意:**不同系统会有所差异,一字节为 8 位。
**注意:**默认情况下,int、short、long都是带符号的,即 signed。
**注意:**long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。
| 类型 | 位 | 范围 |
|---|---|---|
| char | 1 个字节 | -128 到 127 或者 0 到 255 |
| unsigned char | 1 个字节 | 0 到 255 |
| signed char | 1 个字节 | -128 到 127 |
| int | 4 个字节 | -2147483648 到 2147483647 |
| unsigned int | 4 个字节 | 0 到 4294967295 |
| signed int | 4 个字节 | -2147483648 到 2147483647 |
| short int | 2 个字节 | -32768 到 32767 |
| unsigned short int | 2 个字节 | 0 到 65,535 |
| signed short int | 2 个字节 | -32768 到 32767 |
| long int | 8 个字节 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
| signed long int | 8 个字节 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
| unsigned long int | 8 个字节 | 0 到 18,446,744,073,709,551,615 |
| float | 4 个字节 | 精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字) |
| double | 8 个字节 | 双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字) |
| long long | 8 个字节 | 双精度型占8 个字节(64位)内存空间,表示 -9,223,372,036,854,775,807 到 9,223,372,036,854,775,807 的范围 |
| long double | 16 个字节 | 长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。 |
| wchar_t | 2 或 4 个字节 | 1 个宽字符 |
sizeof 用法
1#include<iostream>
2#include <limits>
3
4using namespace std;
5
6int main()
7{
8 cout << "type: \t\t" << "************size**************"<< endl;
9 cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
10 cout << "\t最大值:" << (numeric_limits<bool>::max)();
11 cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
12 cout << "char: \t\t" << "所占字节数:" << sizeof(char);
13 cout << "\t最大值:" << (numeric_limits<char>::max)();
14 cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
15 cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
16 cout << "\t最大值:" << (numeric_limits<signed char>::max)();
17 cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
18 cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
19 cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
20 cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
21 cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
22 cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
23 cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
24 cout << "short: \t\t" << "所占字节数:" << sizeof(short);
25 cout << "\t最大值:" << (numeric_limits<short>::max)();
26 cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
27 cout << "int: \t\t" << "所占字节数:" << sizeof(int);
28 cout << "\t最大值:" << (numeric_limits<int>::max)();
29 cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
30 cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
31 cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
32 cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
33 cout << "long: \t\t" << "所占字节数:" << sizeof(long);
34 cout << "\t最大值:" << (numeric_limits<long>::max)();
35 cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
36 cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
37 cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
38 cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
39 cout << "double: \t" << "所占字节数:" << sizeof(double);
40 cout << "\t最大值:" << (numeric_limits<double>::max)();
41 cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
42 cout << "long double: \t" << "所占字节数:" << sizeof(long double);
43 cout << "\t最大值:" << (numeric_limits<long double>::max)();
44 cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
45 cout << "float: \t\t" << "所占字节数:" << sizeof(float);
46 cout << "\t最大值:" << (numeric_limits<float>::max)();
47 cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
48 cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
49 cout << "\t最大值:" << (numeric_limits<size_t>::max)();
50 cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
51 cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
52 // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
53 cout << "type: \t\t" << "************size**************"<< endl;
54 return 0;
55}

typedef 声明
您可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:
typedef type newname;
派生数据类型
| 数据类型 | 描述 | 示例 |
|---|---|---|
数组 | 相同类型元素的集合 | int arr[5] = {1, 2, 3, 4, 5}; |
指针 | 存储变量内存地址的类型 | int* ptr = &x; |
引用 | 变量的别名 | int& ref = x; |
函数 | 函数类型,表示函数的签名 | int func(int a, int b); |
结构体 | 用户定义的数据类型,可以包含多个不同类型的成员 | struct Point { int x; int y; }; |
类 | 用户定义的数据类型,支持封装、继承和多态 | class MyClass { ... }; |
联合体 | 多个成员共享同一块内存 | union Data { int i; float f; }; |
枚举 | 用户定义的整数常量集合 | enum Color { RED, GREEN, BLUE }; |
类型别名
| 别名 | 描述 | 示例 |
|---|---|---|
typedef | 为现有类型定义别名 | typedef int MyInt; |
using | 为现有类型定义别名(C++11 引入) | using MyInt = int; |
标准库类型
| 数据类型 | 描述 | 示例 |
|---|---|---|
std::string | 字符串类型 | std::string s = "Hello"; |
std::vector | 动态数组 | std::vector<int> v = {1, 2, 3}; |
std::array | 固定大小数组(C++11 引入) | std::array<int, 3> a = {1, 2, 3}; |
std::pair | 存储两个值的容器 | std::pair<int, float> p(1, 2.0); |
std::map | 键值对容器 | std::map<int, std::string> m; |
std::set | 唯一值集合 | std::set<int> s = {1, 2, 3}; |
常见创建变量错误
写值时的单双引号

一个字符变量想写多个字符的方法


转义字符
| 转义序列 | 含义 |
|---|---|
\\ | \ 字符 |
\' | ’ 字符 |
\" | " 字符 |
\? | ? 字符 |
| \a | 警报铃声 |
| \b | 退格键 |
| \f | 换页符 |
| \n | 换行符 |
| \r | 回车 |
| \t | 水平制表符 占8个位置 |
| \v | 垂直制表符 |
| \ooo | 一到三位的八进制数 |
| \xhh . . . | 一个或多个数字的十六进制数 |
\t水平制表符的对齐效果

\v垂直制表符对齐效果

字符串型
- C风格型字符串:
char charname[] = "ABCDEF",注意是双引号。 - C++风格字符串:
string strname = "ABCDEF",注意是双引号。头文件要包含#include <string>

1#include<iostream>
2#include <limits>
3#include <string>
4
5using namespace std;
6
7int main()
8{
9 char ch1[] = "Hello World"; // C-style string
10 string str1 = "Hello C++"; // C++ string
11 cout << ch1 << endl;
12 cout << str1 << endl;
13 return 0;
14}
布尔类型
bool:表示布尔类型,只有 true(1) 和 false(0) 两个值。

1#include<iostream>
2
3using namespace std;
4
5int main()
6{
7 bool flag = true;
8 cout << flag << endl;
9 flag = 0;
10 cout << flag << endl;
11 cout << sizeof(flag) << endl;
12}
数据输入
从键盘获取输入: cin >> 变量

1#include<iostream>
2
3using namespace std;
4
5int main()
6{
7 int a = 10; //int是C++中的整型
8 cout << "a的值为" << a << endl;
9 cout << "输入a的新值" << endl;
10 cin >> a;
11 cout << "a的新值为" << a << endl;
12
13 float b = 10.5; //float是C++中的浮点类型
14 cout << "b的值为" << b << endl;
15 cout << "输入b的新值" << endl;
16 cin >> b;
17 cout << "b的新值为" << b << endl;
18
19 char c = 'a'; //char是C++中的字符类型
20 cout << "c的值为" << c << endl;
21 cout << "输入c的新值" << endl;
22 cin >> c;
23 cout << "c的新值为" << c << endl;
24
25 bool d = true; //bool是C++中的布尔类型
26 cout << "d的值为" << d << endl;
27 cout << "输入d的新值" << endl;
28 cin >> d; //只要非0都是真
29 cout << "d的新值为" << d << endl;
30
31 string e = "Hello World"; //string是C++中的字符串类型
32 cout << "e的值为" << e << endl;
33 cout << "输入e的新值" << endl;
34 cin >> e;
35 cout << "e的新值为" << e << endl;
36
37 return 0;
38
39}

🔔 想要获取更多网络安全与编程技术干货? 关注 泷羽Sec-静安 公众号,与你一起探索前沿技术,分享实用的学习资源与工具。我们专注于深入分析,拒绝浮躁,只做最实用的技术分享!💻
扫描下方二维码,马上加入我们,共同成长!🌟
👉 长按或扫描二维码关注公众号

或者直接回复文章中的关键词,获取更多技术资料与书单推荐!📚



