【c++的各种数据类型与头文件的用处,基础语法】
C 数据类型基本数据类型整型int通常为 4 字节存储整数。short2 字节范围较小。long4 或 8 字节取决于系统。long long8 字节存储更大整数。浮点型float4 字节单精度浮点数。double8 字节双精度浮点数。long double扩展精度浮点数。字符型char1 字节存储单个字符。wchar_t宽字符用于 Unicode。布尔型bool存储true或false。派生数据类型数组固定大小的同类型元素集合。示例int arr[5] {1, 2, 3, 4, 5};指针存储内存地址。示例int* ptr var;引用变量的别名。示例int ref var;结构体与类struct组合不同类型的数据。class支持数据封装与继承。常用头文件及用途输入输出iostream提供cin、cout等标准输入输出功能。#include iostream using namespace std; int main() { cout Hello, World!; return 0; }数学运算cmath包含数学函数如sqrt()、sin()。#include cmath double result sqrt(25.0);字符串处理string支持std::string类型及操作。#include string string str C;动态内存管理new提供动态内存分配功能。int* arr new int[10]; delete[] arr;算法与容器algorithm包含排序、查找等算法。vector实现动态数组。#include vector vectorint vec {1, 2, 3};bits/stdc.h 万能头基础语法变量声明与初始化int a 10; // 直接初始化 double b{3.14}; // 列表初始化控制结构条件语句if (a b) { cout a is greater; } else { cout b is greater; }循环for (int i 0; i 5; i) { cout i endl; } while (a 10) { a; }函数定义int add(int x, int y) { return x y; }类与对象class Rectangle { public: int width, height; int area() { return width * height; } }; Rectangle rect; rect.width 5; rect.height 10;