3、C++数据类型

3、C++数据类型

C++ 提供了丰富的数据类型来满足不同编程需求,可以分为基本数据类型、复合数据类型和用户自定义类型三大类。

一、基本数据类型

1. 整数类型

类型 大小 (字节) 范围 (32位系统)
short 2 -32,768 到 32,767
int 4 -2,147,483,648 到 2,147,483,647
long 4 同 int
long long 8 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned short 2 0 到 65,535
unsigned int 4 0 到 4,294,967,295
unsigned long 4 同 unsigned int
unsigned long long 8 0 到 18,446,744,073,709,551,615

2. 浮点类型

类型 大小 (字节) 精度 范围
float 4 6-7位小数 ±3.4e-38 到 ±3.4e+38
double 8 15-16位小数 ±1.7e-308 到 ±1.7e+308
long double 16 18-19位小数 ±1.1e-4932 到 ±1.1e+4932

3. 字符类型

类型 大小 (字节) 描述
char 1 存储ASCII字符或小整数
wchar_t 2 或 4 宽字符,用于Unicode
char16_t 2 UTF-16字符
char32_t 4 UTF-32字符

4. 布尔类型

类型 大小 (字节)
bool 1 true/false

二、复合数据类型

1. 数组

// 一维数组
int numbers[5] = {1, 2, 3, 4, 5};

// 二维数组
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// C++11 初始化
double temps[] {10.5, 12.4, 9.8};

2. 指针

int var = 20;
int *ptr = &var;  // 指向var的指针

// 动态内存分配
int *arr = new int[10];
delete[] arr;  // 释放内存

3. 引用

int x = 10;
int &ref = x;  // ref是x的引用
ref = 20;      // 现在x的值也是20

4. 字符串

// C风格字符串
char str1[] = "Hello";

// C++ string类
#include <string>
std::string str2 = "World";

三、用户自定义类型

1. 结构体(struct)

struct Person {
    std::string name;
    int age;
    double height;
};

Person p1 {"Alice", 25, 1.68};

2. 类(class)

class Rectangle {
private:
    int width, height;
public:
    Rectangle(int w, int h) : width(w), height(h) {}
    int area() { return width * height; }
};

Rectangle rect(5, 3);

3. 枚举(enum)

// 传统枚举
enum Color { RED, GREEN, BLUE };
Color c = GREEN;

// 强类型枚举(C++11)
enum class Day { MON, TUE, WED };
Day d = Day::MON;

4. 联合体(union)

union Data {
    int i;
    float f;
    char str[20];
};

Data data;
data.i = 10;

四、类型修饰符

1. 符号修饰符

  • signed - 有符号数(默认)
  • unsigned - 无符号数

2. 大小修饰符

  • short - 短整型
  • long - 长整型

3. 类型限定符

限定符 描述
const 定义常量,值不可改变
volatile 告诉编译器不要优化该变量
mutable 允许const成员函数修改该成员

五、C++11新增类型

1. 自动类型推导(auto)

auto x = 5;        // int
auto y = 3.14;     // double
auto z = "hello";  // const char*

2. 声明类型(decltype)

int x = 5;
decltype(x) y = 10;  // y的类型与x相同(int)

3. 固定宽度整数类型

#include <cstdint>

int8_t small;    // 8位有符号整数
uint16_t medium; // 16位无符号整数
int32_t large;   // 32位有符号整数

六、类型转换

1. 隐式转换

int i = 3.14;  // 3.14被截断为3

2. 显式转换

// C风格转换
double d = (double)5 / 2;

// C++风格转换
double d2 = static_cast<double>(5) / 2;

3. C++四种强制类型转换

转换类型 用途
static_cast 基本类型转换,类层次间的上行转换
dynamic_cast 类层次间的下行转换,运行时检查
const_cast 移除const/volatile属性
reinterpret_cast 低级别重新解释,不安全的转换

七、类型别名

1. typedef

typedef unsigned long ulong;
ulong distance = 1000;

2. using (C++11)

using StringVector = std::vector<std::string>;
StringVector names;

八、类型大小和限制

#include <iostream>
#include <climits>
#include <cfloat>

int main() {
    std::cout << "int size: " << sizeof(int) << " bytes\n";
    std::cout << "int max: " << INT_MAX << "\n";
    std::cout << "float max: " << FLT_MAX << "\n";
    return 0;
}

C++的数据类型系统既丰富又灵活,理解这些类型及其特性对于编写高效、安全的代码至关重要。

posted @ 2025-06-27 20:03  站着说话不腰疼  阅读(134)  评论(0)    收藏  举报