C++ 快速入门指南
1. 安装 C++
- 从 Microsoft Visual Studio 下载并安装最新版本的 Visual Studio,或从 GCC 官方网站 下载并安装 GCC 编译器。
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++ 使用 if、else if 和 else 进行条件判断:
#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++ 支持 for、while 和 do-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_type 和 function_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. 异常处理
使用 try、catch 和 throw 处理异常:
#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++ 官方文档。

1486

被折叠的 条评论
为什么被折叠?



