欢迎来到hydrooj

以下为c++基础知识

c++框架及部分代码解释

1 #include<iostream> 
2 using namespace std;
3 int main(){
4     return 0;
5 }
6 //cout<<"hello";输出hello
7 //endl换行一般与cout一起使用
8 //cin>>a;输入变量a 

1. 基本输出

#include <iostream>  // 包含输入输出流的头文件
using namespace std;
int main() {         // 主函数,程序入口
    cout << "Hello, World!";  // 输出字符串到控制台
    return 0;        // 返回0表示程序正常结束
}

解释:

#include 是C++标准输入输出库的头文件

main() 是每个C++程序必须有的主函数

std::cout 是标准输出流对象

<< 是流插入运算符,用于向输出流发送数据

return 0 表示程序成功结束

2. 变量声明与使用

#include <iostream>
using namespace std;  // 使用标准命名空间,避免重复写std::

int main() {
    int age = 25;            // 声明并初始化整型变量
    double height = 1.75;    // 声明并初始化双精度浮点变量
    char grade = 'A';        // 声明并初始化字符变量
    string name = "Alice";   // 声明并初始化字符串变量
    
    cout << "Name: " << name << endl;     // endl表示换行
    cout << "Age: " << age << endl;
    cout << "Height: " << height << "m" << endl;
    cout << "Grade: " << grade << endl;
    
    return 0;
}

3. 基本输入

#include <iostream>
using namespace std;

int main() {
    int number;
    
    cout << "Enter a number: ";  // 提示用户输入
    cin >> number;               // 从键盘读取输入并存储到变量
    
    cout << "You entered: " << number << endl;
    
    return 0;
}

4. 条件语句

#include <iostream>
using namespace std;

int main() {
    int score;
    cout << "Enter your score: ";
    cin >> score;
    
    if (score >= 90) {
        cout << "Excellent!" << endl;
    } 
    else if (score >= 60) {
        cout << "Pass" << endl;
    }
    else {
        cout << "Fail" << endl;
    }
    
    return 0;
}

5. 循环结构

while循环

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {      // 当条件为真时循环
        cout << i << " ";
        i++;             // 相当于i = i + 1
    }
    return 0;
}

for循环

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {  // 初始化;条件;更新
        cout << i << " ";
    }
    return 0;
}

6. 数组基础

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};  // 声明并初始化数组
    
    for (int i = 0; i < 5; i++) {
        cout << numbers[i] << " ";  // 访问数组元素
    }
    
    return 0;
}

7. 函数基础

#include <iostream>
using namespace std;

// 函数声明
int add(int a, int b);

int main() {
    int result = add(3, 4);  // 调用函数
    cout << "3 + 4 = " << result << endl;
    return 0;
}

// 函数定义
int add(int a, int b) {
    return a + b;  // 返回两个数的和
}

8. 类和对象基础

#include <iostream>
using namespace std;

// 定义一个简单的类
class Rectangle {
public:
    double length;
    double width;
    
    double area() {  // 成员函数
        return length * width;
    }
};

int main() {
    Rectangle rect;  // 创建对象
    
    rect.length = 5.0;
    rect.width = 3.0;
    
    cout << "Area: " << rect.area() << endl;
    
    return 0;
}

以下为c++进阶知识

9. 指针基础

#include <iostream>
using namespace std;

int main() {
    int var = 20;     // 普通变量
    int *ptr = &var;  // 指针变量,存储var的地址
    
    cout << "变量值: " << var << endl;
    cout << "变量地址: " << &var << endl;
    cout << "指针存储的地址: " << ptr << endl;
    cout << "指针指向的值: " << *ptr << endl;  // 解引用
    
    *ptr = 30;  // 通过指针修改变量值
    cout << "修改后的变量值: " << var << endl;
    
    return 0;
}

10.引用基础

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int &ref = a;  // 引用,ref是a的别名
    
    cout << "a = " << a << ", ref = " << ref << endl;
    ref = 20;  // 通过引用修改值
    cout << "修改后 a = " << a << endl;
    
    return 0;
}

11.动态内存分配

#include <iostream>
using namespace std;

int main() {
    int *ptr = new int;  // 动态分配内存
    *ptr = 100;
    
    cout << "动态分配的值: " << *ptr << endl;
    
    delete ptr;  // 释放内存
    ptr = nullptr;  // 避免悬空指针
    
    // 动态分配数组
    int size = 5;
    int *arr = new int[size];
    for(int i = 0; i < size; i++) {
        arr[i] = i * 10;
    }
    
    delete[] arr;  // 释放数组内存
    
    return 0;
}

12.结构体

#include <iostream>
#include <string>
using namespace std;

struct Student {
    string name;
    int age;
    double score;
};

int main() {
    Student s1;
    s1.name = "张三";
    s1.age = 18;
    s1.score = 92.5;
    
    Student s2 = {"李四", 19, 88.5};
    
    cout << s1.name << "的成绩是" << s1.score << endl;
    cout << s2.name << "的成绩是" << s2.score << endl;
    
    return 0;
}

13.函数重载

#include <iostream>
using namespace std;

// 同名函数,参数不同
void print(int i) {
    cout << "整数: " << i << endl;
}

void print(double f) {
    cout << "浮点数: " << f << endl;
}

void print(string s) {
    cout << "字符串: " << s << endl;
}

int main() {
    print(5);
    print(3.14);
    print("Hello C++");
    
    return 0;
}

14.文件操作

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // 写入文件
    ofstream outFile("example.txt");
    if(outFile.is_open()) {
        outFile << "这是第一行\n";
        outFile << "这是第二行\n";
        outFile.close();
    }
    
    // 读取文件
    string line;
    ifstream inFile("example.txt");
    if(inFile.is_open()) {
        while(getline(inFile, line)) {
            cout << line << endl;
        }
        inFile.close();
    }
    
    return 0;
}

15.异常处理

#include <iostream>
using namespace std;

int divide(int a, int b) {
    if(b == 0) {
        throw "除数不能为零!";
    }
    return a / b;
}

int main() {
    try {
        cout << divide(10, 2) << endl;
        cout << divide(10, 0) << endl;  // 会抛出异常
    }
    catch(const char* msg) {
        cerr << "错误: " << msg << endl;
    }
    
    return 0;
}

16.STL容器 - vector

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {1, 2, 3, 4, 5};
    
    // 添加元素
    nums.push_back(6);
    
    // 遍历vector
    cout << "vector元素: ";
    for(int num : nums) {
        cout << num << " ";
    }
    cout << endl;
    
    // 访问元素
    cout << "第一个元素: " << nums[0] << endl;
    cout << "最后一个元素: " << nums.back() << endl;
    
    // 删除元素
    nums.pop_back();
    
    return 0;
}

17.标准算法库

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {5, 2, 8, 1, 9};
    
    // 排序
    sort(nums.begin(), nums.end());
    
    // 查找
    if(find(nums.begin(), nums.end(), 8) != nums.end()) {
        cout << "找到了8" << endl;
    }
    
    // 反转
    reverse(nums.begin(), nums.end());
    
    // 遍历输出
    for(int num : nums) {
        cout << num << " ";
    }
    
    return 0;
}

18.类和对象进阶

#include <iostream>
using namespace std;

class BankAccount {
private:
    string owner;
    double balance;
    
public:
    // 构造函数
    BankAccount(string name, double initial) : owner(name), balance(initial) {}
    
    // 成员函数
    void deposit(double amount) {
        balance += amount;
    }
    
    bool withdraw(double amount) {
        if(amount > balance) {
            return false;
        }
        balance -= amount;
        return true;
    }
    
    void display() {
        cout << owner << "的余额: " << balance << endl;
    }
};

int main() {
    BankAccount acc("张三", 1000);
    acc.deposit(500);
    acc.display();
    
    if(acc.withdraw(200)) {
        cout << "取款成功" << endl;
    }
    acc.display();
    
    return 0;
}

比赛

  1. 22
    2025-7

    测试

    • IOI
    • Rated
    • 2 小时
    • 2

排名

排名 用户名 RP 个人简介
1 21
2 11
3 10
4 10

讨论

  1. 0
    评论

    BT