C++快速入门
一、准备工作:搭建 C++ 开发环境
C++ 需要编译器(将代码翻译成机器语言)+ 编辑器,新手推荐两种简单方案:
方案 1:轻量快捷(推荐新手)
- 编译器:MinGW(Windows)/ Clang(Mac)/ GCC(Linux)
- Windows:下载「MinGW-w64」,安装后配置环境变量(将
bin目录添加到 PATH),验证:终端输入g++ --version显示版本即成功。 - Mac:终端输入
xcode-select --install安装 Command Line Tools,自带 Clang。 - Linux:
sudo apt install g++(Ubuntu)/sudo yum install gcc-c++(CentOS)。
- Windows:下载「MinGW-w64」,安装后配置环境变量(将
- 编辑器:VS Code(安装「C/C++」插件,微软官方),支持代码高亮、编译运行。
方案 2:一站式 IDE(无需配置)
- 下载 Dev-C++、Code::Blocks 或 CLion(社区版免费),安装后直接用,适合完全零基础的新手。
二、核心基础:快速掌握必学知识点
C++ 是静态类型语言(变量必须声明类型),语法比 Python 严谨,先从最核心的部分入手。
1. 第一个 C++ 程序(Hello World)
新建文件
hello.cpp(C++ 文件后缀为.cpp),输入代码:cpp
运行
// 注释:单行注释用//,多行注释用/* ... */
// 包含输入输出头文件(相当于Python的import)
#include <iostream>
// 主函数:程序入口,必须有,返回int类型
int main() {
// cout:输出流,<< 是输出运算符,endl是换行(等价于\n)
std::cout << "Hello, C++!" << std::endl;
// return 0:表示程序正常结束(0是返回值)
return 0;
}
编译运行:
- 终端输入:
g++ hello.cpp -o hello(编译生成可执行文件 hello) - 运行:
./hello(Mac/Linux)或hello.exe(Windows) - 输出:
Hello, C++!
2. 变量与数据类型(必须声明类型)
C++ 变量使用前必须声明「类型 + 变量名」,核心数据类型如下:
cpp
运行
#include <iostream>
using namespace std; // 引入std命名空间,后续不用写std::
int main() {
// 基本数据类型
int age = 18; // 整数(4字节,范围约±20亿)
float height = 1.75f; // 单精度浮点数(加f区分)
double weight = 65.5; // 双精度浮点数(精度更高)
char ch = 'A'; // 字符(单引号,1字节)
bool is_student = true;// 布尔值(true/false,小写)
string name = "小明"; // 字符串(需包含<string>头文件)
// 输出变量(可以拼接输出)
cout << "姓名:" << name << endl;
cout << "年龄:" << age << " 身高:" << height << endl;
cout << "字符:" << ch << " 是否学生:" << boolalpha << is_student << endl;
// boolalpha:让布尔值输出true/false,而非1/0
return 0;
}
编译运行需注意:字符串
string需要包含头文件,编译命令不变,输出:plaintext
姓名:小明
年龄:18 身高:1.75
字符:A 是否学生:true
3. 基本运算(与 Python 类似,语法更严谨)
cpp
运行
#include <iostream>
using namespace std;
int main() {
// 算术运算:+ - * / % ++ --(自增/自减是C++特色)
int a = 10, b = 3;
cout << a + b << endl; // 13
cout << a / b << endl; // 3(整数除法,直接截断小数)
cout << (double)a / b << endl; // 3.33333(强制类型转换)
cout << a % b << endl; // 1(取余)
// 自增自减:++a(先加后用)、a++(先用后加)
int c = 5;
cout << c++ << endl; // 5(输出后c变成6)
cout << ++c << endl; // 7(先加1再输出)
return 0;
}
4. 条件判断(if-else,语法与 Python 差异:无缩进,用 {})
cpp
运行
#include <iostream>
using namespace std;
int main() {
int score = 85;
// 条件判断:if(条件){...},条件用()包裹,代码块用{}
if (score >= 90) {
cout << "优秀" << endl;
} else if (score >= 80) {
cout << "良好" << endl;
} else if (score >= 60) {
cout << "及格" << endl;
} else {
cout << "不及格" << endl;
}
return 0;
}
输出:
良好5. 循环(for/while,核心逻辑与 Python 一致,语法不同)
cpp
运行
#include <iostream>
using namespace std;
int main() {
// 1. for循环:for(初始化; 条件; 增量){...}
for (int i = 0; i < 5; i++) { // i从0到4,循环5次
cout << "第" << i+1 << "次循环" << endl;
}
// 2. while循环
int count = 0;
while (count < 3) {
cout << "count = " << count << endl;
count++;
}
return 0;
}
输出:
plaintext
第1次循环
第2次循环
第3次循环
第4次循环
第5次循环
count = 0
count = 1
count = 2
6. 数组(C++ 的「列表」,固定长度)
C++ 数组长度固定,声明时需指定大小,索引从 0 开始:
cpp
运行
#include <iostream>
using namespace std;
int main() {
// 声明数组:类型 数组名[长度]
int nums[5] = {10, 20, 30, 40, 50}; // 长度5的整数数组
string fruits[3] = {"苹果", "香蕉", "橙子"};
// 访问数组元素
cout << nums[0] << endl; // 10
cout << fruits[1] << endl; // 香蕉
// 遍历数组(用for循环)
for (int i = 0; i < 5; i++) {
cout << nums[i] << " ";
}
cout << endl;
return 0;
}
输出:
plaintext
10
香蕉
10 20 30 40 50
三、新手实战:猜数字小游戏
结合基础语法,写一个 C++ 版猜数字游戏,巩固知识点:
cpp
运行
#include <iostream>
#include <cstdlib> // 包含rand()、srand()
#include <ctime> // 包含time()
using namespace std;
int main() {
// 设置随机数种子(避免每次随机数相同)
srand((unsigned)time(NULL));
// 生成1-10的随机数
int secret_num = rand() % 10 + 1;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经生成了1-10之间的随机数,猜猜是多少?" << endl;
int guess;
while (true) {
cout << "请输入你的猜测:";
cin >> guess; // 接收用户输入(自动转整数)
if (guess == secret_num) {
cout << "恭喜你,猜对了!" << endl;
break;
} else if (guess < secret_num) {
cout << "太小了,再试试!" << endl;
} else {
cout << "太大了,再试试!" << endl;
}
}
return 0;
}
编译运行:
bash
运行
g++ guess.cpp -o guess
./guess # Mac/Linux 或 guess.exe(Windows)
运行后输入数字,程序会提示猜大 / 猜小,直到猜对。
总结
- 核心差异(对比 Python):C++ 是静态类型语言,变量必须声明类型;代码块用
{}包裹(无缩进要求);程序入口是main()函数,必须返回int。 - 入门关键语法:掌握「变量声明→条件判断→循环→数组」,能写出基础交互程序;记住编译运行的两步:
g++ 文件名.cpp -o 可执行文件→ 运行可执行文件。 - 新手避坑点:编译错误常因「忘记包含头文件」(如
<iostream>/<string>)、「变量未声明类型」、「数组越界」,遇到错误先检查这几点。

浙公网安备 33010602011771号