### C++ Hello World 程序 Source: https://learncpp-cn.github.io/cpp/198adc04 这是一个经典的“Hello World!”C++程序,演示了基本的程序结构、预处理指令、main函数以及输出语句。它展示了如何包含iostream库以进行控制台输出。 ```cpp #include int main() { std::cout << "Hello world!"; return 0; } ``` -------------------------------- ### Class with Public Access Modifier Source: https://learncpp-cn.github.io/cpp/9783892c Shows how to use the 'public' keyword in a C++ class to make members accessible from outside the class. This example makes the date members public, allowing them to be initialized directly. ```cpp class DateClass { public: int m_month; // public, can be accessed by anyone int m_day; // public, can be accessed by anyone int m_year; // public, can be accessed by anyone }; int main() { DateClass date; date.m_month = 10; // okay because m_month is public date.m_day = 14; // okay because m_day is public date.m_year = 2020; // okay because m_year is public return 0; } ``` -------------------------------- ### C++ Hello World 程序(带语法错误) Source: https://learncpp-cn.github.io/cpp/198adc04 这是一个 C++ 的“Hello world”程序,其中故意省略了第 5 行末尾的分号,以演示语法错误。 ```cpp #include int main() std::cout << "Hello world!" return 0; } ``` -------------------------------- ### Class with Private Members (Default) Source: https://learncpp-cn.github.io/cpp/9783892c Illustrates a C++ class where members are private by default, preventing direct access from outside the class. This example shows the compilation error that occurs when trying to access private members. ```cpp class DateClass // Class members are private by default { int m_month; // Default private, only accessible by other members int m_day; // Default private, only accessible by other members int m_year; // Default private, only accessible by other members }; int main() { DateClass date; date.m_month = 10; // Error date.m_day = 14; // Error date.m_year = 2020; // Error return 0; } ``` -------------------------------- ### 集成开发环境(IDE) Source: https://learncpp-cn.github.io/cpp/9b6d5a68 集成开发环境(IDE)将编辑器、编译器、链接器和调试器等开发工具集成到一个软件包中,提供统一的用户界面来简化开发流程。 ```cpp // IDE集成了代码编辑器、编译器、链接器和调试器。 // 简化了C++程序的开发、构建和调试过程。 // 下一节将讨论如何安装和使用IDE。 ``` -------------------------------- ### 编译过程 Source: https://learncpp-cn.github.io/cpp/2258fc9a 编译器将源代码转换为独立的可执行程序。一旦编译完成,程序无需编译器即可运行。现代编译器能生成高效优化的代码。 ```APIDOC 源代码 -> 编译器 -> 可执行程序 ``` -------------------------------- ### C++测试与调试 Source: https://learncpp-cn.github.io/cpp/9b6d5a68 在生成可执行文件后,需要进行测试以验证其功能是否符合预期。如果程序运行不正常,则需要进行调试来找出并修复问题。 ```cpp // 运行可执行文件以测试其输出。 // 如果程序行为不符合预期,则需要进行调试。 // 调试是找出并修复程序中错误的过程。 ``` -------------------------------- ### Web编译器使用 Source: https://learncpp-cn.github.io/cpp/fd9ffcf1 提及可以使用基于Web的编译器进行简单的练习,但功能有限。 ```text 可以使用TutorialsPoint中的编译器进行简单练习。 ``` -------------------------------- ### 解释过程 Source: https://learncpp-cn.github.io/cpp/2258fc9a 解释器直接执行源代码中的指令,无需预编译。解释器更灵活但效率较低,因为每次运行时都需要执行解释过程。 ```APIDOC 源代码 -> 解释器 -> 执行 ``` -------------------------------- ### 机器语言示例 Source: https://learncpp-cn.github.io/cpp/2258fc9a 机器语言是CPU直接理解的指令集,由二进制数字(位)组成。每条指令的长度可能不同,且机器语言程序通常不可移植到不同指令集的CPU上。 ```machine 10110000 01100001 ``` -------------------------------- ### C++链接过程详解 Source: https://learncpp-cn.github.io/cpp/9b6d5a68 本节解释了链接器的作用,它负责将编译器生成的目标文件以及库文件组合成一个可执行程序。链接器还会解析跨文件依赖关系,确保所有引用都能找到定义。标准库(如iostream)通常会被自动链接。 ```cpp // 链接器将所有目标文件(.o/.obj)组合成一个可执行文件。 // 链接器还可以链接库文件(预编译代码的集合)。 // 例如,链接C++标准库以使用iostream功能。 // 链接器确保跨文件依赖关系得到解析。 // 如果引用找不到定义,会产生链接器错误。 ``` -------------------------------- ### 命令行编译器使用 Source: https://learncpp-cn.github.io/cpp/fd9ffcf1 确认可以使用命令行编译器(如Linux上的g++),但需要自行配置编辑器。 ```text 可以使用命令行编译器,如Linux上的g++。 ``` -------------------------------- ### C++ 集成开发环境(IDE)安装 Source: https://learncpp-cn.github.io/archives 指导用户如何安装和设置C++开发所需的集成开发环境(IDE)。 ```bash # 示例:在Ubuntu上安装g++ sudo apt update sudo apt install g++ ``` -------------------------------- ### 高级语言特性 Source: https://learncpp-cn.github.io/cpp/2258fc9a 高级语言命令更接近自然语言,编写简洁易懂,且程序具有良好的可移植性,可在不同平台编译运行。 ```APIDOC 示例: C++: a = b * 2 + 5; 汇编: 需要5-6条指令 ``` -------------------------------- ### C++ 基础知识 Source: https://learncpp-cn.github.io/index 本节介绍C++的基础知识,包括语句、程序结构、注释、变量、初始化、赋值、cout、cin、endl、函数、返回值、形参、实参、关键字、命名标识符、局部作用域、操作符、空格和格式化、前向声明、定义、头文件、预处理器、头文件保护、调试程序等。 ```cpp // 示例:注释 // 这是一个单行注释 /* 这是一个 多行注释 */ // 示例:变量初始化和赋值 int count; count = 10; // 示例:cout, cin, endl #include int main() { int number; std::cout << "请输入一个数字:"; std::cin >> number; std::cout << "您输入的数字是:" << number << std::endl; return 0; } ``` -------------------------------- ### C++ 操作符 Source: https://learncpp-cn.github.io/index 本节详细介绍C++中的各种操作符,包括运算符优先级和关联性、算术运算符、递增/递减运算符及其副作用、sizeof、逗号和条件运算符、关系运算符、逻辑运算符、二进制和十进制转换、按位运算符以及位标志和位掩码。 ```cpp // 示例:算术运算符 int a = 10, b = 5; int sum = a + b; // 15 int difference = a - b; // 5 int product = a * b; // 50 int quotient = a / b; // 2 int remainder = a % b; // 0 // 示例:递增/递减运算符 int x = 5; ++x; // x becomes 6 (pre-increment) x++; // x becomes 7 (post-increment) // 示例:关系运算符 bool isEqual = (a == b); // false bool isGreater = (a > b); // true // 示例:逻辑运算符 bool logicalAnd = (true && false); // false bool logicalOr = (true || false); // true bool logicalNot = (!true); // false // 示例:按位运算符 int num1 = 5; // Binary: 0101 int num2 = 3; // Binary: 0011 int bitwiseAnd = num1 & num2; // Binary: 0001 (Decimal: 1) int bitwiseOr = num1 | num2; // Binary: 0111 (Decimal: 7) int bitwiseXor = num1 ^ num2; // Binary: 0110 (Decimal: 6) int leftShift = num1 << 1; // Binary: 1010 (Decimal: 10) int rightShift = num1 >> 1; // Binary: 0010 (Decimal: 2) // 示例:条件运算符 int max_val = (a > b) ? a : b; // max_val is 10 ``` -------------------------------- ### IDE安装问题排查 Source: https://learncpp-cn.github.io/cpp/fd9ffcf1 提供解决IDE安装和配置问题的通用建议,包括卸载重装、禁用安全软件和搜索错误信息。 ```text 1. 卸载IDE,重启计算机。 2. 暂时禁用防病毒软件。 3. 尝试安装不同的IDE。 4. 将错误信息复制到搜索引擎中查找解决方案。 ``` -------------------------------- ### C++ 运算符重载 Source: https://learncpp-cn.github.io/index 本节深入讲解C++中的运算符重载,包括运算符重载简介、使用友元函数和常规函数重载算术运算符、重载I/O运算符、使用成员函数重载运算符、重载一元运算符、比较运算符、递增/递减运算符、下标运算符、括号运算符、类型转换以及复制构造函数和赋值运算符。 ```cpp // 示例:重载加法运算符 #include class Vector2D { public: int x, y; Vector2D(int x_val = 0, int y_val = 0) : x(x_val), y(y_val) {} // Overload the + operator using a member function Vector2D operator+(const Vector2D& other) const { return Vector2D(x + other.x, y + other.y); } void print() const { std::cout << "(" << x << ", " << y << ")"; } }; int main() { Vector2D v1(1, 2); Vector2D v2(3, 4); Vector2D v3 = v1 + v2; // Uses the overloaded + operator std::cout << "v1 + v2 = "; v3.print(); std::cout << std::endl; return 0; } // 示例:重载输出运算符 (<<) #include class Point { public: int x, y; Point(int x_val = 0, int y_val = 0) : x(x_val), y(y_val) {} // Overload the << operator using a friend function friend std::ostream& operator<<(std::ostream& os, const Point& p); }; std::ostream& operator<<(std::ostream& os, const Point& p) { os << "(" << p.x << ", " << p.y << ")"; return os; } int main() { Point p1(10, 20); std::cout << "Point: " << p1 << std::endl; return 0; } // 示例:重载递增运算符 (++) (prefix) #include class Counter { public: int value; Counter(int val = 0) : value(val) {} // Overload prefix ++ operator Counter& operator++() { ++value; return *this; } void print() const { std::cout << value; } }; int main() { Counter c(5); std::cout << "Before: "; c.print(); std::cout << std::endl; ++c; // Calls operator++() std::cout << "After: "; c.print(); std::cout << std::endl; return 0; } ``` -------------------------------- ### C++ 语句和程序结构 Source: https://learncpp-cn.github.io/archives 讲解C++中的基本语句和程序结构,如变量声明、赋值、控制流语句等。 ```cpp int main() { int x = 5; if (x > 0) { // 条件语句 x++; } // 循环语句示例 for (int i = 0; i < x; ++i) { // 循环体 } return 0; } ``` -------------------------------- ### 汇编语言示例 Source: https://learncpp-cn.github.io/cpp/2258fc9a 汇编语言使用缩写代替二进制指令,比机器语言更易读写,但仍需汇编器翻译成机器语言。它比机器语言易于理解,但仍有大量指令完成简单任务,且可移植性不足。 ```assembly mov al, 061h ``` -------------------------------- ### C++ 可变范围和更多类型 Source: https://learncpp-cn.github.io/index 本节涵盖C++中的作用域、生存周期和类型转换,包括块语句、局部变量、全局变量、静态生存周期变量、命名空间、using语句、隐式和显式类型转换、std::string简介、枚举类型、枚举类、typedef和类型别名、结构体以及auto关键字。 ```cpp // 示例:局部变量和作用域 #include int main() { int outer_var = 10; if (true) { int inner_var = 20; std::cout << "Inner var: " << inner_var << std::endl; std::cout << "Outer var in if: " << outer_var << std::endl; } // std::cout << inner_var; // Error: inner_var is out of scope here std::cout << "Outer var: " << outer_var << std::endl; return 0; } // 示例:全局变量 #include int global_var = 50; void print_global() { std::cout << "Global var: " << global_var << std::endl; } // 示例:命名空间 #include namespace MyNamespace { void greet() { std::cout << "Hello from MyNamespace!" << std::endl; } } int main() { MyNamespace::greet(); return 0; } // 示例:std::string #include #include int main() { std::string message = "Hello, World!"; std::cout << message << std::endl; return 0; } // 示例:结构体 struct Point { int x; int y; }; int main() { Point p1; p1.x = 10; p1.y = 20; std::cout << "Point: (" << p1.x << ", " << p1.y << ")" << std::endl; return 0; } // 示例:auto关键字 #include int main() { auto count = 100; // count is deduced as int auto message = "C++"; // message is deduced as const char* std::cout << "Count: " << count << ", Message: " << message << std::endl; return 0; } ``` -------------------------------- ### C++编译过程详解 Source: https://learncpp-cn.github.io/cpp/9b6d5a68 本节详细阐述了C++程序的编译过程,包括编译器如何检查代码语法并将其转换为目标文件(.o或.obj)。如果代码存在语法错误,编译器会报告错误信息并中止编译。 ```cpp // 编译器检查C++源代码的语法规则。 // 如果发现错误,会报告错误信息和行号。 // 成功后,将C++源代码转换为目标文件(name.o或name.obj)。 // 例如,一个名为 'main.cpp' 的文件会被编译成 'main.o' 或 'main.obj'。 ``` -------------------------------- ### C++ 基本的面向对象编程 Source: https://learncpp-cn.github.io/index 本节介绍C++中的面向对象编程(OOP)基础,包括类和类成员、公共与私有访问修饰符、访问函数、封装、构造函数(包括成员初始化列表和委托构造函数)、析构函数、this指针、静态成员变量和函数、友元函数和类、匿名对象以及嵌套类型。 ```cpp // 示例:类和成员 #include #include class Dog { public: std::string name; int age; void bark() { std::cout << name << " says Woof!" << std::endl; } }; int main() { Dog my_dog; my_dog.name = "Buddy"; my_dog.age = 3; my_dog.bark(); return 0; } // 示例:构造函数和析构函数 #include #include class Cat { public: std::string name; // Constructor Cat(const std::string& n) : name(n) { std::cout << name << " has been created." << std::endl; } // Destructor ~Cat() { std::cout << name << " has been destroyed." << std::endl; } void meow() { std::cout << name << " says Meow!" << std::endl; } }; int main() { Cat whiskers("Whiskers"); whiskers.meow(); return 0; } // 示例:公共与私有访问修饰符 #include class Rectangle { private: int width, height; public: Rectangle(int w, int h) : width(w), height(h) {} int getArea() { return width * height; } }; int main() { Rectangle rect(10, 5); std::cout << "Area: " << rect.getArea() << std::endl; // std::cout << rect.width; // Error: width is private return 0; } // 示例:静态成员变量和函数 #include class Counter { public: static int count; Counter() { count++; } static void showCount() { std::cout << "Current count: " << count << std::endl; } }; int Counter::count = 0; // Initialize static member int main() { Counter c1; Counter c2; Counter::showCount(); // Call static member function return 0; } ``` -------------------------------- ### C++ 函数 Source: https://learncpp-cn.github.io/index 本节深入讲解C++函数,包括函数形参和实参、按值传递、通过引用传递、通过地址传递、返回值类型、内联函数、函数重载、默认参数、函数指针、栈和堆、递归、错误处理(cerr和exit)、断言、命令行参数等。 ```cpp // 示例:按值传递参数 #include void increment(int num) { num++; std::cout << "Inside function (by value): " << num << std::endl; } int main() { int value = 5; increment(value); std::cout << "Outside function (by value): " << value << std::endl; return 0; } // 示例:通过引用传递参数 #include void increment_ref(int& num) { num++; std::cout << "Inside function (by reference): " << num << std::endl; } int main() { int value = 5; increment_ref(value); std::cout << "Outside function (by reference): " << value << std::endl; return 0; } // 示例:函数重载 #include int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int main() { std::cout << "Sum of ints: " << add(5, 10) << std::endl; std::cout << "Sum of doubles: " << add(5.5, 10.5) << std::endl; return 0; } // 示例:默认参数 #include void print_message(const std::string& msg, int times = 1) { for (int i = 0; i < times; ++i) { std::cout << msg << std::endl; } } int main() { print_message("Hello"); print_message("World", 3); return 0; } // 示例:命令行参数 #include int main(int argc, char* argv[]) { std::cout << "Number of arguments: " << argc << std::endl; for (int i = 0; i < argc; ++i) { std::cout << "Argument " << i << ": " << argv[i] << std::endl; } return 0; } ``` -------------------------------- ### C++ 变量和基本数据类型 Source: https://learncpp-cn.github.io/index 本节深入探讨C++中的变量和基本数据类型,包括变量定义、初始化、赋值、void类型、sizeof运算符、整型(包括固定宽度整数)、浮点数、布尔值、字符、字面值、const、constexpr和符号常量等。 ```cpp // 示例:整型变量 int age = 30; // 示例:浮点数 float price = 19.99f; double pi = 3.1415926535; // 示例:布尔值 bool is_active = true; // 示例:字符 char initial = 'A'; // 示例:const 和 constexpr const double GRAVITY = 9.8; constexpr int MAX_SIZE = 100; // 示例:sizeof 运算符 #include int main() { std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl; return 0; } ``` -------------------------------- ### C++教程常见问题解答 Source: https://learncpp-cn.github.io/cpp/1ed2d910 解答了关于教程时效性、遇到概念不懂、遗忘知识点等常见问题,并提供了相应的解决方案和建议。 ```zh 问:这些教程是在2007年编写的。它们是否仍然相关? 是的,一点没错。 C++不会经常更改,这些教程已经更新,以适应新的内容和语言更改。 问:如果我遇到一个概念,该怎么办? 如果您不理解某些内容,请仔细阅读评论。其他读者可能遇到过类似的问题。其次,尝试浏览系列中的下一课 – 您的问题可以在那里得到解答。第三,使用搜索引擎查看您的问题(或错误消息)是否已在其他地方解决。第四,去编程问答平台上询问您的问题,例如Stack Overflow。 如果所有其他方法都尝试了且没有得到结果,请跳过您不理解的部分,稍后再回过头来。 问:如果我对某些知识遗忘了,我该怎么办? 前往教程的目录页。查找您想要了解更多信息的任何主题,您将找到讨论该主题的课程的链接。 ``` -------------------------------- ### Code::Blocks 编译器路径错误解决 Source: https://learncpp-cn.github.io/cpp/fd9ffcf1 提供解决Code::Blocks中“在GNU GCC编译器的已配置搜索路径中找不到编译器可执行文件”错误的步骤。 ```text 1. 确保下载了包含MinGW的Code::Blocks版本。 2. 尝试“重置为默认值”。 3. 在“工具链可执行文件”选项卡中,设置“编译器的安装目录”为MinGW目录。 4. 尝试完全卸载并重新安装。 ``` -------------------------------- ### Visual Studio Community 2017 安装配置 Source: https://learncpp-cn.github.io/cpp/fd9ffcf1 指导用户在Windows上安装Visual Studio Community 2017,并选择“使用C++进行桌面开发”工作负载,确保包含Windows 10 SDK。 ```text 选择 "使用C++进行桌面开发" 工作负载。 确保选择了 Windows 10 SDK。 ``` -------------------------------- ### C++ 内联函数 Source: https://learncpp-cn.github.io/archives 解释C++中的内联函数,它是一种请求编译器将函数体直接插入到调用点而不是进行函数调用的机制。 ```cpp inline int add(int a, int b) { return a + b; } ``` -------------------------------- ### 右值引用变量的函数调用 Source: https://learncpp-cn.github.io/cpp/96d9fb0d 展示当一个变量被声明为右值引用时,传递该变量给一个重载函数会发生什么。即使变量本身是左值,它仍然会匹配右值引用参数的函数。 ```cpp int &&ref = 5; fun(ref); ``` -------------------------------- ### C++ 教程简介 Source: https://learncpp-cn.github.io/archives 对整个C++教程进行概述,介绍学习路径和内容。 ```markdown # C++教程大纲 1. C++基础 2. 控制流 3. 函数 4. 数组与字符串 5. 指针 6. 结构体 7. 对象与类 8. ... ``` -------------------------------- ### Visual Studio 语法错误报告 Source: https://learncpp-cn.github.io/cpp/198adc04 当编译包含语法错误的 C++ 代码时,Visual Studio 编译器生成的错误消息示例。它指出了错误的位置和类型。 ```text c:\vcprojects\test1.cpp(6): error C2143: syntax error : missing ';' before 'return' ``` -------------------------------- ### C++源代码文件命名规范 Source: https://learncpp-cn.github.io/cpp/46108864 关于C++源代码文件的命名规范。建议使用`.cpp`作为文件扩展名,并为文件选择一个描述性的名称。 ```text 将您的代码文件命名为name.cpp,其中name是您选择的名称,.cpp是指示该文件是C++源文件的扩展名。 ``` -------------------------------- ### C++ 访问修饰符 Source: https://learncpp-cn.github.io/archives 介绍C++中的public和private访问修饰符,它们用于控制类成员的可访问性。 ```cpp class MyClass { public: // 公有成员 void publicMethod(); private: // 私有成员 int privateVariable; }; ``` -------------------------------- ### C++ 对象关系的介绍 Source: https://learncpp-cn.github.io/index 本节介绍C++中对象之间的关系,包括组合、聚合、关联和依赖。还介绍了容器类和std::initializer_list。 ```cpp // 示例:组合 (Composition) #include #include class Engine { public: void start() { std::cout << "Engine started." << std::endl; } void stop() { std::cout << "Engine stopped." << std::endl; } }; class Car { private: Engine engine; // Car HAS-A Engine (Composition) public: void startCar() { engine.start(); std::cout << "Car is running." << std::endl; } void stopCar() { engine.stop(); std::cout << "Car has stopped." << std::endl; } }; int main() { Car myCar; myCar.startCar(); myCar.stopCar(); return 0; } // 示例:聚合 (Aggregation) #include #include #include class Student { public: std::string name; Student(const std::string& n) : name(n) {} }; class Department { private: std::vector students; // Department HAS Students (Aggregation) public: void addStudent(Student* s) { students.push_back(s); } void displayStudents() { std::cout << "Students in department:" << std::endl; for (Student* s : students) { std::cout << "- " << s->name << std::endl; } } }; int main() { Student s1("Alice"); Student s2("Bob"); Department csDept; csDept.addStudent(&s1); csDept.addStudent(&s2); csDept.displayStudents(); return 0; } // 示例:关联 (Association) #include #include class Teacher { public: std::string name; Teacher(const std::string& n) : name(n) {} }; class Course { private: std::string title; Teacher* instructor; // Course USES-A Teacher (Association) public: Course(const std::string& t, Teacher* teacher) : title(t), instructor(teacher) {} void displayCourseInfo() { std::cout << "Course: " << title << ", Instructor: " << instructor->name << std::endl; } }; int main() { Teacher prof("Dr. Smith"); Course cppCourse("Introduction to C++", &prof); cppCourse.displayCourseInfo(); return 0; } // 示例:std::initializer_list #include #include void printNumbers(std::initializer_list nums) { std::cout << "Numbers: "; for (int num : nums) { std::cout << num << " "; } std::cout << std::endl; } int main() { printNumbers({1, 2, 3, 4, 5}); return 0; } ``` -------------------------------- ### 右值引用作为函数参数的重载 Source: https://learncpp-cn.github.io/cpp/96d9fb0d 演示如何使用右值引用重载函数,以便根据传递的是左值还是右值参数来执行不同的操作。当传递左值时,会调用 const 左值引用版本;当传递右值时,会调用右值引用版本。 ```cpp void fun(const int &lref) { std::cout << "l-value reference to const\n"; } void fun(int &&rref) { std::cout << "r-value reference\n"; } int main() { int x = 5; fun(x); fun(5); return 0; } ``` -------------------------------- ### 高级语言示例 Source: https://learncpp-cn.github.io/cpp/2258fc9a 高级语言(如C++)旨在让程序员无需关心底层硬件细节,提高了可读性和可移植性。它们需要通过编译器或解释器转换为机器可执行格式。 ```cpp a = 97; ``` -------------------------------- ### C++ 注释 Source: https://learncpp-cn.github.io/archives 介绍C++中的注释,包括单行注释和多行注释,用于解释代码。 ```cpp // 这是单行注释 /* 这是 多行注释 */ ``` -------------------------------- ### C++ 控制流 Source: https://learncpp-cn.github.io/index 本节介绍C++中的控制流语句,包括流程控制介绍、if语句、switch语句、goto语句、while语句、do-while语句、for语句、break和continue、随机数生成以及std::cin的输入处理。 ```cpp // 示例:if-else 语句 #include int main() { int score = 75; if (score >= 60) { std::cout << "Pass" << std::endl; } else { std::cout << "Fail" << std::endl; } return 0; } // 示例:switch 语句 #include int main() { char grade = 'B'; switch (grade) { case 'A': std::cout << "Excellent!" << std::endl; break; case 'B': std::cout << "Good!" << std::endl; break; default: std::cout << "Needs Improvement." << std::endl; } return 0; } // 示例:while 循环 #include int main() { int i = 1; while (i <= 5) { std::cout << i << " "; i++; } std::cout << std::endl; return 0; } // 示例:for 循环 #include int main() { for (int j = 1; j <= 5; ++j) { std::cout << j << " "; } std::cout << std::endl; return 0; } // 示例:break 和 continue #include int main() { for (int k = 1; k <= 10; ++k) { if (k == 4) { continue; // Skip printing 4 } if (k == 8) { break; // Stop the loop at 8 } std::cout << k << " "; } std::cout << std::endl; return 0; } ``` -------------------------------- ### C++ 开发介绍 Source: https://learncpp-cn.github.io/archives 介绍C++开发环境和基本流程。 ```cpp // C++程序的基本结构 #include int main() { std::cout << "Hello, C++!\n"; return 0; } ``` -------------------------------- ### C++应用领域 Source: https://learncpp-cn.github.io/cpp/a5c6b875 列举了C++在需要高性能和精确内存控制方面的典型应用场景。 ```zh 视频游戏 实时系统(运输、制造等) 高性能金融应用(高频交易) 图形应用程序和模拟 生产力/办公应用 嵌入式软件 音视频处理 ```