Spring Boot 自动配置
2026/1/11 4:00:03
C++ 支持多种数据类型,包括整数、浮点数、字符和布尔值。
int(4字节)、short(2字节)、long(4或8字节)、long long(8字节)。float(4字节)、double(8字节)、long double(16字节)。char(1字节),用于存储单个字符。bool,值为true或false。声明变量示例:
int age = 25; double price = 99.99; char grade = 'A'; bool isStudent = true;使用iostream库中的cin和cout进行输入输出操作。
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; cout << "You entered: " << num << endl; return 0; }C++ 提供条件语句和循环结构控制程序流程。
条件语句:
if (age >= 18) { cout << "Adult" << endl; } else { cout << "Minor" << endl; }循环结构:
for循环:for (int i = 0; i < 5; i++) { cout << i << endl; }while循环:int i = 0; while (i < 5) { cout << i << endl; i++; }函数用于封装可重用的代码块。
int add(int a, int b) { return a + b; } int main() { int result = add(3, 4); cout << "Sum: " << result << endl; return 0; }数组用于存储相同类型的多个元素。
int numbers[5] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { cout << numbers[i] << endl; }指针存储变量的内存地址。
int var = 10; int* ptr = &var; cout << "Value: " << *ptr << endl; // 输出 10C++ 支持面向对象编程,通过类定义对象的行为和属性。
class Rectangle { public: int width, height; int area() { return width * height; } }; int main() { Rectangle rect; rect.width = 5; rect.height = 10; cout << "Area: " << rect.area() << endl; return 0; }STL 提供常用数据结构(如向量、列表)和算法。
向量示例:
#include <vector> using namespace std; int main() { vector<int> nums = {1, 2, 3}; nums.push_back(4); // 添加元素 for (int num : nums) { cout << num << endl; } return 0; }