#include <iostream>
using namespace std;
int main() {
// 创建一个包含5个整数的数组
int arr[5] = {1, 2, 3, 4, 5};
// 输出数组中的元素
for (int i = 0; i < 5; i++) {
cout << "arr[" << i << "] = " << arr[i] << endl;
}
return 0;
}
包含头文件和命名空间:
#include <iostream>
:引入输入输出流库,以便使用 cout
和 cin
。using namespace std;
:使用标准命名空间,避免每次调用标准库函数时都要加上 std::
前缀。创建数组:
int arr[5] = {1, 2, 3, 4, 5};
:定义一个包含5个整数的数组,并初始化这些元素为1到5。遍历数组并输出:
for
循环遍历数组中的每个元素,并通过 cout
输出每个元素的值及其索引位置。上一篇:c++集合
下一篇:c++ const_cast
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站