字符型: char
整型:short、int、long、long long、
浮点型:float、double、
布尔型:bool
查看数据类型:typeid(s[i]).name()
根据操作系统、编译器、的不同,数据类型存储的大小也不一致。
变量声明、查看当前数据类型所占大小、查看当前数据类型使用范围
#include <iostream>
#include <climits> // 查看大小
#include <float.h> // float 查看大小
using namespace std; // 命名空间定义 使用C++标识符 例如:cout
int main()
{
// 变量声明的类型
char a1={'0'}; // 字符型 不是字符串
short a2{0}; // 整型
int a3{0}; // 整型
long a4{0}; // 整型
long long a5{0}; // 整型
float a6{0.1}; // 浮点型
double a7{0.1}; // 浮点型
bool a8{true}; // 布尔型
cout << "a1 = " << a1 << endl;
cout << "a2 = " << a2 << endl;
cout << "a3 = " << a3 << endl;
cout << "a4 = " << a4 << endl;
cout << "a5 = " << a5 << endl;
cout << "a6 = " << a6 << endl;
cout << "a7 = " << a7 << endl;
cout << "a8 = " << a8 << endl;
cout << "========================================"<< endl;
// 查看数据类型名
cout << typeid(a8).name() << endl;
// sizeof 查看大小 单位字节
cout << "char:" << sizeof(char)<< endl;
cout << "short:" << sizeof(short)<< endl;
cout << "int:" << sizeof(int)<< endl;
cout << "long:" << sizeof(long)<< endl;
cout << "long long:" << sizeof(long long)<< endl;
cout << "float:" << sizeof(float)<< endl;
cout << "double:" << sizeof(double)<< endl;
// cout << "bool:" << sizeof(bool)<< endl;
cout << "========================================"<< endl;
// 查看数据类型的范围
cout << "char min~max:" << CHAR_MIN <<" ~ "<< CHAR_MAX<< endl;
cout << "short min~max:" << SHRT_MIN <<" ~ "<< SHRT_MAX<< endl;
cout << "int min~max:" << INT_MIN <<" ~ "<< INT_MAX<< endl;
cout << "long min~max:" << LONG_MIN <<" ~ "<< LONG_MAX<< endl;
cout << "long long min~max:" << LLONG_MIN <<" ~ "<< LLONG_MAX<< endl;
// float.h 导入头文件
cout << "float min~max:" << FLT_MIN <<" ~ "<< FLT_MAX<< endl;
cout << "double min~max:" << DBL_MIN <<" ~ "<< DBL_MAX<< endl;
return 0;
}
常量定义、数组的定义与修改
#include <iostream>
using namespace std; // 命名空间定义 使用C++标识符 例如:cout
int main()
{
// 定义常量 const
const int N {100};
cout << N << endl;
cout << "============================" << endl;
// 定义数组:类型 名字 数量 初始化
//
char a1[3] {'a', 'b', 'c'};
// char a2[] {'a1', 'b1', 'c1'}; // 这种是错误的,
double a3[] {0.1, 0.2, 0.3}; // 浮点型 定义数组时 数量(可选)
double b3[4] {0.1, 0.2}; // 未经初始化的数组内,数是随机的. 若初始一部分,其他默认用0填充
cout << a1[0] << " "<< a1[1] << " "<< a1[2] << " "<< endl;
cout << a3[0] << " "<< a3[1] << " "<< a3[2] << " "<< endl;
cout << b3[0] << " "<< b3[1] << " "<< b3[2] << " "<< b3[3] << endl;
cout << "============================" << endl;
// 数组元素修改
double a4[] {0.1, 0.2, 0.3};
// 修改前
cout << a4[0] << " "<< a4[1] << " "<< a4[2] << " "<< endl;
// 修改
a4[0] = 0.01;
// 修改后
cout << a4[0] << " "<< a4[1] << " "<< a4[2] << " "<< endl;
cout << "============================" << endl;
return 0;
}
复制内存 memcpy
#include <iostream>
#include <memory.h>
using namespace std;
// 查看类型
// 自动判断类型
int main()
{
int arr1[5] = {};
int arr2[5] = {1, 2, 3, 4, 5};
// 复制内存
// 目标数组、原始数组、复制多少
memcpy(arr1, arr2, sizeof(arr1));
for (int i = 0; i < 5; i++)
cout << arr1[i];
return 0;
}
定义多维数组
#include <iostream>
using namespace std; // 命名空间定义 使用C++标识符 例如:cout
int main()
{
// 定义多维数组
double a4[3][4]
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
cout << "============================" << endl;
cout << a4[0][0] << " "<< a4[0][1] << " "<< a4[0][2] << " "<< endl;
cout << "============================" << endl;
return 0;
}