Home
avatar

静静

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个字节。img
double双精度浮点值,用于存储双精度浮点数。双精度是 1 位符号,11 位指数,52 位小数,通常占用 8 个字节。img
void表示类型的缺失。
wchar_t宽字符类型,用于存储更大范围的字符,通常占用 2 个或 4 个字节。

下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类

型的变量所能存储的最大值和最小值。

**注意:**不同系统会有所差异,一字节为 8 位。

**注意:**默认情况下,int、short、long都是带符号的,即 signed。

**注意:**long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。

类型范围
char1 个字节-128 到 127 或者 0 到 255
unsigned char1 个字节0 到 255
signed char1 个字节-128 到 127
int4 个字节-2147483648 到 2147483647
unsigned int4 个字节0 到 4294967295
signed int4 个字节-2147483648 到 2147483647
short int2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int2 个字节-32768 到 32767
long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int8 个字节0 到 18,446,744,073,709,551,615
float4 个字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double8 个字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long long8 个字节双精度型占8 个字节(64位)内存空间,表示 -9,223,372,036,854,775,807 到 9,223,372,036,854,775,807 的范围
long double16 个字节长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t2 或 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;  
}

image-20250114110035983

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};

常见创建变量错误

写值时的单双引号

image-20250114111334428

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

image-20250114111459334

image-20250114111615242

转义字符

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

\t水平制表符的对齐效果

image-20250114113459943

\v垂直制表符对齐效果

image-20250114113903353

字符串型

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

image-20250114114437784

#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) 两个值。

image-20250114115219203

#include<iostream>  

using namespace std;

int main()
{
	bool flag = true;
	cout << flag << endl; 
	flag = 0;
	cout << flag << endl;
	cout << sizeof(flag) << endl; 
}

数据输入

从键盘获取输入: cin >> 变量

image-20250114170107765

#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;

}

image-20250114170511130

🔔 想要获取更多网络安全与编程技术干货? 关注 泷羽Sec-静安 公众号,与你一起探索前沿技术,分享实用的学习资源与工具。我们专注于深入分析,拒绝浮躁,只做最实用的技术分享!💻

扫描下方二维码,马上加入我们,共同成长!🌟

👉 长按或扫描二维码关注公众号

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

C++