卓姆比尼人免安装绿色版
637M · 2025-09-26
基本数据类型:
作用:整型变量表示的是==整数类型==的数据
这些整型数据类型可以使用带符号(可以表示正数和负数)或无符号(仅表示非负数)修饰符进行声明。
有符号类型可以存储负数、正数和零,其中最高位用于表示符号(0 表示正数,1 表示负数)。
在 C++ 中,默认情况下,整数类型是有符号的,除非显式地使用 unsigned 关键字声明为无符号类型。
C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同:
数据类型 | 占用空间 | 取值范围 |
---|---|---|
short(短整型) | 2字节 | (-2^15 ~ 2^15-1) |
int(整型) | 4字节 | (-2^31 ~ 2^31-1) |
long(长整形) | Windows为4字节,Linux为4字节(32位),8字节(64位) | (-2^31 ~ 2^31-1) |
long long(长长整形) | 8字节 | (-2^63 ~ 2^63-1) |
无符号类型是指只能表示非负数(包括零)的数据类型。例如,unsigned int表示无符号整数。
此外,C++还提供了其他整型数据类型,如signed、unsigned short、unsigned long等,它们提供了不同的位数和范围,以满足不同的需求。
数据类型 | 占用空间(字节) | 取值范围(最小值到最大值) |
---|---|---|
unsigned char | 1 | 0 到 255 |
unsigned short | 2 | 0 到 65,535 |
unsigned int | 4 | 0 到 4,294,967,295 |
unsigned long | 4 或 8 | 0 到 4,294,967,295(4 字节) |
0 到 18,446,744,073,709,551,615(8 字节) | ||
unsigned long long | 8 | 0 到 18,446,744,073,709,551,615 |
无符号整数类型的特点
C++11 引入了 固定宽度的整数类型,这些类型定义在 <cstdint>
头文件中。它们提供了明确大小的整数类型,方便跨平台开发时确保数据大小的一致性。
数据类型 | 占用空间(字节) | 取值范围(有符号) | 取值范围(无符号) |
---|---|---|---|
int8_t | 1 | -128 到 127 | - |
uint8_t | 1 | - | 0 到 255 |
int16_t | 2 | -32,768 到 32,767 | - |
uint16_t | 2 | - | 0 到 65,535 |
int32_t | 4 | -2,147,483,648 到 2,147,483,647 | - |
uint32_t | 4 | - | 0 到 4,294,967,295 |
int64_t | 8 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | - |
uint64_t | 8 | - | 0 到 18,446,744,073,709,551,615 |
固定宽度整数类型的特点
int32_t
始终是 4 字节。int8_t
),则编译器可能不会定义该类型。示例代码
#include <iostream>
#include <cstdint> // 包含固定宽度整数类型
using namespace std;
int main() {
// 固定宽度有符号整数类型
int8_t i8 = -128;
int16_t i16 = -32768;
int32_t i32 = -2147483648;
int64_t i64 = -9223372036854775807LL;
// 固定宽度无符号整数类型
uint8_t u8 = 255;
uint16_t u16 = 65535;
uint32_t u32 = 4294967295;
uint64_t u64 = 18446744073709551615ULL;
// 输出
cout << "int8_t: " << sizeof(i8) << " bytes, value: " << (int)i8 << endl;
cout << "int16_t: " << sizeof(i16) << " bytes, value: " << i16 << endl;
cout << "int32_t: " << sizeof(i32) << " bytes, value: " << i32 << endl;
cout << "int64_t: " << sizeof(i64) << " bytes, value: " << i64 << endl;
cout << "uint8_t: " << sizeof(u8) << " bytes, value: " << (int)u8 << endl;
cout << "uint16_t: " << sizeof(u16) << " bytes, value: " << u16 << endl;
cout << "uint32_t: " << sizeof(u32) << " bytes, value: " << u32 << endl;
cout << "uint64_t: " << sizeof(u64) << " bytes, value: " << u64 << endl;
return 0;
}
输出示例
int8_t: 1 bytes, value: -128
int16_t: 2 bytes, value: -32768
int32_t: 4 bytes, value: -2147483648
int64_t: 8 bytes, value: -9223372036854775807
uint8_t: 1 bytes, value: 255
uint16_t: 2 bytes, value: 65535
uint32_t: 4 bytes, value: 4294967295
uint64_t: 8 bytes, value: 18446744073709551615
使用场景
C++ 中的字符类型用于存储单个字符或小整数值。字符类型在 C++ 中非常重要,因为它们不仅用于表示字符,还可以用于处理 ASCII 值或进行位操作。
作用:字符型变量用于显示单个字符
数据类型 | 占用空间(字节) | 描述 |
---|---|---|
char | 1 | 默认字符类型,通常用于存储 ASCII 字符。可以是有符号或无符号,取决于平台。 |
signed char | 1 | 明确表示有符号的字符类型,取值范围:-128 到 127。 |
unsigned char | 1 | 明确表示无符号的字符类型,取值范围:0 到 255。 |
wchar_t | 2 或 4 | 宽字符类型,用于存储 Unicode 字符。大小取决于平台(Windows 通常为 2 字节,Linux 通常为 4 字节)。 |
char16_t | 2 | 用于存储 UTF-16 编码的字符(C++11 引入)。 |
char32_t | 4 | 用于存储 UTF-32 编码的字符(C++11 引入)。 |
语法:char ch = 'a';
示例:
#include <iostream>
using namespace std;
int main() {
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl;
//ch = "abcde"; //错误,不可以用双引号
//ch = 'abcde'; //错误,单引号内只能引用一个字符
cout << (int) ch << endl; //查看字符a对应的ASCII码
ch = 97; //可以直接用ASCII给字符型变量赋值
cout << ch << endl;
return 0;
}
//a
//1
//97
//a
char
类型:默认情况下,char
可以是有符号或无符号,具体取决于编译器和平台。 通常用于存储 ASCII 字符(0 到 127)。signed char
和 unsigned char
: 明确表示有符号或无符号的字符类型。常用于处理小整数值或进行位操作。wchar_t
、char16_t
和 char32_t
用于存储 Unicode 字符。wchar_t
的大小取决于平台,而 char16_t
和 char32_t
的大小是固定的。'A'
。宽字符字面量使用前缀 L
(wchar_t
)、u
(char16_t
)或 U
(char32_t
),例如 L'中'
、u'中'
、U'中'
。char
类型用于处理 ASCII 字符或字符串。宽字符类型用于处理 Unicode 字符。unsigned char
常用于位操作,因为它可以表示 0 到 255 的值。signed char
和 unsigned char
可以用于存储小整数值。wchar_t
、char16_t
和 char32_t
用于支持多语言字符集。char
的符号性:char
的符号性取决于平台,如果需要明确的有符号或无符号行为,请使用 signed char
或 unsigned char
。wchar_t
的大小因平台而异,而 char16_t
和 char32_t
的大小是固定的。L
、u
或 U
)。C++ 中的浮点类型用于表示实数(即带小数点的数字)。它们基于 IEEE 754 标准,提供了不同精度的浮点数表示。
作用:用于==表示小数==
浮点型变量分为两种:
两者的区别在于表示的有效数字范围不同。
数据类型 | 占用空间(字节) | 精度(有效数字) | 取值范围(近似) |
---|---|---|---|
float | 4 | 6-9 位 | ±1.18e-38 到 ±3.4e38 |
double | 8 | 15-17 位 | ±2.23e-308 到 ±1.8e308 |
long double | 8、12 或 16 | 18-21 位 | 取决于平台,通常与 double 相同或更高 |
float
提供单精度浮点数,适合对精度要求不高的场景。double
提供双精度浮点数,适合大多数科学计算和工程应用。long double
提供扩展精度,适合对精度要求极高的场景。double
类型。f
或 F
表示 float
类型,例如 3.14f
。l
或 L
表示 long double
类型,例如 3.14L
。double
是科学计算中最常用的浮点类型,提供了足够的精度和范围。float
常用于图形处理,因为其精度足够且占用内存较少。long double
或专门的库(如 decimal
)。double
或 long double
以确保精度。1e-9
)进行比较。long double
的大小和精度可能因平台而异。浮点数可以表示一些特殊值:
INFINITY
(正无穷)和 -INFINITY
(负无穷)。NaN
(Not a Number),表示无效的浮点数操作结果。#include <iostream>
#include <cmath> // 包含 INFINITY 和 NaN
using namespace std;
int main() {
double inf = INFINITY;
double nan = NAN;
cout << "Infinity: " << inf << endl;
cout << "NaN: " << nan << endl;
return 0;
}
输出:
Infinity: inf
NaN: nan
作用:布尔数据类型代表真或假的值
类型 | 大小(字节) | 值 |
---|---|---|
bool | 1 | true 或 false |
示例:
#include <iostream>
using namespace std;
int main() {
bool flag = true;
cout << flag << endl; // 1
flag = false;
cout << flag << endl; // 0
cout << "size of bool = " << sizeof(bool) << endl; //1
return 0;
}
//1
//0
//size of bool = 1
作用:给一段指定的内存空间起名,方便操作这段内存。
语法:数据类型 变量名 = 初始值;
示例:
#include <iostream>
using namespace std;
int main() {
//变量的定义
//语法:数据类型 变量名 = 初始值
int a = 10;
cout << "a = " << a << endl;
return 0;
}
作用:用于记录程序中不可更改的数据
C++定义常量两种方式
#define 常量名 常量值
==通常在文件上方定义==,表示一个常量
const 数据类型 常量名 = 常量值
==通常在变量定义前加关键字const==,修饰该变量为常量,不可修改
示例:
#include <iostream>
using namespace std;
//1.宏常量
#define day = 7;
int main() {
//cout << "一周里总共有 %d" << day << " 天" << endl;
//printf("一周里总共有:%dn",day);
//day = 8; //报错,宏常量不可以修改
//2、const修饰变量
const int month = 12;
cout << "一年里总共有 " << month << " 个月份" << endl;
//month = 24; //报错,常量是不可以修改的
return 0;
}
在 C++ 中,类型转换 是将一种数据类型转换为另一种数据类型的过程。C++ 提供了多种类型转换方式,包括隐式转换和显式转换。
显式转换又分为 C 风格转换和 C++ 风格转换(static_cast
、dynamic_cast
、const_cast
、reinterpret_cast
)。
隐式类型转换由编译器自动完成,通常发生在赋值、表达式计算或函数调用时。
示例
int a = 10;
double b = a; // 隐式将 int 转换为 double
int c = 3.14; // 隐式将 double 转换为 int(丢失小数部分)
1.C风格转换,C 风格转换使用 (目标类型)
的语法。
double d = 3.14;
int e = (int)d; // 显式将 double 转换为 int
2.C++风格转换,C++ 提供了四种显式转换运算符,更加安全和明确。
用于基本数据类型之间的转换,以及具有继承关系的类之间的转换。
double f = 3.14;
int g = static_cast<int>(f); // 显式将 double 转换为 int
class Base {};
class Derived : public Base {};
Base* basePtr = new Derived;
Derived* derivedPtr = static_cast<Derived*>(basePtr); // 向下转型
用于具有多态性的类之间的转换(通常用于向下转型),转换失败时返回 nullptr
。
class Base { virtual void foo() {} };
class Derived : public Base {};
Base* basePtr = new Derived;
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr); // 向下转型
if (derivedPtr) {
cout << "转换成功" << endl;
} else {
cout << "转换失败" << endl;
}
用于移除或添加 const
或 volatile
修饰符。
const int h = 10;
int* i = const_cast<int*>(&h); // 移除 const 修饰符
*i = 20; // 修改值(未定义行为,不推荐)
用于低级别的类型转换,通常用于指针或整数之间的转换。
int j = 42;
int* ptr = &j;
long k = reinterpret_cast<long>(ptr); // 将指针转换为整数
const_cast
: 移除 const
修饰符可能导致未定义行为,应谨慎使用。dynamic_cast
进行安全的向下转型:在具有多态性的类之间转换时,优先使用 dynamic_cast
。static_cast
、dynamic_cast
、const_cast
、reinterpret_cast
。const_cast
。#include <iostream>
using namespace std;
class Base {
public:
virtual void foo() { cout << "Base::foo" << endl; }
};
class Derived : public Base {
public:
void foo() override { cout << "Derived::foo" << endl; }
};
int main() {
// 隐式转换
int a = 10;
double b = a;
cout << "b = " << b << endl;
// C 风格转换
double c = 3.14;
int d = (int)c;
cout << "d = " << d << endl;
// C++ 风格转换
double e = 3.14;
int f = static_cast<int>(e);
cout << "f = " << f << endl;
// dynamic_cast
Base* basePtr = new Derived;
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);
if (derivedPtr) {
cout << "dynamic_cast 成功" << endl;
} else {
cout << "dynamic_cast 失败" << endl;
}
// const_cast
const int g = 10;
int* h = const_cast<int*>(&g);
*h = 20;
cout << "g = " << g << ", *h = " << *h << endl;
// reinterpret_cast
int i = 42;
int* ptr = &i;
long j = reinterpret_cast<long>(ptr);
cout << "j = " << j << endl;
delete basePtr;
return 0;
}
作用:用于从键盘获取数据
关键字:cin
语法: cin >> 变量
示例:
#include <iostream>
using namespace std;
int main(){
//整型输入
int a = 0;
cout << "请输入整型变量:" << endl;
cin >> a;
cout << a << endl;
//浮点型输入
double d = 0;
cout << "请输入浮点型变量:" << endl;
cin >> d;
cout << d << endl;
//字符型输入
char ch = 0;
cout << "请输入字符型变量:" << endl;
cin >> ch;
cout << ch << endl;
//字符串型输入
string str;
cout << "请输入字符串型变量:" << endl;
cin >> str;
cout << str << endl;
//布尔类型输入
bool flag = true;
cout << "请输入布尔型变量:" << endl;
cin >> flag;
cout << flag << endl;
return EXIT_SUCCESS;
}
腾讯 QQ 音乐推出“网赚畅听包”会员,付费后每天看广告获取听歌权益
开源电子书管家 Calibre 8.11 发布:整合 AI 问答功能,随时解答你的提问