C++数据类型
C++数据类型
- 内置基本数据类型
- 数值类型
- C++11 新增类型
- C++空指针表示
- std::initializer_list使用
- std::tuple使用
- 其他类型
- 数组使用
- 类型别名
- 类型转换
- 1、静态转换
- 2、动态转换
- 3、常量转换
- 4、重新解释转换
内置基本数据类型
类型 | 关键字 |
---|---|
布尔型 | bool |
字符型 | char |
整型 | int |
浮点型 | float |
双浮点型 | double |
无类型 | void |
宽字符型 | wchar_t |
1、整形还区分有符号、无符号;短整型,长整型
2、字符型区分有符号、无符号
数值类型
类型 | 描述 | 所占字节数 | 最大值 | 最小值 |
---|---|---|---|---|
bool | 表示真或假(true、false) | 1 | 1 | 0 |
char | 字符类型,通常用于存储ASCII字符 | 1 | 127 | -128 |
signed char | 有符号字符类型(-128 到 127) | 1 | 127 | -128 |
unsigned char | 无符号字符类型(0 到 255) | 1 | 255 | 0 |
wchar_t | 宽字符类型,用于存储 Unicode 字符 | 2 | 65535 | 0 |
char16_t | 16 位 Unicode 字符类型(C++11 引入) | 2 | 65535 | 0 |
char32_t | 32 位 Unicode 字符类型(C++11 引入) | 4 | 4294967295 | 0 |
short | 短整型(-32768 到 32767) | 2 | 32767 | -32768 |
signed short | 有符号短整型(-32768 到 32767) | 2 | 32767 | -32768 |
unsigned short | 无符号短整型(0 到 65535) | 2 | 65535 | 0 |
int | 整型(-2147483648 到 2147483647) | 4 | 2147483647 | -2147483648 |
signed int | 有符号整型(-2147483648 到 2147483647) | 4 | 2147483647 | -2147483648 |
unsigned int | 无符号整型(0 到 4294967295) | 4 | 4294967295 | 0 |
long | 长整型(4或8取决于平台) | 4 | 2147483647 | -2147483648 |
signed long | 有符号长整型(4或8取决于平台) | 4 | 2147483647 | -2147483648 |
unsigned long | 无符号长整型(4或8取决于平台) | 4 | 4294967295 | 0 |
long long | 长长整型(C++11 引入) | 8 | 9223372036854775807 | -9223372036854775808 |
signed long long | 有符号长长整型(C++11 引入) | 8 | 9223372036854775807 | -9223372036854775808 |
unsigned long long | 无符号长长整型(C++11 引入) | 8 | 18446744073709551615 | 0 |
float | 单精度浮点数(约 ±3.4e±38(6-7 位有效数字)) | 4 | 3.40282e+38 | 1.17549e-38 |
double | 双精度浮点数(约 ±1.7e±308(15 位有效数字)) | 8 | 1.79769e+308 | 2.22507e-308 |
long double | 扩展精度浮点数(8、12 或 16取决于平台) | 8 | 1.79769e+308 | 2.22507e-308 |
#include <iostream>
#include <limits>using namespace std;
int main()
{std::cout << "|类型|描述|所占字节数|最大值|最小值|" << endl;std::cout << "|---|---|---|---|---|" << endl;std::cout << "|bool|表示真或假(true、false)|" << sizeof(bool) << "|" << (numeric_limits<bool>::max)() << "|" << (numeric_limits<bool>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|char|字符类型,通常用于存储ASCII字符|" << sizeof(char) << "|" << (int)(numeric_limits<char>::max)() << "|" << (int)(numeric_limits<char>::min)() << "|" << endl;std::cout << "|signed char|有符号字符类型(-128 到 127)|" << sizeof(signed char) << "|" << (int)(numeric_limits<signed char>::max)() << "|" << (int)(numeric_limits<signed char>::min)() << "|" << endl;std::cout << "|unsigned char|无符号字符类型(0 到 255)|" << sizeof(unsigned char) << "|" << (int)(numeric_limits<unsigned char>::max)() << "|" << (int)(numeric_limits<unsigned char>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|wchar_t|宽字符类型,用于存储 Unicode 字符|" << sizeof(wchar_t) << "|" << (numeric_limits<wchar_t>::max)() << "|" << (numeric_limits<wchar_t>::min)() << "|" << endl;std::cout << "|char16_t|16 位 Unicode 字符类型(C++11 引入)|" << sizeof(char16_t) << "|" << (numeric_limits<char16_t>::max)() << "|" << (numeric_limits<char16_t>::min)() << "|" << endl;std::cout << "|char32_t|32 位 Unicode 字符类型(C++11 引入)|" << sizeof(char32_t) << "|" << (numeric_limits<char32_t>::max)() << "|" << (numeric_limits<char32_t>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|short|短整型(-32768 到 32767)|" << sizeof(short) << "|" << (numeric_limits<short>::max)() << "|" << (numeric_limits<short>::min)() << "|" << endl;std::cout << "|signed short|有符号短整型(-32768 到 32767)|" << sizeof(signed short) << "|" << (numeric_limits<signed short>::max)() << "|" << (numeric_limits<signed short>::min)() << "|" << endl;std::cout << "|unsigned short|无符号短整型(0 到 65535)|" << sizeof(unsigned short) << "|" << (numeric_limits<unsigned short>::max)() << "|" << (numeric_limits<unsigned short>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|int|整型(-2147483648 到 2147483647)|" << sizeof(int) << "|" << (numeric_limits<int>::max)() << "|" << (numeric_limits<int>::min)() << "|" << endl;std::cout << "|signed int|有符号整型(-2147483648 到 2147483647)|" << sizeof(signed int) << "|" << (numeric_limits<signed int>::max)() << "|" << (numeric_limits<signed int>::min)() << "|" << endl;std::cout << "|unsigned int|无符号整型(0 到 4294967295)|" << sizeof(unsigned int) << "|" << (numeric_limits<unsigned int>::max)() << "|" << (numeric_limits<unsigned int>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|long|长整型(4或8取决于平台)|" << sizeof(long) << "|" << (numeric_limits<long>::max)() << "|" << (numeric_limits<long>::min)() << "|" << endl;std::cout << "|signed long|有符号长整型(4或8取决于平台)|" << sizeof(signed long) << "|" << (numeric_limits<signed long>::max)() << "|" << (numeric_limits<signed long>::min)() << "|" << endl;std::cout << "|unsigned long|无符号长整型(4或8取决于平台)|" << sizeof(unsigned long) << "|" << (numeric_limits<unsigned long>::max)() << "|" << (numeric_limits<unsigned long>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|long long|长长整型(C++11 引入)|" << sizeof(long long) << "|" << (numeric_limits<long long>::max)() << "|" << (numeric_limits<long long>::min)() << "|" << endl;std::cout << "|signed long long|有符号长长整型(C++11 引入)|" << sizeof(signed long long) << "|" << (numeric_limits<signed long long>::max)() << "|" << (numeric_limits<signed long long>::min)() << "|" << endl;std::cout << "|unsigned long long|无符号长长整型(C++11 引入)|" << sizeof(unsigned long long) << "|" << (numeric_limits<unsigned long long>::max)() << "|" << (numeric_limits<unsigned long long>::min)() << "|" << endl;std::cout << "||||||" << endl;std::cout << "|float|单精度浮点数(约 ±3.4e±38(6-7 位有效数字))|" << sizeof(float) << "|" << (numeric_limits<float>::max)() << "|" << (numeric_limits<float>::min)() << "|" << endl;std::cout << "|double|双精度浮点数(约 ±1.7e±308(15 位有效数字))|" << sizeof(double) << "|" << (numeric_limits<double>::max)() << "|" << (numeric_limits<double>::min)() << "|" << endl;std::cout << "|long double|扩展精度浮点数(8、12 或 16取决于平台)|" << sizeof(long double) << "|" << (numeric_limits<long double>::max)() << "|" << (numeric_limits<long double>::min)() << "|" << endl;
}
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'); |
C++空指针表示
- 1、NULL
- 本质是宏定义,通常被替换为0或(void*)0
- 属于C语言遗留,在C++中可能引发类型安全问题
- 示例:
int* p = NULL;
- 2、0
- 字面量零,可隐式转换为指针类型
- 可能导致函数重载歧义(无法区分整型和指针重载)
- 示例:
int* p = 0;
- 3、nullptr (C++11引入)
- 真正的指针空值常量,类型为
std::nullptr_t
- 解决函数重载歧义问题
- 类型安全,不会隐式转换为整型
- 示例:
int* p = nullptr;
- 真正的指针空值常量,类型为
关键区别:
1、类型安全:nullptr
有独立类型,避免意外转换
2、重载解析:void func(int) 和 void func(int*)调用时,nullptr
会明确选择指针版本
3、代码清晰:语义上明确表示空指针而非零值
综上所诉,以后优先使用nullptr
std::initializer_list使用
- 1、只读特性
for (auto val : list) {std::cout << val << endl; }
- 2、轻量级
底层通常实现为数组的引用,不拷贝元素,性能高效
- 3、自动推导
// 整形 std::initializer_list<int> list1 = { 1, 2, 3 }; // 字符串 std::initializer_list<std::string> list2 = { "北京", "上海", "广州","深圳"};// 浮点型(自动推导) auto list3 = { 1.1, 2.2, 3.3 }; for (auto it : list3 ) {std::cout << it << endl; }
std::tuple使用
- 1、定义
// 直接初始化std::tuple<int, std::string, double> t1(1, "北京", 3.14);// 使用 make_tuple 自动推导类型auto t2 = std::make_tuple(2, "上海", 2.718);
- 2、访问
// 通过索引访问std::cout << std::get<0>(t1) << endl;// 解包到具体变量int id;double value;std::tie(id, std::ignore, value) = t1;
- 3、合并
auto merged = std::tuple_cat(t1, t2);std::cout << "merged[0]= " << std::get<0>(merged) << endl;std::cout << "merged[1]= " << std::get<1>(merged) << endl;std::cout << "merged[2]= " << std::get<2>(merged) << endl;
- 4、比较
bool is_less = (t1 < t2);std::cout << "t1 < t2 is " << is_less << endl;
其他类型
数据类型 | 描述 | 示例 |
---|---|---|
数组 | 相同类型元素的集合 | 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 }; 默认第一个元素是0 |
数组使用
- 1、定义数组,然后赋值
string a[10]; a[0] = "北京"; a[1] = "上海"; for (auto it : a) {std:cout << it << endl; }
- 2、定义并赋值(指定长度,必须大于等于实际长度)
string a[10] = { "北京" ,"上海" }; cout << "数组长度:" << a->length() << endl; for (auto it : a) {cout << it << endl; }
- 3、定义并赋值(不指定长度)
string a[] = { "北京" ,"上海" }; cout << "数组长度:" << a->length() << endl; for (auto it : a) {cout << it << endl; }
类型别名
别名 | 描述 | 示例 |
---|---|---|
typedef | 为现有类型定义别名 | typedef int MyInt; |
using | 为现有类型定义别名(C++11 引入) | using MyInt = int; |
类型转换
1、静态转换
int a = 10;
float b = static_cast<float>(a);
cout << "int -> float:" << b << endl;a = static_cast<int>(12.3);
cout << "double -> int:" << a << endl;
2、动态转换
1、通常用于父类、子类之间的转换
2、通常用于将一个基类指针或引用转换为派生类指针或引用
3、在运行时进行类型检查。如果转换失败,对于指针类型会返回 nullptr,对于引用类型则会抛出 std::bad_cast 异常
3、常量转换
1、用于将 const 类型的对象转换为非 const 类型的对象
2、只能用于转换掉 const 属性,不能改变对象的类型
const int a = 10;
int& b = const_cast<int&>(a);
cout << "常量转换:" << b << endl;
4、重新解释转换
1、将一个数据类型的值重新解释为另一个数据类型的值,通常用于在不同的数据类型之间进行转换
2、不进行任何类型检查
int a = 10;
float b = reinterpret_cast<float&>(a);
cout << "int -> float:" << b << endl;a = reinterpret_cast<int&>(b);
cout << "float - >int:" << a << endl;