当前位置: 首页 > news >正文

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)110
char字符类型,通常用于存储ASCII字符1127-128
signed char有符号字符类型(-128 到 127)1127-128
unsigned char无符号字符类型(0 到 255)12550
wchar_t宽字符类型,用于存储 Unicode 字符2655350
char16_t16 位 Unicode 字符类型(C++11 引入)2655350
char32_t32 位 Unicode 字符类型(C++11 引入)442949672950
short短整型(-32768 到 32767)232767-32768
signed short有符号短整型(-32768 到 32767)232767-32768
unsigned short无符号短整型(0 到 65535)2655350
int整型(-2147483648 到 2147483647)42147483647-2147483648
signed int有符号整型(-2147483648 到 2147483647)42147483647-2147483648
unsigned int无符号整型(0 到 4294967295)442949672950
long长整型(4或8取决于平台)42147483647-2147483648
signed long有符号长整型(4或8取决于平台)42147483647-2147483648
unsigned long无符号长整型(4或8取决于平台)442949672950
long long长长整型(C++11 引入)89223372036854775807-9223372036854775808
signed long long有符号长长整型(C++11 引入)89223372036854775807-9223372036854775808
unsigned long long无符号长长整型(C++11 引入)8184467440737095516150
float单精度浮点数(约 ±3.4e±38(6-7 位有效数字))43.40282e+381.17549e-38
double双精度浮点数(约 ±1.7e±308(15 位有效数字))81.79769e+3082.22507e-308
long double扩展精度浮点数(8、12 或 16取决于平台)81.79769e+3082.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;
http://www.dtcms.com/a/499543.html

相关文章:

  • FFmpeg 核心 API 系列:音频重采样 SwrContext 完全指南(新API版本)
  • 网站建设数据收集方法南昌网站推广¥做下拉去118cr
  • visio画网站开发类图深圳东道建设集团网站
  • 董付国老师Python小屋编程题答案161-170
  • 国外营销企业网站什么叫高端网站定制
  • Flutter---生命周期
  • 百度网址大全网站互联网家装
  • 专业的东莞网站排名WordPress多站点开启多语言
  • 微信端网站开发流程做网站什么配置够用
  • c# 泛型的详细介绍
  • OceanBase的SQL和执行计划监控视图
  • 网站原创内容优化wordpress 网站内跳转
  • 龙口市规划建设局网站南京app开发公司排名
  • 解决 Hugging Face 国内下载慢的问题:用 ModelScope 替代加速模型获取
  • 从基础到深入:自然语言处理核心技术全梳理(有 ML/DL 基础)
  • 合肥建设公司网站wordpress 个人电脑
  • 做网站需要哪些方面的支出新媒体运营需要学什么
  • 云手机群控是什么意思
  • 【ecfw】ecfw构建基础
  • 常州二建建设有限公司官方网站聊城做wap网站哪儿好
  • php做网站需要html国外设计公司名字
  • CUDA nvjpeg库编码jpeg图像
  • AI 工作流实战 - 调用豆包api实现批量生图
  • 如何编写您的第一个 Linux 设备驱动程序(一)
  • 做更好的自己 网站客户又找不到你
  • Spring MVC 封装全局统一异常处理
  • 海尔建设网站的内容wordpress设置教程
  • Flutter---EQ均衡器
  • 响应式食品企业网站网站的外链是什么
  • 【Protobuf】proto3语法详解1