本文最后更新于 2025年7月18日 晚上
学C++的时候,数据类型不仅是重要的,而且也是很基础的。
基本数据类型(Primitive Types)
整型(Integer Types)
long long:存储整数值(C++11),超长整型。
1
| long long d = 1234567890123456L;
|
浮点型(Floating-Point Types)
long double:存储小数值(C++11),长双精度浮点型。
1
| long double h = 3.14159265358979323846;
|
字符型(Character Types)
char:单字符(1 字节),通常用于 ASCII 字符。
wchar_t:宽字符(2 或 4 字节),用于 Unicode 字符(如 UTF-16)。
char16_t(C++11)、char32_t(C++11)Unicode 字符类型(UTF-16 和 UTF-32)。
1 2
| char16_t u16ch = u'你'; char32_t u32ch = U'你';
|
布尔型(Boolean Type)
无类型(Void)
1 2
| void printMessage() { } void* ptr = nullptr;
|
派生数据类型(Derived Types)
指针(Pointer)
指向内存地址的类型。
1 2
| int x = 10; int* ptr = &x;
|
引用(Reference)
变量的别名(必须初始化且不能更改绑定)。
1 2
| int y = 20; int& ref = y;
|
数组(Array)
同一类型元素的连续集合。
1
| int arr[5] = {1, 2, 3, 4, 5};
|
函数(Function)
函数类型,可指向函数的指针。
1 2
| int add(int a, int b) { return a + b; } int (*funcPtr)(int, int) = add;
|
用户自定义类型(User-Defined Types)
结构体(struct)
组合多个不同类型的数据成员。
1 2 3 4 5
| struct Point { int x; int y; }; Point p = {10, 20};
|
类(class)
封装数据和方法的类型(支持面向对象编程)。
1 2 3 4 5 6 7
| class Rectangle { private: int width, height; public: Rectangle(int w, int h) : width(w), height(h) {} int area() { return width * height; } };
|
枚举(enum)
命名整型常量集合。
1 2
| enum Color { Red, Green, Blue }; Color c = Red;
|
1 2
| enum class Direction { Up, Down, Left, Right }; Direction d = Direction::Up;
|
联合体(union)
共享内存空间的类型(同一时间只能存储一个成员)。
1 2 3 4 5 6
| union Data { int i; float f; }; Data data; data.i = 42;
|
类型别名(typedef 或 using)
为现有类型定义新名称。
1 2
| typedef int Integer; using String = std::string;
|
其他重要类型
标准库类型
1
| std::string s = "Hello, C++";
|
1
| std::vector<int> vec = {1, 2, 3};
|
模板类型(Template Types)
泛型编程的基础。
1 2 3 4
| template <typename T> T max(T a, T b) { return (a > b) ? a : b; }
|
类型修饰符(Type Modifiers)
volatile防止编译器优化(用于硬件访问或多线程)。
1
| volatile int sensorValue;
|
mutable允许在 const 成员函数中修改成员变量。
1 2 3 4 5
| class Test { mutable int counter; public: void increment() const { counter++; } };
|
类型转换(Type Conversion)
隐式转换(Implicit)
编译器自动完成的转换。
显式转换(Explicit)
1 2
| double d = 3.14; int a = (int)d;
|
1 2 3 4
| int b = static_cast<int>(d); dynamic_cast<Derived*>(basePtr); const_cast<int&>(constVar); reinterpret_cast<int*>(p);
|
C++11/17/20 新增类型特性
nullptr(C++11)空指针常量(替代 NULL)。
char8_t(C++20)用于 UTF-8 字符。
1
| char8_t utf8_char = u8'a';
|
总结
| 类型 |
典型大小 |
描述 |
| bool |
1 字节 |
true 或 false |
| char |
1 字节 |
signed char:-128 到 127 unsigned char:0 到 255 |
| short |
2 字节 |
-32768 到 32767 |
| int |
4 字节 |
-2^31 到 2^31-1 |
| long |
4/8 字节 |
大小与平台相关(32位系统为 4 字节,64位系统为 8 字节) |
| long long |
8 字节 |
-2^63 到 2^63-1 |
| float |
4 字节 |
约 ±3.4e±38(6-7 位有效数字) |
| double |
8 字节 |
约 ±1.7e±308(15 位有效数字) |
| long double |
8/16 字节 |
与平台相关,精度更高 |
| char8_t(C++20) |
1 字节 |
UTF-8 字符 |
| wchar_t(C++11) |
2/4 字节 |
与平台相关,Unicode 字符 |
| char16_t(C++11) |
2 字节 |
UTF-16 字符 |
| char32_t(C++11) |
4 字节 |
UTF-32 字符 |
| void |
无类型 |
无类型 |
| nullptr(C++11) |
4/8字节 |
空指针常量(与平台相关) |
| const |
类型修饰符 |
用于声明常量 |
| volatile |
类型修饰符 |
用于声明易变的变量 |
| mutable |
类型修饰符 |
用于声明类中的可变成员 |
| auto(C++11) |
类型推导关键字 |
用于自动推导变量的类型 |
| decltype(C++11) |
关键字 |
用于获取表达式的类型 |
| 函数指针 |
4/8 字节 |
指向函数的指针(与平台相关) |
| 结构体 |
取决于成员 |
组合多个不同类型的数据成员,大小取决于成员变量和内存对齐 |
| 类(C++) |
取决于成员 |
封装数据和方法的类型,大小取决于成员变量和内存对齐 |
| 枚举(C++) |
取决于底层类型 |
命名整型常量集合,默认底层类型为 int |
| 联合体(C++) |
等于最大成员的大小 |
共享内存空间的类型 |
| 类型别名(C++) |
与原类型相同 |
为现有类型定义新名称 |