51. 数组声明与遍历
语法讲解 今天学: 数组可以存多个同类型的值,用下标访问,下标从 0 开始。 #include <iostream> using namespace std; int main() { int arr[5] = {10
52. vector 容器
语法讲解 今天学: vector 是 C++ 的动态数组,需要 #include <vector>。可以用 {} 初始化。 #include <iostream> #include <vector> us
53. vector 求和
语法讲解 今天学: 遍历 vector 时可以把所有元素加起来。 #include <iostream> #include <vector> using namespace std; int main() {
54. push_back 添加元素
语法讲解 今天学: push_back 可以在 vector 末尾添加新元素。 #include <iostream> #include <vector> using namespace std; int main
55. 范围 for 遍历
语法讲解 今天学: C++ 的范围 for(for-each)可以方便地遍历数组或 vector。 #include <iostream> #include <vector> using namespace std;
56. 统计 vector 中的偶数
语法讲解 今天学: 遍历容器时可以加条件判断来统计。 #include <iostream> #include <vector> using namespace std; int main() { vect
57. 第一个函数
语法讲解 今天学: 可以自己定义函数来封装一段逻辑,然后在 main 中调用。 #include <iostream> using namespace std; void greet() { cout <<
58. 带参数的函数
语法讲解 今天学: 函数可以接收参数,参数写在括号里。 #include <iostream> using namespace std; void say_hi(int n) { for (int i = 0; i &
59. 有返回值的函数
语法讲解 今天学: 函数可以用 return 返回一个值。 #include <iostream> using namespace std; int add(int a, int b) { return a + b;
60. 函数综合运用
语法讲解 今天学: 多个函数可以互相配合完成任务。 #include <iostream> using namespace std; int square(int x) { return x * x; } int s