C++ 教程
概述 C++ 的起源、跨平台与面向对象定位、适用读者前置基础,并通过 Hello World 示例对比 std::endl 与换行符的缓冲行为差异。
学完这一章,你能够
- 概述 C++ 的起源、跨平台与面向对象定位、适用读者前置基础,并通过 Hello World 示例对比 std::endl 与换行符的缓冲行为差异。
C++ 语言定位与适用人群
C++ 由 Bjarne Stroustrup 于 1979 年在贝尔实验室发起设计,在 C 语言基础上引入面向对象、泛型与现代系统编程能力,可编译运行于 Windows、macOS 及各类 UNIX/Linux 平台。本系列教程循序渐进覆盖基础语法到现代标准库实践,读者既可在本地编译器练习,也可配合在线运行环境验证代码效果。
本教程面向希望系统掌握 C++ 的初学者与工程开发者,内容由浅入深覆盖基础语法至进阶机制。开始动手编写示例前,只需具备对计算机程序执行流程与编程语言基本概念的初步了解即可顺利跟进。
编译与执行第一个 C++ 程序
经典的 C++ 入门程序通过 `#include <iostream>` 引入标准输入输出流头文件,利用 `using namespace std;`(或显式书写 `std::cout`)在主函数 `int main()` 内向标准输出写入 `"Hello, world!"`,并由 `return 0;` 向操作系统报告正常退出状态。
输出换行时既可使用流操纵符 `endl`,也可以直接输出转义字符 `"\n"`。二者生成的换行文本内容完全一致,但 `std::endl` 还会强制刷新输出缓冲区;在高频循环输出场景中优先使用 `"\n"` 可减少不必要的 I/O 刷新开销。本站实测程序已在 G++ C++17 容器中验证 `__cplusplus == 201703L`、`std::endl` 与 `"\n"` 的等价文本及流状态。
编译运行 C++17 Hello World,对比 std::endl 与换行符输出及标准版本宏
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
std::string jint(long long v) { return std::to_string(v); }
std::string juint(unsigned long long v) { return std::to_string(v); }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: __cplusplus macro confirms C++17 (201703L)
long std_ver = __cplusplus;
cases.push_back({
"cpp17_standard_macro",
lab::jint(201703),
lab::jint(std_ver),
std_ver == 201703L
});
// Case 2: std::endl vs "\n" produce identical line content
std::ostringstream s1, s2;
s1 << "Hello, world!" << std::endl;
s2 << "Hello, world!" << "\n";
bool same_text = (s1.str() == "Hello, world!\n") && (s1.str() == s2.str());
cases.push_back({
"hello_world_endl_vs_newline",
lab::esc("Hello, world!\n"),
lab::esc(s1.str()),
same_text
});
// Case 3: stream state and exit code 0 convention
int exit_code = 0;
bool stream_ok = s1.good() && s2.good() && (exit_code == 0);
cases.push_back({
"stream_format_and_exit_status",
lab::jbool(true),
lab::jbool(stream_ok),
stream_ok
});
lab::emit(cases);
return 0;
}
- 在离线 Docker G++ 13.3.0 C++17 环境中验证 __cplusplus 为 201703、std::endl 与 \n 产生相同输出文本且流状态正常。
所需环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
当前源码 SHA256:143021faf7cb577ce9c8a9b9a4b22ac337d8cb4986d6cc928be81648c0b01f7e
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
ZIP SHA256:f75fca59f494aa72b2871d5802db191379dae5ab7406badb2b2341de88f2cc41
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在循环输出大量日志行时,为什么通常建议用 `"\n"` 替代 `std::endl`?
展开参考解答
`std::endl` 除了写入换行符之外还会立即刷新输出流缓冲区,频繁刷新会显著增加系统调用与 I/O 开销;仅输出 `"\n"` 则由缓冲区按策略批量刷新,性能更高。