02x数据类型8-15
基本的内置类型
C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:
| 类型 | 关键字 |
|---|---|
| 布尔型 | bool |
| 字符型 | char |
| 整型 | int |
| 浮点型 | float |
| 双浮点型 | double |
| 无类型 | void |
| 宽字符型 | wchar_t |
| 类型 | 描述 |
|---|---|
| bool | 布尔类型,存储值 true 或 false,占用 1 个字节。 |
| char | 字符类型,用于存储 ASCII 字符,通常占用 1 个字节。 |
| int | 整数类型,通常用于存储普通整数,通常占用 4 个字节。 |
| float | 单精度浮点值,用于存储单精度浮点数。单精度是这样的格式,1 位符号,8 位指数,23 位小数,通常占用4个字节。![]() |
| double | 双精度浮点值,用于存储双精度浮点数。双精度是 1 位符号,11 位指数,52 位小数,通常占用 8 个字节。![]() |
| void | 表示类型的缺失。 |
| wchar_t | 宽字符类型,用于存储更大范围的字符,通常占用 2 个或 4 个字节。 |
下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类
型的变量所能存储的最大值和最小值。
**注意:**不同系统会有所差异,一字节为 8 位。
**注意:**默认情况下,int、short、long都是带符号的,即 signed。
**注意:**long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。
| 类型 | 位 | 范围 |
|---|---|---|
| char | 1 个字节 | -128 到 127 或者 0 到 255 |
| unsigned char | 1 个字节 | 0 到 255 |
| signed char | 1 个字节 | -128 到 127 |
| int | 4 个字节 | -2147483648 到 2147483647 |
| unsigned int | 4 个字节 | 0 到 4294967295 |
| signed int | 4 个字节 | -2147483648 到 2147483647 |
| short int | 2 个字节 | -32768 到 32767 |
| unsigned short int | 2 个字节 | 0 到 65,535 |
| signed short int | 2 个字节 | -32768 到 32767 |
| long int | 8 个字节 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
| signed long int | 8 个字节 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
| unsigned long int | 8 个字节 | 0 到 18,446,744,073,709,551,615 |
| float | 4 个字节 | 精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字) |
| double | 8 个字节 | 双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字) |
| long long | 8 个字节 | 双精度型占8 个字节(64位)内存空间,表示 -9,223,372,036,854,775,807 到 9,223,372,036,854,775,807 的范围 |
| long double | 16 个字节 | 长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。 |
| wchar_t | 2 或 4 个字节 | 1 个宽字符 |
sizeof 用法
#include<iostream>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)();
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************"<< endl;
return 0;
}
typedef 声明
您可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:
typedef type newname; 派生数据类型
| 数据类型 | 描述 | 示例 |
|---|---|---|
数组 | 相同类型元素的集合 | 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 }; |
类型别名
| 别名 | 描述 | 示例 |
|---|---|---|
typedef | 为现有类型定义别名 | typedef int MyInt; |
using | 为现有类型定义别名(C++11 引入) | using MyInt = int; |
标准库类型
| 数据类型 | 描述 | 示例 |
|---|---|---|
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}; |
常见创建变量错误
写值时的单双引号

一个字符变量想写多个字符的方法


转义字符
| 转义序列 | 含义 |
|---|---|
\\ | \ 字符 |
\' | ’ 字符 |
\" | ” 字符 |
\? | ? 字符 |
| \a | 警报铃声 |
| \b | 退格键 |
| \f | 换页符 |
| \n | 换行符 |
| \r | 回车 |
| \t | 水平制表符 占8个位置 |
| \v | 垂直制表符 |
| \ooo | 一到三位的八进制数 |
| \xhh … | 一个或多个数字的十六进制数 |
\t水平制表符的对齐效果

\v垂直制表符对齐效果

字符串型
- C风格型字符串:
char charname[] = "ABCDEF",注意是双引号。 - C++风格字符串:
string strname = "ABCDEF",注意是双引号。头文件要包含#include <string>

#include<iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
char ch1[] = "Hello World"; // C-style string
string str1 = "Hello C++"; // C++ string
cout << ch1 << endl;
cout << str1 << endl;
return 0;
}布尔类型
bool:表示布尔类型,只有 true(1) 和 false(0) 两个值。

#include<iostream>
using namespace std;
int main()
{
bool flag = true;
cout << flag << endl;
flag = 0;
cout << flag << endl;
cout << sizeof(flag) << endl;
}数据输入
从键盘获取输入: cin >> 变量

#include<iostream>
using namespace std;
int main()
{
int a = 10; //int是C++中的整型
cout << "a的值为" << a << endl;
cout << "输入a的新值" << endl;
cin >> a;
cout << "a的新值为" << a << endl;
float b = 10.5; //float是C++中的浮点类型
cout << "b的值为" << b << endl;
cout << "输入b的新值" << endl;
cin >> b;
cout << "b的新值为" << b << endl;
char c = 'a'; //char是C++中的字符类型
cout << "c的值为" << c << endl;
cout << "输入c的新值" << endl;
cin >> c;
cout << "c的新值为" << c << endl;
bool d = true; //bool是C++中的布尔类型
cout << "d的值为" << d << endl;
cout << "输入d的新值" << endl;
cin >> d; //只要非0都是真
cout << "d的新值为" << d << endl;
string e = "Hello World"; //string是C++中的字符串类型
cout << "e的值为" << e << endl;
cout << "输入e的新值" << endl;
cin >> e;
cout << "e的新值为" << e << endl;
return 0;
}
🔔 想要获取更多网络安全与编程技术干货? 关注 泷羽Sec-静安 公众号,与你一起探索前沿技术,分享实用的学习资源与工具。我们专注于深入分析,拒绝浮躁,只做最实用的技术分享!💻
扫描下方二维码,马上加入我们,共同成长!🌟
👉 长按或扫描二维码关注公众号

或者直接回复文章中的关键词,获取更多技术资料与书单推荐!📚