C++ 快速入门指南

C++ 快速入门指南

1. 安装 C++

2. 基本语法

2.1 变量和数据类型

C++ 支持多种数据类型,如整数、浮点数、字符、字符串、数组和指针。以下是一些示例代码:

#include <iostream>
using namespace std;

int main() {
    // 数字
    int a = 10;
    double b = 3.14;

    // 字符
    char c = 'A';

    // 字符串
    string name = "Alice";

    // 布尔值
    bool is_active = true;

    // 数组
    int numbers[] = {1, 2, 3, 4, 5};

    // 指针
    int *p = &a;

    cout << "Name: " << name << endl;
    return 0;
}
2.2 基本操作
#include <iostream>
using namespace std;

int main() {
    int a = 10;
    double b = 3.14;

    // 算术运算
    int sum = a + b;
    int difference = a - b;
    double product = a * b;
    double quotient = a / b;

    // 字符串操作
    string greeting = "Hello, " + name;

    // 数组操作
    int numbers[] = {1, 2, 3, 4, 5};
    numbers[0] = 10;

    cout << "Sum: " << sum << endl;
    return 0;
}

3. 控制结构

3.1 条件语句

C++ 使用 ifelse ifelse 进行条件判断:

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 5;

    if (a > b) {
        cout << "a is greater than b" << endl;
    } else if (a == b) {
        cout << "a is equal to b" << endl;
    } else {
        cout << "a is less than b" << endl;
    }

    return 0;
}
3.2 循环

C++ 支持 forwhiledo-while 循环:

#include <iostream>
using namespace std;

int main() {
    // for 循环
    for (int i = 0; i < 5; i++) {
        cout << "i: " << i << endl;
    }

    // while 循环
    int count = 0;
    while (count < 5) {
        cout << "count: " << count << endl;
        count++;
    }

    // do-while 循环
    int num = 0;
    do {
        cout << "num: " << num << endl;
        num++;
    } while (num < 5);

    return 0;
}

4. 函数

函数用 return_typefunction_name 定义:

#include <iostream>
using namespace std;

int add(int x, int y) {
    return x + y;
}

int main() {
    int sum = add(10, 20);
    cout << "Sum: " << sum << endl;
    return 0;
}

5. 类和对象

C++ 是面向对象的编程语言,可以定义类和创建对象:

#include <iostream>
using namespace std;

class Person {
public:
    string name;
    int age;

    Person(string n, int a) {
        name = n;
        age = a;
    }

    void introduce() {
        cout << "My name is " << name << " and I am " << age << " years old." << endl;
    }
};

int main() {
    Person alice("Alice", 25);
    alice.introduce();
    return 0;
}

6. 模板和 STL

6.1 模板

模板允许你编写与类型无关的代码:

#include <iostream>
using namespace std;

template <typename T>
T add(T x, T y) {
    return x + y;
}

int main() {
    cout << "Int Sum: " << add(10, 20) << endl;
    cout << "Double Sum: " << add(3.14, 2.72) << endl;
    return 0;
}
6.2 标准模板库 (STL)

STL 提供许多有用的容器和算法:

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

int main() {
    vector<int> numbers = {1, 2, 3, 4, 5};

    // 使用迭代器遍历
    for (auto it = numbers.begin(); it != numbers.end(); ++it) {
        cout << *it << endl;
    }

    // 使用算法
    sort(numbers.begin(), numbers.end());

    return 0;
}

7. 文件操作

C++ 提供简单的文件读写功能:

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

int main() {
    // 写入文件
    ofstream outfile("example.txt");
    outfile << "Hello, world!" << endl;
    outfile.close();

    // 读取文件
    ifstream infile("example.txt");
    string content;
    while (getline(infile, content)) {
        cout << content << endl;
    }
    infile.close();

    return 0;
}

8. 异常处理

使用 trycatchthrow 处理异常:

#include <iostream>
using namespace std;

int main() {
    try {
        int a = 10;
        int b = 0;
        if (b == 0) {
            throw "Division by zero!";
        }
        int c = a / b;
    } catch (const char* msg) {
        cerr << msg << endl;
    }

    return 0;
}

9. 常用库

C++ 生态系统中有许多强大的库,例如:

  • Boost: 提供许多扩展功能
  • Qt: 图形用户界面开发
  • OpenCV: 计算机视觉

总结

这份文档涵盖了 C++ 的基础知识和常用操作。通过不断练习和参考官方文档,你将能够掌握 C++ 并应用于各种项目中。

有关更多详细信息,请参考 C++ 官方文档

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城十三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值