文章目录
C++基本数据类型
七种C++基本数据类型
数据类型 | 关键字 |
---|---|
布尔型 | bool |
字符型 | char |
整型 | int |
浮点型 | float |
双浮点型 | float |
无类型 | void |
宽字符型 | wchar_t |
而wchar_t
是这样定义的:
typedef short int wchar_t;
数据之前的修饰符
在数据类型之前还可以用一些类型修饰符进行修饰,一些修饰符可以叠加修饰:
修饰符 | 描述 | 示例 |
---|---|---|
signed | 表示有符号类型(默认) | signed int x = -10; |
unsigned | 表示无符号类型 | unsigned int y = 10; |
short | 表示短整型 | short int z = 100; |
long | 表示长整型 | long int a = 10000; |
const | 表示常量,值不可修改 | const int b = 5; |
volatile | 表示变量可能被意外修改,禁止编译器优化 | volatile int c = 10; |
mutable | 表示类成员可以在const对象中修改 | mutable int counter; |
short就等于short int;long就等于long int。此外还有long long或者long long int。
数据所占的内存大小
不同的变量类型在内存中占用不同的空间大小,而且最大值与最小值也不相同。对于不同的系统,占用内存大小和取值范围可能有所不同。常见的一些数据类型列在表中,可以使用
cout << sizeof(int) << endl;
来查看本机器各种数据类型所占的空间大小。
类型 | 占用字节数(1Byte=8bit) | 取值范围 |
---|---|---|
bool布尔类型 | 1 | true或false |
char字符类型 | 1 | -128到127或0到255 |
int整型 | 4 | -2147483648到2147483647 |
long长整型 | 4或8 | 取决于平台 |
short短整型 | 2 | -32768到32767 |
float单精度浮点数 | 4 | 约±3.4e±38(6-7位有效数字) |
double双精度浮点数 | 8 | 约±1.7e±308(15位有效数字) |
C++11新增特性
C++11的新增特性有:
数据类型 | 描述 | 示例 |
---|---|---|
auto | 自动类型推断 | auto x = 10; |
decltype | 获取表达式的类型 | decltype(x) y = 20; |
nullptr | 空指针常量 | int* ptr = nullptr; |
std::initializer_list | 初始化列表类型 | std::initializer_list<int> list = {1, 2, 3}; |
std::tuple | 元组类型,可以储存多个不同类型的值 | std::tuple<int, float, char> t(1, 2.0, 'a'); |
派生数据类型
一些基础的派生数据类型
数据类型 | 描述 | 示例 |
---|---|---|
数组 | 相同类型元素的集合 | 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}; |
STL标准库类型
数据类型 | 描述 | 示例 |
---|---|---|
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}; |
类型别名
别名 | 描述 | 示例 |
---|---|---|
typedef | 为现有类型定义别名 | typedef int MyInt; |
using | 为现有类型定义别名(C++11引入) | using MyInt = int; |
类型转换
类型转换是将一个数据类型的值转换为另一种数据类型的值。C++有四种类型转换:静态转换、动态转换、常量转换和重新解释转换。
静态转换
静态转换是将一种数据类型的值强制转换为另一种数据类型的值,常用在比较相似的类型之间的转换,例如float和int之间的转换。
静态转换不进行任何运行时类型检查,可能导致运行时错误。例:
int i = 10;
float f = static_cast<float>(i); // 静态将int类型转换为float类型
动态转换
动态转换(dynamic_cast)是C++中用于在继承层次结构中进行向下转换(downcasting)的一种机制。
动态转换通常用于将一个基类指针或引用转换为派生类指针或引用。
动态转换在运行时进行类型检查。如果转换失败,对于指针类型会返回nullptr,对于引用类型则会抛出std::bad_cast异常。
语法:
dynamic_cast<目标类型>(表达式)
//目标类型:必须是指针或者引用类型
//表达式:需要转换的基类指针或引用
相关内容可以在面向对象程序设计中查阅。
常量转换
常量转换用于将const类型的对象转换为非const类型的对象,它只能转换掉const属性,不能改变对象的类型。例:
const int i = 10;
int& r = const_cast<int&>(i);
重新解释转换
重新解释转换将一个数据类型的值重新解释为另一个数据类型的值,通常用于在不同的数据类型之间进行转换。
重新解释转换不进行任何类型检查,因此可能会导致未定义的行为。例:
int i = 10;
float f = reinterpret_cast<float&>(i);